[知了堂学习笔记]_Java中session的学习

来源:互联网 发布:什么是网络信息收集 编辑:程序博客网 时间:2024/05/21 18:22

 请关注“知了堂学习社区”,地址:http://www.zhiliaotang.com/portal.php 

Servlet的作用域

1.      请求不仅仅是在于客户端和服务器之间,服务器内部也有请求的概念。

2.      JSP的本质是一个Servlet。

3.      Web编程的两大块:

(1)    跳转:url路径。

(2)    跳转过程中的信息传递。(信息共享区域)

4.      用户管理:

request.getAttribute(“list”);

servlet:List<User>-----------JSP

request.setAttribute(“list”,list);

5.      Servlet的作用域:跳转过程中实现信息共享的模型。


HTTP无状态协议

1.      HTTP协议不记录用户的信息。

2.      它会把每一次请求当成一个新的请求。


会话技术

1.      会话:用户打开一个浏览器,点击多个超链接,访问服务器多个Web资源,然后关闭                          

浏览器,整个过程称之为一次会话。

2.      会话技术:保持浏览器和服务器进行会话过程中的一些数据——信息共享区域。(session和cookie)


session的概念

1.      session如何工作:

(1)    客户发起请求,服务器接收请求。

(a)    如果是第一次请求,服务器就会产生一个与客户对应的信息共享区域,保留客户信息。

(b)    如果非第一次请求,服务器就可以找到与该客户相关的信息共享区域,保留客户信息。

(2)    客户再发起请求,服务器就会找到同一个区域。

(3)    多次请求。相同会话,信息共享。

(4)    另一个客户发起请求,针对该客户的一个新的信息共享区域。

2.      session是位于服务器端的信息共享区域,跨多次请求的信息共享。

3.      每一个客户端都有一个对应的session区域。

4.      SessionServlet的代码:

doGet方法的代码:

protected voiddoGet(HttpServletRequest request, HttpServletResponseresponse) throwsServletException, IOException {

       // TODO Auto-generatedmethod stub

       HttpSession session = request.getSession();

       session.setAttribute("info","This issessionTest");

}

5.      index.jsp的代码:

body主体代码:

<body>

<ahref ="SessionServlet">跳转</a>

<%

String info = (String)session.getAttribute("info");

out.print(info);

%>

</body>

6.      result1.jsp的代码:

body主体代码:

<body>

<%

String info = (String)session.getAttribute("info");

out.print(info);

%>

</body>

7.      result2.jsp的代码:

body主体代码:

<body>

<%

String info = (String)session.getAttribute("info");

out.print(info);

%>

</body>


session的API

1.      把数据保存在HttpSession对象中,该对象也是一个域对象。

void setAttribute(String name,Object value);

Object getAttribute(String name);

void remove Attribute(String name);

HttpSession.getId();

2.      setMaxInactiveInterval(intInterval)设置session的存活时间。

3.      invalidate()使此会话无效。

4.      session是有访问时效的,30分钟。

5.      获得session对象:

(1)    getSession()

(2)    getSession(true/false)

注:1.JSP自动创建session对象。

2.Servlet不会自动创建session对象,通过getSession()创建session对象。

6.      isNew/getId()。

7.      时间。

8.      结束会话:

invalidate()。

超时:设置/配置。

应用结束。

doGet方法的代码:

protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

       // TODO Auto-generatedmethod stub

       //1.获得session对象

        //方法一:

        HttpSession session = request.getSession();//创建会话对象,如果没有,创建后返回

        //方法二:

        session =request.getSession(true);//true:getSession()/false:获得当前会话,如果没有,返回null

       if(session.isNew()) {

           System.out.println("新建会话");

       }

       else {

           System.out.println("欢迎回来");

       }

       //2.得到sessionID

       System.out.println(session.getId());

       //3.与会话时间相隔的方法

       System.out.println(session.getCreationTime());//创建会话的时间

    System.out.println(session.getLastAccessedTime());//最后一次得到会话后过去了多少时间

    session.setMaxInactiveInterval(10);//设置session的存活期

       System.out.println(session.getMaxInactiveInterval());//session的存活期

       //4.session的注销

       session.invalidate();

}

9.      session在web.xml中设置会话时间:

<session-config>

<session-timeout></session-timeout>

</session-config>


session的生命周期

1.      创建:当浏览器第一次访问服务器动态资源就创建request.getSession();

2.      服务器:应用运行时,活着。

3.      持久化:存盘。

内存中的一个全景图。

IO实现socializable。

4.      死了:

session.invalidate();强制销毁。

超时:默认30分钟。

setMaxInactiveInterval(int )单位秒

5.      (1).内存益处。

(2). 服务器重启。

钝化-搁置

活化-激活

6.      第一次请求,生成会话ID,通过响应发回给客户端,客户在以后的每一个请求过程携带这个会话ID。


购物车框架搭建

1.      login.jsp的代码:

body主体代码:

<body>

<formaction="LoginServlet"method="post">

           用户名:<inputtype="text"name="username"/><br>

           密码:<inputtype="password"name="pass"/><br>

           <inputtype="submit"value="登录"/>

        </form>

</body>

2.      goods.jsp的代码:

body主体代码:

<body>

        <tableborder="1">

           <tr>

               <td>id</td>

               <td>书名</td>

               <td>价格</td>

               <td>操作</td>

           </tr>

           <tr>

               <td>1</td>

               <td>Thinking in Java</td>

               <td>90</td>

               <td><ahref="GoodsServlet">购买</a></td>

           </tr>

           <tr>

               <td>2</td>

               <td>Java核心技术</td>

               <td>60</td>

               <td><ahref="GoodsServlet">购买</a></td>

           </tr>

        </table>

</body>

3.      shoppingCar.jsp的代码:

body主体代码:

<body>

        <ahref="goods.jsp">返回</a>

        <tableborder="1">

           <tr>

               <td>id</td>

               <td>书名</td>

               <td>价格</td>

               <td>数量</td>

           </tr>

           <tr>

               <td>1</td>

               <td>Thinking in Java</td>

               <td>90</td>

               <td>1</td>

           </tr>

           <tr>

               <td>2</td>

               <td>Java核心技术</td>

               <td>60</td>

               <td>2</td>

           </tr>

           <tr>

               <tdcolspan="4">总价</td>

           </tr>

        </table>

</body>

4.      LoginServlet的代码:

doGet方法和doPost方法的代码:

protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

       // TODO Auto-generatedmethod stub

doPost(request,response);

}

 

    /**

* @seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

     */

protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

       // TODO Auto-generatedmethod stub

       response.sendRedirect("goods.jsp");

}

5.      GoodsServlet的代码:

doGet方法和doPost方法的代码:

protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

       // TODO Auto-generatedmethod stub

       doPost(request,response);

    }

 

    /**

* @see HttpServlet#doPost(HttpServletRequestrequest, HttpServletResponse response)

     */

protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

       // TODO Auto-generatedmethod stub

       response.sendRedirect("shoppingCar.jsp");

}

购物车实现

1.      Goods的代码:

    publicclass Goods {

    privateint id;

    private Stringname;

        privatedouble price;

        publicint getId() {

        returnid;

        }

        publicvoid setId(intid) {

           this.id =id;

        }

        public String getName(){

           returnname;

        }

        publicvoid setName(String name) {

           this.name =name;

        }

        publicdouble getPrice() {

           returnprice;

        }

        publicvoid setPrice(doubleprice) {

           this.price =price;

        }

        public Goods(intid, String name,double price) {

           super();

           this.id =id;

           this.name =name;

           this.price =price;

        }

        public Goods() {

           super();

           // TODO Auto-generated constructor stub

        }

        public Goods(intid) {

           super();

           this.id =id;

    }

}

2.      LoginServlet的代码:

doPost方法的代码:

protectedvoid doPost(HttpServletRequestrequest, HttpServletResponseresponse) throws ServletException, IOException {

        // TODO Auto-generated method stub

       //得到商品列表

       List<Goods> goodsList = new ArrayList<Goods>();

       Goods goods1 =new Goods(1,"Thinkingin Java",90.0);

       Goods goods2 =new Goods(2,"HTML",30);

       Goods goods3 =new Goods(3,"SQL",50);

       Goods goods4 =new Goods(4,"C#",60);

       Goods goods5 =new Goods(5,"JavaScript",70);

      

       goodsList.add(goods1);

       goodsList.add(goods2);

       goodsList.add(goods3);

       goodsList.add(goods4);

       goodsList.add(goods5);

       //goodsList放入信息共享区域:request/session

request.setAttribute("goodsList",goodsList);    request.getRequestDispatcher("goods.jsp").forward(request,response);

}

3.      goods.jsp的代码:

body主体代码:

<body>

        <tableborder="1">

           <tr>

               <td>id</td>

               <td>书名</td>

               <td>价格</td>

               <td>操作</td>

           </tr>

           <%

List<Goods> goodsList = (List<Goods>)request.getAttribute("goodsList");

           for(Goodsgoods:goodsList){%>

           <tr>

               <td><%=goods.getId()%></td>

               <td><%=goods.getName()%></td>

               <td><%=goods.getPrice()%></td>

<td><ahref="GoodsServlet?id=<%=goods.getId()%>">购买</a></td>

           </tr>

           <%}%>

        </table>

</body>

4.      GoodsServlet的代码:

doPost方法的代码:

protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

       // TODO Auto-generatedmethod stub

       //要加入购物车书籍的id传递过来

       //书放进购物车

       //购物车

       HttpSession session = request.getSession();

       //如果是第一次,session区域中是没有购物车存在的

       List<Goods> car = (List<Goods>)session.getAttribute("car");

       if(car==null){

           car =new ArrayList<Goods>();

       }

       //把书放进去

       intid = Integer.parseInt(request.getParameter("id"));

       car.add(new Goods(id));

       //购物车放回session区域

       session.setAttribute("car",car);

      

       response.sendRedirect("shoppingCar.jsp");

}

5.      shoppingCar.jsp的代码:

body主体代码:

<body>

        <ahref="LoginServlet">返回</a>

        <tableborder="1">

           <tr>

               <td>id</td>

               <td>书名</td>

               <td>价格</td>

               <td>数量</td>

           </tr>

           <%

           List<Goods> car= (List<Goods>)session.getAttribute("car");

           for(Goods goods:car){%>

           <tr>

               <td><%=goods.getId()%></td>

               <td><%=goods.getName()%></td>

               <td><%=goods.getPrice()%></td>

               <td>1</td>

           </tr>

           <%}%>

           <tr>

               <tdcolspan="4">总价</td>

           </tr>

        </table>

</body>


















 








阅读全文
0 0
原创粉丝点击