详解JSP 内置对象request常见用法

request 对象是 HttpServletRequestWrapper 类的实例。它的继承体系如下:

_request 对象继承层次结构图.png

ServletRequest 接口的唯一子接口是 HttpServletRequest ,HttpServletRequest 接口的唯一实现类 HttpServletRequestWrapper ,单从 request 对象一脉单传的类继承体系可以看出,javaweb 标准类库只支持了 http 协议。 Servlet/JSP 中大量使用了接口而不是实现类,这恰恰就是面向接口编程的最佳应用啊。

request 内置对象是由 Tomcat 创建的,可以用来封装 HTTP 请求参数信息、进行属性值的传递以及完成服务端跳转,这就是 request 对象最重要的三个功能了。

request 对象的创建流程

一旦 http 请求报文发送到 Tomcat 中, Tomcat 对数据进行解析,就会立即创建 request 对象,并对参数赋值,然后将其传递给对应的 jsp/servlet 。一旦请求结束,request 对象就会立即被销毁。服务端跳转,因为仍然是同一次请求,所以这些页面会共享一个 request 对象。

 1、访问请求参数

<a href="login.jsp?name=张三&sex=man&id=" rel="external nofollow" >传递参数</a>

login.jsp关键 代码

<%= "name:"+new String(request.getParameter("name").getBytes("ISO-8859-1"),"utf-8") %><br>
<%= "sex:"+request.getParameter("sex") %><br>
<%= "id:"+request.getParameter("id") %><br>
<%= "pwd:"+request.getParameter("pwd") %><br>

说明:如果指定的参数不存在,将返回null;如果指定了参数名,但未指定参数值,将返回空的字符串"。

因为所有的request请求都是ISO-8859-1的,而在页面采用的是utf-8编码方式,所以在遇到中文时,将获取到的数据通过String的构造方法使用指定的编码类型重新构造一个String对象。

2、在作用域中管理属性

在进行请求转发时,需要把一些数据传递到转发后的页面进行处理,这时,需要用request对象的setAttribute方法将数据保存在request范围内的变量中。

 <%
   try {
     int money = 100;
     int number = 0;
     request.setAttribute("result", money / number);
   } catch (Exception e) {
     request.setAttribute("result", "很抱歉,页面产生错误!");
   }
 %>
 
 <jsp:forward page="deal.jsp" />
<%= request.getAttribute("result").toString() %>

由于getAttribute方法返回值是Object,需要调用toString方法转换为字符串。

3、获取cookie

cookie是小段文本信息,在网络服务器上生成,并发送给浏览器。通过cookie可以标识用户身份,记录用户名和密码,跟踪重复用户等。以键值对形式保存在客户机的某个目录下。

 <%
   Cookie[] cookies = request.getCookies();
   String user = "";
   String date = "";
   if (cookies != null) {
     for (int i = 0; i < cookies.length; i++) {
       if (cookies[i].getName().equals("mrCookie")) {
         user = URLDecoder.decode(cookies[i].getValue().split("#")[0]);
         date = cookies[i].getValue().split("#")[1];
       }
     }
   }
   if ("".equals(user) && "".equals(date)) {
 %>
 游客您好,欢迎您初次光临!
 <form action="deal.jsp" method="post">
 请输入姓名:<input name="user" type="text" value=""> <input
  type="submit" value="确定">
 </form>
 
 <% } else { %>
 
 欢迎
 <b> <%=user%></b> 再次光临
 <br>
 
 <% }%>

deal.jsp:

 <%
       request.setCharacterEncoding("GB18030");
  String user = URLEncoder.encode(request.getParameter("user"), "utf-8");
  Cookie cookie = new Cookie("mrCookie", user + "#" + new Date().toLocaleString());
  cookie.setMaxAge(60 * 60 * 24 * 30);
  response.addCookie(cookie);
 %>
 
 <script type="text/javascript">
 window.location.href = "index.jsp"
 </script>

4、获取客户端信息

 客户提交信息的方式:<%=request.getMethod() %>
 使用的协议:<%=request.getProtocol() %>
 客户端地址:<%=request.getRequestURL() %>
 客户端ip地址:<%=request.getRemoteAddr() %>
 服务器端口号:<%=request.getServerPort() %>
 服务器名称:<%=request.getServerName() %>
 客户端主机名:<%=request.getRemoteHost() %>
 客户端所请求的脚本文件的文件路径:<%=request.getServletPath() %>
 Http协议定义的文件头信息Host的值:<%=request.getHeader("host") %>
 Http协议定义的文件头信息User-Agent的值:<%=request.getHeader("user-agent") %>
 Http协议定义的文件头信息accept-language的值:<%=request.getHeader("accept-language") %>
 请求文件的绝对路径:<%=request.getRealPath("index.jsp") %>

5、显示国际化信息

浏览器可以通过accept-language的HTTP报头向Web服务器指明它所使用的本地语言。java.util.Local类型对象封装了一个国家和国家所使用的一种语言。示例如下:

 <%
   Locale locale = request.getLocale();
   String str = "";
   if (locale.equals(Locale.US)) {
     str = "Hello,welcome to access our company's web!";
   }
   if (locale.equals(Locale.CHINA)) {
     str = "您好,欢迎访问我们公司网站!";
   }
 %>
 
 <%=str%>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持来客网。