15 session session

1.1. Session Technology

1.1.1. Session Overview

Session is a server-side technology, the use of this technology, the server can be created at run time for each user's browser session object of its exclusive, due to the user's browser session is exclusive, so when users access the web server resources, It can be placed in each respective data session when the user access other web resources to go to the server, and then remove the other web resources, user services data from the respective user session.

  In the browser save the data, the data can be viewed, access, data security is low. Important data should be stored in a location not easily acquired. Server is a good choice. Sharing data through the server, this technology is the session session technique.            

       2. Session Works

              Each browser accessing the server, each corresponding session object is created to hold the data. Do not affect each other, because the body contains a session object sessionid, you can distinguish each browser through the corresponding session id.

              . A session works:

                     The nature of the work is carried out through a name for JSESSIONID the cookie. The cookie will be stored in your browser to use.

              . B session features:

                     session server is a technique, the data stored in the server. Save a higher data security requirements. Shorter and stores data.

              c. Create a session object

              request.getSession (); // if the session object server, then remove the use, if there is no session object is created using a new session object.

              request.getSession (true); // if the session object server, then remove the use, if there is no session object is created using a new session object.

request.getSession (false); // if no session server object, the return null, if it wants to remove the used session. (When present is determined whether the session)

1.1.2. Session is a domain object

The life cycle:

 

 

When the program first call to request.getSession () code to the server until a clear need to use the session, this time to create a session.

                     . I accidental death: In the case of server unexpectedly closed, session object is destroyed. In the case of a normal shutdown the server, if the session object is not released, the sequence in which the contents to disk, this process, known as passivation. When the server is started again, re-read the files on the disk, a process known as activation.

                     ii suicide: active session.invalidate () method immediately release the current session object.

                     iii timeout Death: long Configuration <session-config> tag default maximum life session, the default value of 30 minutes [tomcat] /conf/web.xml in. More than 30 minutes, the current session will be released.

Range:

The entire range of the visible session

main role:

Shared data in the session scope

 

Code:

  i. SessionDemo1.java
package cn.tedu.session;
                
                import java.io.IOException;
                
                import javax.servlet.ServletException;
                import javax.servlet.http.HttpServlet;
                import javax.servlet.http.HttpServletRequest;
                import javax.servlet.http.HttpServletResponse;
                import javax.servlet.http.HttpSession;
                //session共享数据--作为域对象使用
                public class SessionDemo1 extends HttpServlet {
                
                        public void doGet(HttpServletRequest request, HttpServletResponse response)
                                throwsServletException, IOException {
                                 // 1. Get session object 
                                the HttpSession session = Request.getSession ();
                                 // 2. Use as domain objects - Set domain attributes 
                                session.setAttribute ( "name", "Yang Cao" ); 
                        } 
                
                        public  void the doPost (the HttpServletRequest Request, the HttpServletResponse Response)
                                 throws ServletException, IOException { 
                                the doGet (Request, Response); 
                
                        } 
                
                }

SessionDemo2.java

                package cn.tedu.session;
                
                import java.io.IOException;
                
                import javax.servlet.ServletException;
                import javax.servlet.http.HttpServlet;
                import javax.servlet.http.HttpServletRequest;
                import javax.servlet.http.HttpServletResponse;
                import javax.servlet.http.HttpSession;
                
                public class SessionDemo2 extends HttpServlet {
                
                        public void doGet(HttpServletRequest request, HttpServletResponse response)
                                throws ServletException, IOException {
                                //1.获取session对象
                                HttpSession session = request.getSession(true);
                                //2.获取域属性
                                String name = (String) session.getAttribute("name");
                                System.out.println("name:"+name);
                        }
                
                        public void doPost(HttpServletRequest request, HttpServletResponse response)
                                throws ServletException, IOException {
                                doGet(request, response);
                
                        }
                
                }

       4. Case: Shopping Cart

              Two Servlet: buyServlet / PayServlet

              A jsp: sale.jsp --- selection of merchandise Click Name Add to Cart. Click the button for the commodity payment checkout.

index.jsp page click on the label which is equivalent to adding a shopping cart of goods

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" session="false"%>
<!DOCTYPE HTML >
<html>
  <head>
   
  </head>
  
  <body>
    <a href="http://localhost/day12-cookiesession/servlet/BuyServlet?prod=鼠标">鼠标</a><br>
    <a href="http://localhost/day12-cookiesession/servlet/BuyServlet?prod=键盘">键盘</a><br>
    <a href="http://localhost/day12-cookiesession/servlet/BuyServlet?prod=月饼">月饼</a><br>
    <a href="http://localhost/day12-cookiesession/servlet/BuyServlet?prod=手机">手机</a><br>
    <a href="http://localhost/day12-cookiesession/servlet/BuyServlet?prod=拖鞋">A</Slippers><br>
    <a href="http://localhost/day12-cookiesession/servlet/PayServlet">付款</a><br>
  </body>
</html>

 

BuyServlet

                package cn.tedu.session;
                
                import java.io.IOException;
                
                import javax.servlet.ServletException;
                import javax.servlet.http.Cookie;
                import javax.servlet.http.HttpServlet;
                import javax.servlet.http.HttpServletRequest;
                import javax.servlet.http.HttpServletResponse;
                import javax.servlet.http.HttpSession;
                //购物车Servlet
                public class BuyServlet extends HttpServlet {
                
                        public voidthe doGet (the HttpServletRequest Request, the HttpServletResponse Response)
                                 throws ServletException, IOException {
                                 // 1. Request distortion 
                                String request.getParameter Prod = ( "Prod" ); 
                                Prod = new new String (prod.getBytes ( "ISO8859-1"), "UTF- 8 " );
                                 // 2. response distortion processing 
                                the response.setContentType (" text / HTML; charset = UTF-8 " ); 
                                
                                // put items in cart
                                 @ 1 acquires the session object 
                                HttpSession session =request.getSession ();
                                 // close the browser before and after every operation with a session
                                 // --- reserve a name in the browser to JSESSIONID of cookie, cookie values are stored in the session id, so close the browser is also available again to the original session object 
                                cookies Cookie = new new cookies ( "the JSESSIONID" , session.getId ()); 
                                cookie.setMaxAge ( 60 * 60 * 24 ); 
                                cookie.setPath (request.getContextPath () + "/" ); 
                                Response. addCookie (the cookie); 
                                
                                // 2. Add a domain attribute to the session in 
                                session.setAttribute ( "prod", Prod);
                                 // 3. Tip in the browser, commodity Add to Cart 
                                response.getWriter () write ( "commodity [" + prod + "] has been added to cart." ); 
                        } 
                
                        Public  void doPost (Request the HttpServletRequest, HttpServletResponse Response)
                                 throws ServletException, IOException { 
                                the doGet (Request, Response); 
                
                        } 
                
                }

payservlet.java

package cn.tedu.session;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//支付Servlet
public class PayServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        //从session中取出商品
        //1.响应乱码处理
        response.setContentType ( "text / HTML; charset = UTF-8" );
         // 2. determine whether there is session - click on any item will not pay 
        IF (request.getSession ( false !) = null ) {
             // 3. If present, remove the domain properties 
            String Prod = (String) request.getSession () getAttribute ( "Prod." );
             // release session, empty shopping cart. 
            request.getSession ( false ) .invalidate ();
             // page has been prompt payment for the merchandise 
            . response.getWriter () write ( "Do you think commodity [" + prod + "] payment 10000 ¥" ); 
        } the else {
             // 4. If the user is prompted to select the product does not exist yet
            response.getWriter().write("您尚未选择任何商品");
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        doGet(request, response);

    }

}

 

Guess you like

Origin www.cnblogs.com/xuwangqi/p/11354714.html