js| Program
!--Login Manager-->
public static Boolean checkuserpermission (HttpSession mysession, httpservletrequest request, HttpServletResponse Response
{
if (Mysession.getvalue ("haspermission") = = NULL | |! ( (Boolean) Mysession.getvalue ("Haspermission")). Booleanvalue ())
{
String RequestedUrl = httputils.getrequesturl (Request). ToString ();
String querystring = request.getquerystring ();
if (QueryString!= null)
{
RequestedUrl = RequestedUrl + "?" + querystring;
}
RequestedUrl = Response.encodeurl (RequestedUrl);
Mysession.putvalue ("RequestedUrl", RequestedUrl);
return false;
}
Else
{
return true;
}
}
<%
//JSP file
String gooduser = "Bill";
String goodpass = "Bobo";
HttpSession mysession = Request.getsession (true);
String errormessage = "Please Login to Access the Page you requested";
Boolean loginattempt = false;
String MyMethod = Request.getmethod ();
if (request.getparametervalues ("click")!= null && request.getparametervalues ("click") [0].trim (). equals ("Log in")
{
Loginattempt = true;
}
if (loginattempt)
{
String username = request.getparametervalues ("user") [0].trim ()
String Password = request.getparametervalues ("Pass") [0].trim ();
//out.println ("username = |" + username +) | & password = | "+ Password +" |
);
if (gooduser.equals (username) && goodpass.equals (password))
{
Response.sendredirect ((String) Mysession.getvalue ("RequestedUrl"));
Mysession.putvalue ("Haspermission", New Boolean (true));
ErrorMessage = "Unable to redirect:" + (String) mysession.getvalue ("RequestedUrl");
}
Else
{
ErrorMessage = ' You did ' the Username or Password right;
}
}
Else
{
ErrorMessage = "Haven ' t tried logging in yet.";
if (Mysession.getvalue ("requestedurl") = null)
{
Mysession.putvalue ("RequestedUrl", "/index.jsp");
}
//out.println ("Set Userreferrer to" + Mysession.getvalue ("Redirectto") + "
");
}
%>
<%=errorMessage%>
Original URL: <%= (String) mysession.getvalue ("RequestedUrl")%>
How to use 5.11 checkbox in JSP
<%@ page language= "Java" contenttype= "text/html"%>
<%@ page import= "com.ora.jsp.util.*"%>
apple
banana
orange
<%
string[] picked = request.getparametervalues ("fruits");
if (picked!= null && picked.length!= 0) {
%>
You picked the following fruits:
<%= Arraysupport.contains (Picked, "Apple")? " Checked ":" "%> >apple
<%= Arraysupport.contains (Picked, "Banana")? " Checked ":" "%> >banana
<%= Arraysupport.contains (Picked, "Orange")? " Checked ":" "%> >orange
<%}%>
5.12 Request Object
• How to obtain an absolute URL address for a run-time jsp/servlet file
String URL = request.getrequesturl ();
if (request.getquerystring ()!= null)
{
url = '? ' + request.getquerystring ();
}
URL theurl = new URL (request.getscheme (), Request.getservername (), Request.getserverport (), URL);
Out.print (Url.tostring ());
• How to know which URL the client accesses this page through
String callingpage = Request.getheader ("Referer");
Out.print (Callingpage);
• If several submit buttons appear in the form, jsp/servlet how to determine which button is submitted in the form can be defined as:
Request.getparameter ("name") is used in Jsp/servlet, and can be judged based on the return value.
5.13 include directives
This directive allows you to include files when the JSP is compiled into a servlet. This instruction is like this:
<%@ include file= "relative URL"%>
This specified URL is usually a related explanation of the JSP page that points to it. The contained file contents are parsed as JSP text, so you can include static HTML, scripting elements, directives, and actions.
For example, many sites contain small navigation bars on every page. This include is a good way to do this so that developers often copy HTML to different files. For example:
! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"
Servlet tutorial:javaserver Pages (JSP) 1.0
<%@ include file= "/navbar.html"%>
!--part specific to this page ...-->
Now that the file is inserted when the page is compiled, if the navigation bar changes, you need to recompile all the JSP pages it points to. Note that the problem is easy to come up here. Some readers find the navigation bar unchanged when they rerun the JSP file that contains the navigation bar after changing the navigation bar. There are two reasons for this: first, the include directive mentioned above contains the navigation bar file when the JSP is compiled into a servlet, and the second is that the JSP file is compiled into a servlet and then run, if the server finds that the JSP file has not been modified, To invoke the compiled servlet directly. As a result, when the JSP file is invoked, the results of the display are, of course, the previous navigation bar because the compiled servlet is called directly. As long as you modify the JSP file slightly, the problem can be solved by itself.
If the navigation bar is not constantly changing and wants the whole process to be as efficient as possible, it is a good compromise in such an environment. If the included file changes frequently, it is recommended that the reader use the Jsp:include behavior (action) instead.