I. Overview
We will overwrite the service () method when creating the servlet, or doget ()/dopost (), which have two parameters, one for the request requested and the representative response response.
The type of response in the service method is Servletresponse, and the response type of the Doget/dopost method is HttpServletResponse, HttpServletResponse is a servletresponse sub-interface, with more powerful functions and methods. second, the operation flow of response
Our request was first handed to the Tomcat engine of the Tomcat container. The Tomcat engine helps us create the request and response. At this point, the request encapsulates the requested information, and the response is empty. Request and response to the specific servlet,response to collect some information, stored in the response buffer (some collections). Response back to the Tomcat engine. The Tomcat engine gets the contents of the response buffer, which is assembled into an HTTP response with the information added by the engine itself.
third, the structure of the HTTP response
The HTTP response is divided into three parts: Response line, response header, and response body. The response can be set for these three parts.
For the status line we will set the status code. The information will automatically change with the status code, so we don't have to set it up.
For the response header, there are some key-value pairs.
For the response body, the stream is used for output. Getwriter (). Write ("string") or Getoutputstream (). Writer ("bytes"). Iv. Setting the status code for the HTTP response line
Package com.ken.line;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Statusservlet extends HttpServlet {
private static final long serialversionuid = 1L;
protected void doget (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
//manually set the status code in the HTTP response line
Response.setstatus (302);
}
protected void DoPost (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
doget (request, response);
}
}
v. Setting the response head via response
AddHeader (String name,string value)
Addintheader (String name, int value)
Adddateheader (String name, long date)
SetHeader (String name,string value)
Setdateheader (String name, long date)
Setintheader (String name, int value)
Where add represents the addition, and set represents the set
Redirect: Status Code 302 response Header: Location represents the redirected address 5.1 AddHeader
Package com.ken.header;
Import java.io.IOException;
Import java.sql.Date;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Headerservlet extends HttpServlet {
private static final long serialversionuid = 1L;
protected void doget (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
Date data = new Date (0);
Set the response header
Response.AddHeader ("name", "Zhangsan");
Response.addintheader ("Age", at a);
Response.adddateheader ("Birthday", Data.gettime ());
}
protected void DoPost (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
doget (request, response);
}
}
5.2 SetHeader
To know the difference between SetHeader and AddHeader, let's add two headers first.
Package com.ken.header;
Import java.io.IOException;
Import java.sql.Date;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Headerservlet extends HttpServlet {
private static final long serialversionuid = 1L;
protected void doget (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
Date data = new Date (0);
Set the response header
Response.AddHeader ("name", "Zhangsan");
Response.addintheader ("Age", at a);
Response.adddateheader ("Birthday", Data.gettime ());
Response.AddHeader ("name", "Lisi");
}
protected void DoPost (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
doget (request, response);
}
}
AddHeader two name, two name in response.
Look at SetHeader again.
Package com.ken.header;
Import java.io.IOException;
Import java.sql.Date;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Headerservlet extends HttpServlet {
private static final long serialversionuid = 1L;
protected void doget (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
Date data = new Date (0);
Set the response header
Response.AddHeader ("name", "Zhangsan");
Response.addintheader ("Age", at a);
Response.adddateheader ("Birthday", Data.gettime ());
Response.setheader ("name", "Lisi");
}
protected void DoPost (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
doget (request, response);
}
}
SetHeader covered the previous name.
5.3 redirects
Servlet1
Package com.ken.header;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Servlet1 extends HttpServlet {
private static final long serialversionuid = 1L;
protected void doget (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
//No response informs the client to redirect to Servlet2
//1. Set the status code to 302
Response.setstatus (302);
2. Set the response header location
response.setheader ("Location", "/servlet2");
}
protected void DoPost (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
doget (request, response);
}
}
Servlet2
Package com.ken.header;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Servlet2 extends HttpServlet {
private static final long serialversionuid = 1L;
protected void doget (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
response.getwriter (). Write ("Hello world ...");
protected void DoPost (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
doget (request, response);
}
}
Results:
Sendredirect
Package com.ken.header;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Servlet1 extends HttpServlet {
private static final long serialversionuid = 1L;
protected void doget (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
//No response informs the client to redirect to Servlet2
//1. Set the status code to 302
//Response.setstatus (302);
2. Set the response header location
//Response.setheader ("Location", "/servlet2");
Encapsulated as a redirect method Sendredirect (URL)
response.sendredirect ("/servlet2");
}
protected void DoPost (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
doget (request, response);
}
}
Refresh
Package com.ken.header;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Refreshservlet extends HttpServlet {
private static final long serialversionuid = 1L;
protected void doget (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
//Set the header
response.setheader ("Refresh", "5;url=https://www.baidu.com/") for timed refresh;
}
protected void DoPost (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
doget (request, response);
}
}
Refresh and sendredirect function basically the same. The difference is simply that refresh will delay the redirection.