Servlet implementation Session

Source: Internet
Author: User

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


 

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.