Servlet scope object

Table of contents

1. Three major domain objects of Servlet

        1、Request(HttpServletRequest):

        life cycle:

        Commonly used methods:

        2、Session(HttpSession):

        life cycle:

        Commonly used methods:

        3、Applicable(ServletContext):

        life cycle:

        Commonly used methods:

2. Common methods of three scopes


1. Three major domain objects of Servlet

object name object type
request ( valid within the scope of a server request )
HttpServletRequest

 session ( content is valid within a session scope )

HttpSession
applicable ( valid within an application server )
ServletContext

1、Request(HttpServletRequest):

life cycle:

  • Creation : The client sends a request to the server, and the server creates the request object.
  • Destroy : The request object will be destroyed after the server responds to this request.
  • Valid : Only valid in the current request . If web components need to share data in the same request , only request forwarding can be used .

     Commonly used methods:

1.   request.getAttribute(String name);//获取名字为name的属性值
2.   request.setAttribute(String name,Object object);//在请求中保存名称为name的属性
3.   request.removeAttribute(String name);//清除请求中名字为name的属性
4.   request.getParameter(String name);//返回指定请求参数的值

2、Session(HttpSession):

life cycle:

Creation : The server calls getSession() for the first time; (saved in server memory)

Destroy :

  • Abnormal shutdown of the server (normal shutdown of the session will be serialized, and restarting the server session will be deserialized);
  • Session expiration defaults to 30 minutes
  • Manually call session.invalidate();

Note : If you close the browser and visit again, you will find the wrong session ID instead of the session being destroyed.

Valid : A browser session begins when the user opens it and does not end until the user closes the browser session. Only one session object is created during a session.

     Commonly used methods:

1.   String imageMsg = (String) request.getSession().getAttribute(“imageMsg”);//图片的验证码      
     request.getSession().setAttribute(“cart”, cart);//将cart放入session中
                                            //(场景:购物车物品保存、保存用户登录状态)
2.   String getid();//获取sessionid
3.   void invalidate();//设置session对象失效
4.   Object getAttribute(String key);//通过key获取对象值
5.   void removeAttribute(String key); //从session中删除指定名称(key)所对应的对象
6.   void setMaxInactiveInterval(int interval);//设定session的非活动时间
7.   int getMaxInactiveInterval(); //获取session的有效非活动时间(以秒为单位)

3、Applicable(ServletContext):

life cycle:

Creation : When the server starts, the server creates an object ServletContext class belonging to the Web project for each Web application.

Destroyed : when the server is shut down or the project is removed from the server

Valid : This information is retained across the server

     Commonly used methods:

1.   void setAttribute(String key,Object value);//以key/value的形式保存对象值
2.   Object getAttribute(String key);//通过key获取对象值
3.   String getRealPath(String path);//返回相对路径的真实路径


例.统计网站访问次数的实现(代码如下):
2-1统计页:
<%
   integer count = (Integer)application.getAttribute("count");
   if(count!=null){
         count = 1+count;
    }else{
         count = 1;
    }
    application.setAttribute("count",count);
%>
2-2显示页:
<%
    Integer i = (Integer)application.getAttribute("count");
    out.println("您好,您是第"+i+"位访问本网站的用户");
%>

2. Common methods of three scopes

  • Store data : setAttribute(name,value);
  • Get data : getAttribute(name);
  • Delete data : removeAttribute(name);

Guess you like

Origin blog.csdn.net/m0_70314224/article/details/125567015