Java Web Learning (15): Client requests, server responses and their HTTP status codes

Source: Internet
Author: User

        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

Interest part of the head to be transmitted. 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" ><tr bgcolor= "#949494" ><th>header name</th><th>header Value (s) </th></tr><%Enumeration headernames = Request.getheadernames ();while (Headernames.hasmoreelements ()) {String paramname = (string) headernames.nextelement ();Out.print ("<tr><td>" + paramname + "</td>\n");String paramvalue = Request.getheader (paramname);Out.println ("<td>" + paramvalue + "</td></tr>\n");}%> </table></body>

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" >Set to refresh automatically every 5 secondsResponse.setintheader ("Refresh", 5);Get current timeCalendar calendar = new GregorianCalendar ();String am_pm;int hour = Calendar.get (Calendar.hour);int minute = Calendar.get (Calendar.minute);int second = Calendar.get (Calendar.second);if (Calendar.get (calendar.am_pm) = = 0) {}AM_PM = "AM";Elseam_pm = "PM";String CT = Hour + ":" + Minute + ":" + second + "+ am_pm;Out.println ("Current time:" + CT + "\ n");%></body>

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 code

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:

Java Web Learning (15): Client requests, server responses and their HTTP status codes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.