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)來代替。