[Javaweb] Servlet request and response


1. Request

HttpServletRequest represents a Servlet request in the Http environment

1.1 Common methods

method describe
String getParameter(String name) Obtain submission data based on the form component name (Note: The server uses strings to receive data uniformly when receiving data)
String[ ] getParameterValues(String name) Get the request data when the form component corresponds to multiple values
void setCharacterEncoding(String charset) Specify the encoding for each request (only works for post requests)
RequestDispatcher getRequestDispatcher(String path) Jump to the page and return a RequestDispatcher object. The forward() method of this object is used to forward the request.
request.setAttribute(“key”,value); Store value
request.getAttribute(“key”); Transform downward after taking value

1.2 The client sends data to the server

  • Submit via form get/post
  • Send data through a tag (get submission)
  • Directly splice -get request through the address bar
  • js submit data-get request
<a href="请求名?key=value&key=value&key=value...">


key=表单元素的控件名
value=表单中控件的value属性值

注:第一个参数使用?拼接,之后的参数使⽤&拼接,获取数据还是通过 String name=request.getParameter("name");

1.3 The difference between get and post

get post
data transmission The data of the GET request will be appended to the URL to split the URL and transmit data. Multiple parameters are connected with &, and the data will be exposed in the address bar. Place the requested data in the body of the HTTP request packet, and the data will not be exposed.
Transfer data size Transmitting data will be limited by the length of the URL (in the HTTP specification, there is no limit on the length of the URL and the size of the transmitted data, but in the actual development process, for GET, specific browsers and servers have restrictions on the length of the URL) Each server will stipulate restrictions on the size of POST submission data (Apache and IIS have their own configurations)
safety Low (information is exposed in the URL, the login page may view the account password through the browser cache and browser history, and the data submitted by the GET request may also cause a Cross-site request frogery attack) high
- The URL encoding format uses ASCII encoding instead of uniclde, so all non-ASCII characters must be encoded before being transmitted. -

2. Response

In the Service API, an HttpServletResponse interface is defined, which is inherited from the ServletResponse interface and is used to encapsulate HTTP response messages.

The HttpServletResponse interface defines methods for sending response status codes, message headers, and message bodies to the client.

2.1 Common methods

method describe
void addCookie(Cookie var1) Add a cookie to the response
void sendRedirect(String var1) Send a response code to jump the browser to the specified location
PrintWriter getWriter() Obtain the character stream and set the string into the response buffer through write(String s) of the character stream. Then Tomcat assembles the content in the response buffer into an Http response and returns it to the browser.
setContentType() Set the type of response content

2.2 Redirection and forwarding

Both are used to jump to pages

Redirect Forward
response.sendRedirect() request.getRequestDispatcher("…/success.jsp").forward(request,response)
The address bar will change and the data stored in the request will be lost. The address bar displays the address of the request page, and the request data can be saved.
Two requests (the address bar was modified twice) and two responses One request and one response

Insert image description here

Guess you like

Origin blog.csdn.net/m0_50609545/article/details/120383218