Servlet implementation Session
(1) first, let's take a look at the project structure.
Is the myWebSites project under tomcat -- webaps
Under myWebSites there is only a WEB-INF folder that has a directory in the WEB-INF folder (under the classes folder class)
The following three classes are required for the project structure:
(2) Login. java code
// Log on to the package com. tsinghua; import javax. servlet. http. *; import java. io. *; public class Login extends HttpServlet {public void doGet (HttpServletRequest req, HttpServletResponse res) {// business logic try {// solves Chinese garbled res. setContentType (text/html; charset = gbk); PrintWriter pw = res. getWriter (); // return to the logon interface pw. println (
); Pw. println (); pw. println (User Logon); pw. println (); pw. println (); pw. println ();} catch (Exception ex) {ex. printStackTrace () ;}} public void doPost (HttpServletRequest req, HttpServletResponse res) {this. doGet (req, res );}}
(3) Logincl. java code
// Log on to the package com. tsinghua; import javax. servlet. http. *; import java. io. *; public class Logincl extends HttpServlet {public void doGet (HttpServletRequest req, HttpServletResponse res) {// business logic try {// solves Chinese garbled characters // res. setContentType (text/html; charset = gbk); // PrintWriter pw = res. getWriter (); // accept the username and password String u = req. getParameter (username); String p = req. getParameter (passwd); // verify if (u. equals (sp) & p. equals (123) {// set Successfully verified information, written to the session. // 1. obtain sessionHttpSession hs = req. getSession (true); // modify the existence time of the session hs. setMaxInactiveInterval (20000); // hs in seconds. setAttribute (pass, OK); // set to go to the session. // if the session is valid, the page jumps to Welcomeres. sendRedirect (welcome? Uname = + u + & upass = + p); // The servlet url} else {// illegal // redirect to res. sendRedirect (login); // jump to the servlet url} catch (Exception ex) {ex. printStackTrace () ;}} public void doPost (HttpServletRequest req, HttpServletResponse res) {this. doGet (req, res );}}
(4) Welcome. java code
// Log on to the package com. tsinghua; import javax. servlet. http. *; import java. io. *; public class Welcome extends HttpServlet {public void doGet (HttpServletRequest req, HttpServletResponse res) {// get sessionHttpSession hs = req. getSession (true); String val = (String) hs. getAttribute (pass); // judge if (val = null) {try {// illegal login to res. sendRedirect (login);} catch (Exception ex) {ex. printStackTrace () ;}// obtain the username String u = req passed by Logincl. g EtParameter (uname); // obtain the password String p = req. getParameter (upass); String sex = req. getParameter (sex); println (sex); // business logic try {// solve Chinese garbled res. setContentType (text/html; charset = gbk); PrintWriter pw = res. getWriter (); pw. println (hello welcome! + U + p);} catch (Exception ex) {ex. printStackTrace () ;}} public void doPost (HttpServletRequest req, HttpServletResponse res) {this. doGet (req, res );}}