jsp built-in objects response and request

1, request objects

       javax.servlet.http.HttpServletRequest request object is an instance of the class. Whenever a client requests a JSP page, JSP engine will create a new request object to represent this request. The request object provides a range of methods to obtain HTTP headers, cookies, HTTP methods, and so on. Client request information is encapsulated in the request object, which can be learned by the needs of users, and then responds. It is an instance of class HttpServletRequest. The request object having a request field, i.e. before completion of the requesting client, the object has been effective. Common methods are as follows

 1. setAttribute()在Request域中存储数据
 2. setCharacterEncoding()设置请求参数的编码方式,只对post请求有效
 3. getMethod()获取请求类型
 4. getParameter()获取指定请求参数值
 5. getParameterNames()获取所有请求参数,返回一个Enumeration枚举类
 6. getAttribute()获取域中存储的数据
 7. getAttributeNames()获取域中所有数据,返回一个Enumeration枚举类
 8. getContextPath()返回当前WEB项目相对路径
 9. getCookies()获取所有Cookie,返回一个Cookie数组
 10. getRequestDispatcher()获取转发器,参数路径以/打头,代表WebRoot(当前WEB项目根目录)
 11. getSession()获取Session对象

A, request.getParameter () and request.getAttribute () difference

  (1) request.getParameter () is made to obtain a similar post, get, etc. incoming data, request.setAttribute () and getAttribute () just inside the web transfer container container realized by merely request processing stage.

  (2) request.getParameter () method of data transfer, the Web server side will be transmitted from the Web client, on behalf of the HTTP request data. request.getParameter () method returns the String type of data.

   Data request.setAttribute () and getAttribute () method is transmitted only present in the Web container. Another point is that, the HttpServletRequest class has setAttribute () method, but not the setParameter () method. Take a look at it for example, if the time between two WEB pages to link the relationship, that is to say from 1.JSP link to 2.JSP, it is linked 2.JSP request parameters can be obtained by getParameter () method .

 

If there 1.JSP

<form name="form1" method="post" action="2.jsp"> 
请输入用户姓名:<input type="text" name="username"> 
<input type="submit" name="Submit" value="提交"> 
form> 

In 2.JSP by request.getParameter ( "username") method to obtain the requested parameters username:

<% String username = request.getParameter ( "username");%> However, if WEB is between two forwarding relationship, forwarding destination WEB can getAttribute () method and the forward range of the source WEB shared data request, it is still said one example of it. There 1.JSP and 2.JSP

1.JSP want to pass the current user name to 2.JSP, how to pass this information?

First call as follows setAttribute () method in 1.JSP:

<%  
String username=request.getParameter("username");  
request.setAttribute("username",username);  
%> 
<jsp:forward page="2.jsp" /> 
在2.jsp中通过getAttribute()方法获得用户名字:  
<% String username=(String)request.getAttribute("username"); %> 

二、request.getAttribute()与request.setAttribute()

      request.getAttribute ( "nameOfObj") Value JSP pages available in a form control. In fact, form controls in the Object name and value is stored in a hash table, so here is given Object of name will correspond to the hash table to find its value.

      The traditional values ​​among different pages when using request.setAttribute (position, nameOfObj), only one pass from a.JSP to b.JSP, after the request loses its scope, and then send it necessary to set up a request. setAttribute (). The use session.setAttribute () will always retain this value in a process.

PS: JavaScript and JSP can not pass each other value, because JavaScript runs on the client, and JSP running on the server side. For each parameter can be passed so that, a hidden control may be provided in the JSP, to pass the required data value with the value in conjunction with the above mentioned usage therebetween.

2, response objects

     2.1, the basic concept:

     It contains information to respond to customer requests. It is an instance of class HttpServletResponse.

     response object has page scope, ie visit a page, the response object within the page can only be effective for the visit, the response object other pages of the current page is invalid.

Method Description:

 1. setContentType()告知浏览器数据类型
 2. setCharacterEncoding设置Response的编码方式
 3. setHeader()设置消息头
 4. setStatus()设置状态吗
 5. addCookie()添加Cookie
 6. sendRedirect()重定向
 7. getOutputStream()获取通向浏览器的字节流(同一次请求处理中,字节流和字符流不能同时存在)
 8. getWriter()获取通向浏览器的字符流(同一次请求处理中,字节流和字符流不能同时存在)

sendRedirect and forwarding the similarities and differences:

Forwards play a role in the server, forward method to submit information transfer between pages, request will not be lost, the address bar will not change

Redirection is the customer service side to play a role in achieving the steering through the new page address request, request will be lost, the address bar will become

Compare encoding settings:

request.setCharacterEncoding (): set value is obtained or removed from the request value from the database

Encoding the specified page response; response.setContentType ( "charset = gb2312 text / html")

The former is to set dynamic text (parameters, database), which set the static pages of text 

     2.2, response common usage:

1, the redirecting page

        Redirection support to redirect to a different host address, and forwarding it different. On the client browser will be Jump address, and re-send the request link. After redirection, request the property all fail, and start a new request object.

<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
	<%response.sendRedirect("deal.jsp"); %>

2, handle HTTP header

(1), disable caching

Default web browser will cache the page display speed can be improved, but for some high security requirements of the site, usually need to disable caching

<% response.setHeader("Cache-Control", "no-store");

response.setDateHeader("Expires", 0);%>

(2), automatic refresh

<!-- 每隔10秒自动刷新一次 -->
	<% response.setHeader("refresh", "10");%>

(3) the timing of a jump to a page

     Automatically jump to a specified page after 5 seconds:

<!-- 五秒后跳到制定网页 -->
<% response.setHeader("refresh", "5;URL=deal.jsp");%>

 

Third, setting the output buffer

Meet one of the following three cases, the contents of the buffer will be written to the client.

(1) output JSP pages have all been written into the buffer

(2) buffer is full

(3) calls the response of flushBuffer or flush out the method

This article excerpt articles blog.

http://developer.51cto.com/art/200906/132743.htm

https://blog.csdn.net/lulei1217/article/details/50755364

 

Published 14 original articles · won praise 1 · views 5531

Guess you like

Origin blog.csdn.net/Vpn_zc/article/details/81266908