Servlet & JSP (8)

Source: Internet
Author: User

Session management is introduced in servlet & JSP (7), but no specific instance is provided. As the saying goes, listening is real. Therefore, this article describes an instance of session management. First, create a web project, logintest. The creation process is the same as that of mydemo. I will not go into details ~ The main function of logintest is: at the beginning, this program uses cookie-based sessions more frequently. When the client disables cookies, the URL rewriting mechanism is used for session tracking.

First, compile an outputsessioninfo tool class. It has a static method that outputs session-related information in the form of tables, so that we can view the information at any time. The Code is as follows:

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> session Status: </TD>"); If (Session. isnew () {out. println ("<TD> new session </TD>");} else {out. println ("<TD> old session </TD>");} Out. println ("</tr>"); out. println ("<tr>"); out. println ("<TD> session ID: </TD>"); out. println ("<TD>" + session. GETID () + "</TD>"); out. println ("</tr>"); out. println ("<tr>"); out. println ("<TD> session ID: </TD>"); out. println ("<TD>" + session. GETID () + "</TD>"); out. println ("</tr>"); out. println ("<tr>"); out. println ("<TD> Creation Time: </TD>"); out. println ("<TD>" + new date (Session. getcreationtime () + "</TD>"); out. println ("</tr>"); out. println ("<tr>"); out. println ("<TD> last access time: </TD>"); out. println ("<TD>" + new date (Session. getlastaccessedtime () + "</TD>"); out. println ("</tr>"); out. println ("<tr>"); out. println ("<TD> maximum inactivity interval (s): </TD>"); out. println ("<TD>" + session. getmaxinactiveinterval () + "</TD>"); out. println ("</tr>"); out. println ("</table> ");}}

Then, write the loginservlet class. The Code is as follows:

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>"); // tell the browser not to cache this page out. println ("<meta http-equiv = \" Pragma \ "content = \" no-Cache \ ">"); out. println ("

Compile the loginchkservlet class with the following code:

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);}}

Compile the weclomeservlet class with the following code:

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> 

Compile the logoutservlet class with the following code:

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> 

Finally, configure the Web. xml file. As follows:

<?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>

After compilation, you also need to compile the Java file. Switch to the project directory in the DOS environment and execute the following statement:

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

Finally, copy the compiled file to the D: \ apache-Tomcat-7.0.33 \ webapps \ logintest \ WEB-INF \ Classes folder, and put the web. copy XML to the D: \ apache-Tomcat-7.0.33 \ webapps \ logintest \ WEB-INF folder. Execute startup in the DOS environment to start Tomcat. Run result 1.

Figure 1 instance running result

You can disable cookies. in IE or chrome settings, select disable cookies. Then we can see that the cookie-based session tracing mechanism is invalid. In this case, you can use the methods described in servlet & JSP (7) to solve the above problems. I will not elaborate on it here. If you are interested, you can add it yourself to see the effect.

Reprinted please indicate the source: http://blog.csdn.net/iAm333

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.