Servlet&JSP的那些事兒(八)

來源:互聯網
上載者:User

Servlet&JSP的那些事兒(七)中我們介紹了會話管理,但是沒有給出一個具體的執行個體。俗話說,耳聽為虛眼見為實。所以,本篇講述一個會話管理的執行個體。首先,建立一個web工程,LoginTest。建立的過程和之前MyDemo相同。不再贅述~LoginTest完成的主要功能為:開始時,這個程式採用基於cookie的會話更總,當用戶端禁用cookie之後,採用URL重寫的機制來進行會話跟蹤。

首先編寫一個工具類OutputSessionInfo。它有一個靜態方法,該方法以表格形式輸出session相關資訊,方便我們隨時查看。具體代碼如下:

package com.shan.util;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import java.util.*;public class OutputSessionInfo{public static void printSessionInfo (PrintWriter out, HttpSession session) {out.println("<table>");out.println("<tr>");out.println("<td>工作階段狀態:</td>");if(session.isNew()) {out.println("<td>新的會話</td>");} else {out.println("<td>舊的會話</td>");}out.println("</tr>");out.println("<tr>");out.println("<td>會話ID:</td>");out.println("<td>"+session.getId()+"</td>");out.println("</tr>");out.println("<tr>");out.println("<td>會話ID:</td>");out.println("<td>"+session.getId()+"</td>");out.println("</tr>");out.println("<tr>");out.println("<td>建立時間:</td>");out.println("<td>"+new Date(session.getCreationTime())+"</td>");out.println("</tr>");out.println("<tr>");out.println("<td>上次訪問時間:</td>");out.println("<td>"+new Date(session.getLastAccessedTime())+"</td>");out.println("</tr>");out.println("<tr>");out.println("<td>最大不啟用時間間隔(s):</td>");out.println("<td>"+session.getMaxInactiveInterval()+"</td>");out.println("</tr>");out.println("</table>");}}

然後,編寫LoginServlet類。代碼如下:

package com.shan.web;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import com.shan.util.OutputSessionInfo;public class LoginServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {response.setContentType("text/html;charset=UTF-8");         PrintWriter out=response.getWriter();HttpSession session = request.getSession();String user = (String)session.getAttribute("user");out.println("<html>");//告訴瀏覽器不要緩衝這個頁面out.println("<meta http-equiv=\"Pragma\" content=\"no-cache\">");out.println("<head><title>登入頁面</title></head>");out.println("<body>");OutputSessionInfo.printSessionInfo(out,session);out.println("<form method=post action=loginchk.do>");out.println("<table>");out.println("<tr>");out.println("<td>請輸入使用者名稱:</td>");if(user == null) {out.println("<td><input type=text name=user></td>");} else {out.println("<td><input type=text name=user value="+user+"></td>");}out.println("</tr>");out.println("<tr>");out.println("<td>請輸入密碼:</td>");out.println("<td><input type=password name=passwd></td>");out.println("</tr>");out.println("<tr>");out.println("<td><input type=reset value=重填></td>");out.println("<td><input type=submit value=登入></td>");out.println("</tr>");out.println("</table>");out.println("</form>");out.println("</body>");out.println("</html>");}public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {doGet(request,response);}}

編寫LoginchkServlet類,代碼如下:

package com.shan.web;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class LoginchkServlet extends HttpServlet{public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {request.setCharacterEncoding("UTF-8");response.setContentType("text/html; charset=UTF-8");String username = request.getParameter("user");String passwd = request.getParameter("passwd");if(username == null || passwd == null || username.equals("") || passwd.equals("")) {response.sendRedirect("login.do");} else {HttpSession session = request.getSession();session.setAttribute("user",username);response.sendRedirect("welcome.do");}}public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {doGet(request,response);}}

編寫WeclomeServlet類,代碼如下:

package com.shan.web;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import com.shan.util.OutputSessionInfo;public class WelcomeServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {response.setContentType("text/html;charset=UTF-8");PrintWriter out=response.getWriter();HttpSession session = request.getSession();String user = (String)session.getAttribute("user");if(user == null) {response.sendRedirect("/login.do");} else {String username=request.getParameter("username");String welcomeInfo="Hello, "+ username;out.println("<html><head><title>");out.println("歡迎頁面");out.println("</title></head>");out.println("<body>");OutputSessionInfo.printSessionInfo(out,session);out.println("歡迎你,"+user);out.println("<a href=login.do>重新登入</a>");out.println("<a href=logout.do>登出登入</a>");out.println("</body></html>");out.close();}}public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {doGet(request,response);}}

編寫LogoutServlet類,代碼如下:

package com.shan.web;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class LogoutServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {response.setContentType("text/html;charset=UTF-8");         PrintWriter out=response.getWriter();HttpSession session = request.getSession();session.invalidate();out.println("<html><head><title>");out.println("退出登入");out.println("</title></head>");out.println("<body>");out.println("已退出登入");out.println("<a href=login.do>重新登入</a>");out.println("</body></html>");out.close();}public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {doGet(request,response);}}

最後,配置web.xml檔案。如下:

<?xml version='1.0' encoding='utf-8'?><web-app xmlns="http://java.sun.com/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  version="3.0"  metadata-complete="true"><servlet><servlet-name>LoginServlet</servlet-name><servlet-class>com.shan.web.LoginServlet</servlet-class></servlet><servlet><servlet-name>LoginchkServlet</servlet-name><servlet-class>com.shan.web.LoginchkServlet</servlet-class></servlet><servlet><servlet-name>WelcomeServlet</servlet-name><servlet-class>com.shan.web.WelcomeServlet</servlet-class></servlet><servlet><servlet-name>LogoutServlet</servlet-name><servlet-class>com.shan.web.LogoutServlet</servlet-class></servlet><servlet-mapping><servlet-name>LoginServlet</servlet-name><url-pattern>/login.do</url-pattern></servlet-mapping><servlet-mapping><servlet-name>LoginchkServlet</servlet-name><url-pattern>/loginchk.do</url-pattern></servlet-mapping><servlet-mapping><servlet-name>WelcomeServlet</servlet-name><url-pattern>/welcome.do</url-pattern></servlet-mapping><servlet-mapping><servlet-name>LogoutServlet</servlet-name><url-pattern>/logout.do</url-pattern></servlet-mapping><session-config><session-timeout>5</session-timeout></session-config></web-app>

完成後,還需要編譯java檔案。Dos環境下切換到工程所在目錄,執行如下語句:

javac -classpath D:\apache-tomcat-7.0.33\lib\servlet-api.jar;classes -d classes src\com\shan\util\OutputSessionInfo.java
javac -classpath D:\apache-tomcat-7.0.33\lib\servlet-api.jar;classes -d classes src\\com\shan\web\LoginServlet.java
javac -classpath D:\apache-tomcat-7.0.33\lib\servlet-api.jar;classes -d classes src\\com\shan\web\LoginchkServlet.java
javac -classpath D:\apache-tomcat-7.0.33\lib\servlet-api.jar;classes -d classes src\\com\shan\web\WelcomeServlet.java
javac -classpath D:\apache-tomcat-7.0.33\lib\servlet-api.jar;classes -d classes src\\com\shan\web\LogoutServlet.java

最後,將編譯好的檔案拷貝到D:\apache-tomcat-7.0.33\webapps\LoginTest\WEB-INF\classes檔案夾下,並把web.xml拷貝到D:\apache-tomcat-7.0.33\webapps\LoginTest\WEB-INF檔案夾下。在Dos環境中執行startup,啟動tomcat。運行結果1所示。

圖1 執行個體的運行結果

可以禁用cookie,在IE或者chrome的設定中選擇禁用cookie。然後就可以看到基於cookie的會話跟蹤機制就失效了。這時,可以採用Servlet&JSP的那些事兒(七)所述的辦法來解決上述問題。在此就不再細述,有興趣可以自己添加,看看效果。

轉載請註明出處:http://blog.csdn.net/iAm333

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.