JSP的login程序代码
<!-- 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文件 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't tried 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 checkbox在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对象
·如何获得一个运行时刻的JSP/Servlet文件的绝对URL地址
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()); |
·如何知道客户端通过哪个URL访问本页面
String callingPage = request.getHeader("Referer"); Out.print(callingPage); |
·如果form中出现好几个submit按钮,JSP/Servlet如何判断哪个按钮进行了提交在form中可以这样定义:
<input type=submit name="name" value="john"><br> <input type=submit name="name" value="joe"><br> 在JSP/Servlet中使用request.getParameter("name"),根据返回值就可进行判断。 |
5.13 include指令
这个指令让你可以在JSP编译成servlet的时候可以包含进一些文件。这个指令是这样的:
<%@ include file="relative url" %> |
这个指定的URL通常是指向它的JSP页面的相关解释。包含的文件内容被当作JSP文本来分析,因此可以包含静态 HTML、scripting elements、directives以及actions。
例如,很多站点的每个页面上都包含有小的导航条。这个 include 是做这个的很好方法,省得开发者经常拷贝HTML到不同的文件中。例如:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Servlet Tutorial: JavaServer 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> |
既然文件是在页面被编译的时候插入的,如果导航条改变了,你需要去重新编译它所指向的所有JSP页面。注意,问题很容易在这里出现。一些读者在改变导航条后,重新运行包含导航条的JSP文件时,发现导航条不变。原因有两个:一是前面提到的include指令在JSP编译成servlet的时候包含进导航条文件;第二就是JSP文件是被编译成servlet之后再运行的,如果服务器发现JSP文件没有被修改,则直接去调用已经编译好的servlet。如此一来,当调用JSP文件时,由于直接调用了编译好的servlet,显示的结果当然就是以前的导航条了。只要稍微修改一下JSP文件,该问题就可以自行解决。
如果导航条不是经常改变的,而且想要整个过程尽可能高效,那么在这样的环境下这是好的妥协。 如果这个 included 文件经常改变,那么建议读者使用用 jsp:include 行为(action)来代替。