Detailed introduction of httpservlet based on JSP
Httpservlet first review the class structure diagram mentioned in the previous section:
There are two methods commonly used in httpservlet:
doGet void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
This method is called when the browser accesses it in get mode.
doPost void doPost(HttpServletRequest request,IOException
This method is called when the browser accesses it in post mode.
The internal processing methods of these two functions are basically the same as those described in the previous section The service() function is the same.
In addition, other HTTP requests also have corresponding methods:
HttpServletRequest
The two parameters of the doget() and dopost() functions are HttpServletRequest and httpservletresponse objects.
The HttpServletRequest interface represents the browser request. You can obtain any information sent by the browser to the server through this class. For PHP programmers, this class is a bit similar to$_ GET、$_ POST、$_ Server and other variables. Its common methods are as follows:
Gets the parameter value corresponding to the specified variable name. This method is actually the parent interface javax servlet. Method of ServletRequest. If it is a get request, get the parameters after the query string, and if it is a post request, get the parameters in the < form > form. Similar to PHP$_ Get and$_ Post array.
This method is similar to getparameter (). This method should be used when you want to get form attributes that return multiple values such as < input type = "check" > and so on.
Returns the string "get" or "post".
Gets the URI of the request (excluding the query string). Equivalent to $_server ['request_uri '] of PHP.
Gets the path of the servlet. Equivalent to PHP$_ SERVER['PHP_SELF']。
Get pathinfo. Equivalent to PHP$_ SERVER['PATH_INFO']。
Set the encoding of the request. When you need to process Chinese characters, you must set the correct character code through this method, otherwise you will not be able to correctly read the text sent by the browser.
There are many useful methods. You can refer to the interface document yourself.
HttpServletResponse
The httpservletresponse interface is used to control the content sent by the server to the client, which is equivalent to PHP echo, header and other functions.
Sets the type of the return value. Normal HTML content can be set to "text / HTML; charset = UTF-8", while dynamically generated pictures can be set to "image / GIF". Before outputting Chinese characters, be sure to specify the output character code through this method. It is equivalent to writing header ("content type: image / GIF") in PHP.
When sending binary data to the client, you need to obtain the output stream through this method.
When sending text data to the client, you need to obtain the output stream through this method.
Sample program
When we created the servlet in the previous section, our husband became java code and added it to the web In the servlet section of XML. In fact, it can be directly on the web Create a servlet in the servlet section of XML, and eclicpse will automatically help us generate java code.
This time, we will establish a form submission program to submit data through an HTML form, and then read the data in the servlet and display it.
First, right-click the webcontent directory, select new - > HTML, and create a new HTML document named htmlpost html。 The next step is to select the HTML template and use the default value directly.
Then edit htmlpost HTML, please refer to the source code of this section. Source code download:
httppost_ jb51net. zip
Right click servlets in the deployment descriptor and select new - > servlet.
Enter the package name com. In the java package as shown in the following figure idv2. Learn jsp, enter the class name httppost in the class name field, and click next.
The configuration interface of servlet mapping appears, and enter the appropriate description. Note the URL mappings below, which is the URL used when accessing the servlet from the browser.
Next, select the properties of the newly created class, usually default. However, our servlet only needs to handle the post method, so we just need to select dopost in the overload list below.
Finally, click Finish to complete the establishment of the servlet, and eclipse will automatically generate httppost. HTML in the SRC directory of the Java code Java file framework.
Edit java code, you can refer to the following source code download.
httppost_ jb51net. zip
In fact, the main content of this code is to obtain the data submitted by the client through getparameter or getparametervalues methods. The code fragment is as follows;