A JSP client request
When a Web page is requested by the browser, it sends a series of information to the Web server that cannot be read directly, as this information is used as an HTTP
Part of the rest of the head. We can check the HTTP protocol to get more information.
The following table lists some of the important aspects of the browser-side information header, which you will see frequently in future network programming:
(1) HttpServletRequest class
The request object is an instance of the Javax.servlet.http.HttpServletRequest class. Each time a client requests a page, the JSP engine will produce
A new object to represent this request.
The Request object provides a series of methods to get HTTP headers, including form data, cookies,http methods, and so on.
The next step is to introduce some of the methods that are commonly used in JSP programming to get HTTP information headers. Please see the table below for details:
(2) HTTP Information Header Example
In the following example, we use the Getheadernames () method of the HttpServletRequest class to read the HTTP header. This one
method returns the header information for the current HTTP request as an enumeration.
After getting the enumeration object, use the standard way to traverse the enumeration object and use the hasMoreElements () method to determine when
Stop and use the Nextelement () method to get the name of each parameter.
Main.jsp Source code is:
<% @page import= "java.util.Enumeration"%><%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
To access main.jsp, you will get the following results:
Two JSP server response
The response response object mainly passes the result of the JSP container processing back to the client. You can set the state of the HTTP and the guest by using the response variable
Send data, such as cookies, HTTP header information, and so on.
A typical response looks like this:
http/1.1 OK
Content-type:text/html
Header2: ...
...
HeaderN: ...
(Blank line)
<!doctype ...>
<body>
...
</body>
The status line contains HTTP version information, such as http/1.1, a status code, such as 200, and a very short message that corresponds to the status code,
such as OK.
The following table summarizes the most useful parts of the HTTP1.1 response header, which you will see frequently in network programming:
(1) HttpServletResponse class
The response object is an instance of the Javax.servlet.http.HttpServletResponse class. Just as the server would create a request object,
It also creates a client response.
The response object defines the interface that handles the creation of HTTP information headers. By using this object, developers can add new cookies or time
HTTP status code, and so on.
The following table lists the methods used to set HTTP response headers, which are provided by the HttpServletResponse class:
(2) HTTP Response Header Program example
The next example uses the Setintheader () method and the Setrefreshheader () method to simulate a digital clock:
Main1.jsp Source code:
<% @page import= "Java.util.GregorianCalendar"%><% @page import= "Java.util.Calendar"%><%@ page Language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Save the above code as main1.jsp and then access it through your browser. It will display the current time of the system every 5 seconds.
We can look at the following GIF demo:
Three HTTP status codes
HTTP requests are similar in format to HTTP responses, and have the following structure:
1) Start with status line +crlf (carriage return line)
2) 0-line or multi-wardrobe module +crlf
3) A blank line, such as CRLF
4) optional message body such as file, query data, query output
For example, a server response header looks like this:
http/1.1 OK
Content-type:text/html
Header2: ...
...
HeaderN: ...
(Blank line)
<!doctype ...>
<body>
...
</body>
The status line contains the HTTP version, a status code, and a short message corresponding to the status code.
The following table lists the HTTP status codes and the messages associated with them that may be returned from the server:
(1) How to set the HTTP status code
The following table lists the methods used to set the status code in the HttpServletResponse class:
(2) HTTP Status Code Program example
The next example will send a 407 error code to the browser, and then the browser will tell you "Need authentication!!!".
Main2.jsp Source code:
To access the JSP page above, you will get the following results:
Client requests, server responses, and their HTTP status codes