zoukankan      html  css  js  c++  java
  • JAVA WEB:application与session对象使用范例与差异深度分析

    1.本文分析一下request对象中getParameter方法及getAttribute的差异及使用场景。

    这个关系到servlet的生命周期

    很简单的一个例子,上代码

    页面1 :request1.jsp

     1 <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 <html>
     7 <head>
     8 
     9 <title>1</title>
    10 </head>
    11 <body>
    12         <form    action="request2.jsp">
    13                     
    14             username:<input type="text" name="username">
    15 
    16             <input type="submit" value="submit">                    
    17             </form>
    18 </body>
    19 </html>

    这个JSP 从text框获取一个文本值(用户名)

    然后在request2.jsp中调用getParameter方法获取改对象

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>2</title>
     8 </head>
     9 <body>
    10 
    11 <% 
    12     String username = request.getParameter("username");
    13 
    14 %>
    15 
    16         username: <%=username %>
    17         
    18         <% request.setAttribute("username", username);%>
    19         
    20         
    21         <jsp:forward="request3.jsp"></jsp:forward>
    22 </body>
    23 </html>

    显示成功。

    我们在request2.jsp中将username再set到request中传递给第三个页面request3.jsp

     1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
     2     pageEncoding="ISO-8859-1"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     7 <title>3</title>
     8 </head>
     9 <body>
    10     <% String username = (String)request.getAttribute("username");%>
    11     
    12     username:<%=username %>
    13 </body>
    14 </html>

    然后在request3.JSP中用getAtterbute方法获取该对象

    但是二者有什么区别呢,我们将request2.jsp修改一下即可看出差别

    修改后的request2.JSP

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>2</title>
     8 </head>
     9 <body>
    10 
    11 <% 
    12     String username = request.getParameter("username");
    13 
    14 %>
    15 
    16         username: <%=username %>
    17         
    18         <% request.setAttribute("username", username);%>
    19         
    20         
    21         <a href="request3.jsp">request3.jsp</a>
    22 </body>
    23 </html>

    我们将request2.jsp中的页面跳转转为超链接

    username:null
    

      页面只会这么显示。

    因为改为超链接后,在游览器点击该链接,游览器会发送第二个请求,那么request的生命将终止,而发起的第二个request的session会话中是null的不存在一个叫username-String的KEY-VALUE对的。

    总结:

    1.request的setAtterbute与getAtterbute方法一般都是成对出现的,首先通过setAtterbute方法设置属性与属性值,

    然后getAtterbute根据属性获取到该值(一般都需要进行向下的类型转换,转换为确切的类型对象进行使用)。setAtterbute与getAtterbute方法都是在服务器内部执行的,

    客户端不可见。

    2.request的getParameter方法的作用是获取到客户端通过表单或者URL请求参数发送过来的参数值,是客户端与服务器

    之间的交互,服务器端想要获取到客户端发送过来的数据,就需要使用getParameter方法来获取。

    没有雨getParameter方法对应的setParameter方法

    3.*setAtterbute与getAtterbute是服务器内部对话的方法,而getParameter是游览器和服务器进行外部对话的方法

    二。session与request的区别

    request对象内的数据的存活范围是在request对象的范围内,当客户端向服务器发送一个请求,服务器想客户端返回一个响应后,该

    请求对象就被销毁了,之后再向客户端发送新的请求时,服务器会创建新的request的对象,改对象之前的request和其没有任何关系,因此也无法获得之前的request对象中的任何对象。

    4.session对象的内数据的存活范围也就是session对象的存活范围(通俗的说:只要游览器不关闭,session对象就会一直存),

    因此子啊同一个游览器窗口中,无论想服务器发送多少个请求,session对象只有一个

    5.application(应用对象):存活范围最大的对象,只要服务器没关,application对象中的数据就会一直存在。在整个服务器

    运行当中,只有一个application对象

    6.request、session、以及application这三个对象的范围是逐个增加的

    request只在一个请求范围内;session是在游览器运行窗口的范围捏;application则是在整个服务器运行的过成当中;

  • 相关阅读:
    教你修改Linux下高并发socket最大连接数所受的各种限制
    nginx浏览pdf
    Spring3 M2 quartz-2.1.7 解决bean不能注入问题
    DCSync
    Linux下python2.7安装pip
    ._cache_问题
    php-mvc概念
    php第十天-面向对象命名空间
    php第九天-session/cookice会话控制
    fastadmin V1.0.0.20200506_beta 漏洞复现
  • 原文地址:https://www.cnblogs.com/DLzhang/p/4549742.html
Copyright © 2011-2022 走看看