Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement.
The basic process and setting method of servlet are introduced in servlet appetizers. Here, we will look at some servlet instances. These instances are common problems in HTTP Communication (refer to HTTP protocol. We implement HTTP functions by operating request and response.
Generate page
Servlet is designed to dynamically generate pages. For example:
Package Foo; Import Javax. servlet .* ; Import Javax. servlet. http .* ; Import Java. Io .* ; Import Java. util .* ; Public Class Testpage Extends Httpservlet { Public Void Doget (httpservletrequest request, httpservletresponse response) Throws Ioexception, servletexception {printwriter out = Response. getwriter (); date now = New Date ();// Date & timeString page = "<HTML> <body> <p>" + now + "</P> </body> ; Out. println (PAGE );}}
Above, we callGetwriter ()Method To write text to the reply, that is, the body of the reply. For Java read/write, refer to Java I/O basics.
Sometimes the subject part is not text. In this case, we need an outputstream writer to call the responseGetoutputstream ()Method.
We useJava. util. Date ()To dynamically generate the date and time display. The effect is as follows:
Submit a table
The HTTP request has the POST method. Servlet uses the dopost () method to process post requests. The post method is used to submit data to the server, especially the data contained in the HTML form element.
Let's first compile an HTML page containing a form:
< Html > < Body > < Form Action = "Myform" Method = "Post" > Username: < Input Type = "Text" Name = "User" > < BR > < Input Type = "Submit" Value = "Submit" > </ Form > </ Body > </ Html >
The page will be submitted to myform for URL Processing. The submitted data is text-type user input.
Below is testform. JavaCodeThe servlet compiled by the Code corresponds to the URL of myform (set the ing relationship in Web. XML, refer to servlet appealer)
Package Foo; Import Javax. servlet .* ; Import Javax. servlet. http .* ; Import Java. Io .* ; Public Class Testform Extends Httpservlet { Public Void Dopost (httpservletrequest request, httpservletresponse response) Throws Ioexception, servletexception {string Username = Request. getparameter ("user" );// Get "user" InputPrintwriter out = Response. getwriter (); string page = "<HTML> <body> <p> submitted:" + username + "</P> </body> ; Out. println (PAGE );}}
In the above Code, what we rewrite isDopost ()Method. In addition, we callGetparameter ()To obtain the submitted data, that is, the user parameter value. We then showed the submitted data. In actual use cases, we can perform other operations on the data, such as putting the data into a database.
The result is as follows:
Other methods
We can see that HTTP operations in servlet are mainly implemented through the request and response objects. The two objects implement the httpservletrequest interface andHttpservletresponse Interface.The httpservletrequest interface inherits from the servletrequest interface,The httpservletresponse interface inherits from the serveletresponse interface.
Below are some common methods for httpservletrequest and httpservletresponse.
Httpservletrequest method (official reference)
Obtain header information
String host = request. getheader ("host ");
Request Method
String method = request. getmethod ();
Obtain cookies in the request
Cookie [] cookies = request. getcookies ();
Inputstream is required to read non-text resources in the request.
Inputstream input = request. getinputstream ();
Httpservletresponse method (official reference)
Set MIME type
Response. setcontenttype ("image/JPEG ");
Redirect
Response. sendredirect ("http://www.google.com ");
Set header information
Response. setheader ("server", "Apache-Coyote/1.1 ");
Set contentlength
Response. setcontentlength ();
Set status code
Response. setstatus (200 );
Summary
Httpservletrequest and httpservletresponse are specific implementations of HTTP protocol in servlet. By calling them, we can use the HTTP protocol within Java. Understanding the HTTP protocol itself will help us better use these two interfaces.
Welcome to continue reading the "Java quick tutorial" SeriesArticle