JSP login program code
<! -- Login Manager --> <Java type = "class">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; } } </JAVA> <% // 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 + "| <BR> "); 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 not get the Username or Password right "; } } Else { ErrorMessage = "haven'ttried logging in yet ."; If (mySession. getValue ("requestedUrl") = null) { MySession. putValue ("requestedUrl", "/index. jsp "); } // Out. println ("set userReferrer to" + mySession. getValue ("redirectTo") + "<BR> "); } %> <CENTER> <Font color = red> <% = errorMessage %> </font> <TABLE align = center> <FORM action = "adminLogin2.jsp" method = post name = "login"> <TR> <TD> Username: </TD> <TD> <input type = text name = user value = ""> </TD> </TR> <TR> <TD> Password: </TD> <TD> <input type = password name = pass value = ""> </TD> </TR> <TR> <TD colspan = 2 align = center> <input type = submit name = click value = "Log In"> </TD> </TR> </FORM> </TABLE> </CENTER> Original URL: <% = (String) mySession. getValue ("requestedUrl") %> <Script language = "Javascript"> // <! -- If (document. forms. login! = Null) Document. forms. login. user. focus (); // --> </Script> |
5.11 how to use checkbox in jsp
<% @ Page language = "Java" contentType = "text/html" %> <% @ Page import = "com. ora. jsp. util. *" %> <Html> <Body bgcolor = "white"> <Form action = "checkbox. jsp"> <Input type = "checkbox" name = "fruits" value = "Apple"> Apple <br> <Input type = "checkbox" name = "fruits" value = "Banana"> Banana <br> <Input type = "checkbox" name = "fruits" value = "Orange"> Orange <br> <Input type = "submit" value = "Enter"> </Form> <% String [] picked = request. getParameterValues ("fruits "); If (picked! = Null & picked. length! = 0 ){ %> You picked the following fruits: <Form> <Input type = "checkbox" name = "fruits" value = "Apple" <% = ArraySupport. contains (picked, "Apple ")? "Checked": "" %> Apple <br> <Input type = "checkbox" name = "fruits" value = "Banana" <% = ArraySupport. contains (picked, "Banana ")? "Checked": "" %> Banana <br> <Input type = "checkbox" name = "fruits" value = "Orange" <% = ArraySupport. contains (picked, "Orange ")? "Checked": "" %> Orange <br> </Form> <% }%> </Body> </Html> |
5.12 request object
· How to obtain the absolute URL of a JSP/Servlet File at runtime
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
String callingPage = request. getHeader ("Referer "); Out. print (callingPage ); |
· If several submit buttons appear in the form, JSP/Servlet can define how to determine which button is submitted in form as follows:
<Input type = submit name = "name" value = "john"> <br> <Input type = submit name = "name" value = "joe"> <br> Use request. getParameter ("name") in JSP/Servlet to determine based on the returned value. |
5.13 include command
This command allows you to include some files when JSP is compiled into a servlet. This command is as follows:
<% @ Include file = "relative url" %> |
The specified URL usually points to its JSP page. The content of the contained file is analyzed as JSP text, so it can contain static HTML, scripting elements, directives, and actions.
For example, each page of many sites contains a small navigation bar. This include is a good way to do this, saving developers from copying HTML to different files. For example:
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN"> <HTML> <HEAD> <TITLE> Servlet Tutorial: assumerver Pages (JSP) 1.0 </TITLE> <Meta name = "author" CONTENT = "webmaster@somesite.com"> <Meta name = "keywords" CONTENT = "..."> <Meta name = "description" CONTENT = "..."> <Link rel = stylesheet href = "Site-Styles.css" TYPE = "text/css"> </HEAD> <BODY> <% @ Include file = "/navbar.html" %> <! -- Part specific to this page... --> </BODY> </HTML> |
Since the file is inserted when the page is compiled, if the navigation bar changes, you need to re-compile all the JSP pages it points. Note that the problem may easily occur here. When some readers re-run the JSP file containing the navigation bar after changing the navigation bar, they find that the navigation bar remains unchanged. There are two reasons: first, the include command mentioned above is included in the navigation bar file when JSP is compiled into servlet; second, the JSP file is compiled into servlet and then run, if the server finds that the JSP file is not modified, call the compiled servlet directly. In this way, when calling the JSP file, because the compiled servlet is directly called, The displayed result is of course the previous navigation bar. You only need to modify the JSP file a little, and the problem can be solved by yourself.
If the navigation bar does not change frequently and you want to make the whole process as efficient as possible, this is a good compromise in such an environment. If the extended ded file changes frequently, we recommend that you use jsp: include action instead.