Servlet implements Session and servletsession
(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 ("
(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 ") {// write the information successfully verified 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"); // you can set it to the session to redirect to Welcomeres. sendRedirect ("welcome? Uname = "+ u +" & upass = "+ p); // the url of the servlet is still redirected here.} else {// The url is invalid. // the url of the servlet is redirected 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 = r passed by Logincl. Eq. getParameter ("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 );}}
Http://download.csdn.net/detail/u010870518/7843661 that requires project code
How to Use the session in servlet, the focus is to pass values to the session
Session is based on Cookie technology. sessions are stored on the server (generally set the validity period), and SessionID is saved on the client. Session !! Several !!! "Key-value" pair, which can be modified/added through void setAttribute (java. lang. String name, java. lang. Object value)
HttpSession session = request. getSession ();
Session. setAttribute ("abc", new Integer (567); // new property key: abc value: new Integer (567)
Session. setAttribute ("jkl", new Person (); // Add property key: jkl value: new Person ()
Session. setAttribute ("abc", new Double (5.67); // The abc key already exists and the corresponding value of the key will be modified.
Session is essentially a set of "key-value" pairs in which "key" is stored on the client (key). The "value" in the string is saved on the server (safe deposit box) and can be of the Object type.
Relationship between sessionID and session: Key and bank safe deposit box key on client safe deposit box on server
URL rewriting: solves the problem of disabling browser cookies.
!!!!!!!!!!!!! The Session becomes invalid when the browser is closed. The same Session cannot be obtained during the next visit.
Instance:
Package chap03;
Import java. io .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Public class GetSession extends HttpServlet
{
Public void doGet (HttpServletRequest request,
HttpServletResponse response)
Throws ServletException, IOException
{
Response. setContentType ("text/html; charset = GBK ");
PrintWriter out = response. getWriter ();
String user = "";
// No session is created here, but the created session is retrieved.
HttpSession session = request. getSession (false );
// If the session can be obtained, the user has logged on
If (session! = Null)
{
User = (String) session. getAttribute ("abc ");
Out. println ("Get created Session ");
Out. println ("<BR> ");
Out. println ("Login Name:" + user );
}
// Otherwise, it indicates that the user has not logged on. The logon page is displayed to allow the user to log on.
Els ...... remaining full text>
How to obtain session configuration information in servlet
HttpSession session = request. getSession ();
Session. getAttribute ("name"); ------ get the information put into the session.
Session. getAttributeNames () ----- get all the information names in the session.
Session. getMaxInactiveInterval () ---- get the maximum session survival time.
Seesion. getId (); ------ get the session id. Useful properties for sharing sessions.
Session. getCreationTime () ----- session creation time.
...........
You can obtain the session configuration information for more attributes. You can also set the session attributes in the web. xml file through <session-config>.