Detailed introduction Based on JSP HttpServlet

Source: Internet
Author: User
Document directory
  • HttpServletRequest
  • HttpServletResponse
  • Sample program

HttpServlet
Let's first review the class structure diagram mentioned in the previous section:


As you can see, HttpServlet inherits GenericServlet, but it is also an abstract class. It cannot be used directly but can only be inherited.

HttpServlet has two common methods:

DoGet
Void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

This method is called when the browser uses the GET Method for access.

DoPost
Void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

This method is called when the browser uses the POST method for access.

The internal processing methods of these two functions are basically the same as the GenericServlet. service () function described in the previous section.

Other HTTP requests also have the following methods:

HTTP request type HttpServlet Method
GET DoGet ()
POST DoPost ()
HEAD DoHead ()
PUT DoPut ()
DELETE DoDelete ()

HttpServletRequest

doGet()AnddoPost()The two parameters of the function areHttpServletRequestAndHttpServletResponseObject.

HttpServletRequestInterface indicates a browser request. You can use this class to obtain any information sent by the browser to the server. For PHP programmers, this class is a bit similar$_GET,$_POST,$_SERVERAnd other variables. The common method is as follows:

getParameter
String getParameter(String name)

Obtains the parameter value corresponding to the specified variable name. This method is actually a parent interface.javax.servlet.ServletRequest. If it is a GET request, the parameters after the query string are obtained, and the parameters in the <form> form are obtained in the POST request. Similar to PHP$_GETAnd$_POSTArray.

getParameterValues
String[] getParameterValues(String name)

This method is similargetParameter()Similar. This method should be used when you want to obtain form attributes that return multiple values.

getMethod
String getMethod()

Returns a string."GET"Or"POST".

getRequestURI
String getRequestURI()

Obtain the request URI (excluding the query string ). Equivalent to PHP$_SERVER['REQUEST_URI'].

getServletPath
String getServletPath()

Obtain the Servlet Path. Equivalent to PHP$_SERVER['PHP_SELF'].

getPathInfo
String getPathInfo()

Obtain PathInfo. Equivalent to PHP$_SERVER['PATH_INFO'].

setCharacterEncoding
void setCharacterEncoding(String new)

Set the request encoding. To process Chinese characters, you must use this method to set the correct character encoding. Otherwise, the text sent by the browser cannot be correctly read.

There are also many useful methods for you to refer to the interface documentation.

HttpServletResponse

The HttpServletResponse interface is used to control the content sent from the server to the client, which is equivalentecho,headerAnd other functions.

setContentType
void setContentType(String type)

Set the type of the returned value. The common HTML content can be set"text/html; charset=UTF-8"While the dynamically generated image can be set"image/gif". Before outputting a Chinese character, you must use this method to specify the encoding of the output character. Equivalent to writing in PHPheader("Content-Type: image/gif").

ServletOutputStream
ServletOutputStream getOutputStream() throws IOException

This method is used to obtain the output stream when sending binary data to the client.

getWriter
PrintWriter getWriter() throws IOException

When sending text data to a client, you need to use this method to obtain the output stream.

Sample program

When a Servlet was created in the previous section, we became a Java code and then added it to the Servlet section of web. xml. In fact, you can create a Servlet directly in the Servlet section of web. xml. Eclicpse will automatically generate Java code for us.

This time we will create a form submission program, submit data through an HTML form, and then read and display the data in the Servlet.

Right-click the WebContent directory and selectNew-> HTMLCreate an HTML document named htmlpost.html. The next step is to select an HTML template and use the default value directly.

Edit htmlpost.html. For more information, see the source code in this section. Source code download:

Httppost_jb51net.zip

Right-click Servlets in Deployment Descriptor and selectNew-> Servlet.

Enter the package name com. idv2.learnjsp in the Java package, enter the Class name HttpPost in the Class name field, and click Next.

On the Servlet ing configuration page, enter the appropriate Description. Note the following URL Mappings. This is the URL used to access the Servlet from a browser.

Next, select the attributes of the new class. Generally, select default. However, our Servlet only needs to process the POST method, so you only need to select doPost In the overload list below.

Finally, click Finish to create the Servlet. Eclipse will automatically generate the framework of the HttpPost. Java file in the src directory of the java code.

Edit the java code by referring to the following source code download.

Httppost_jb51net.zip

In fact, the main content of this Code is to get the data submitted by the client through the getParameter or getParameterValues method. The code snippets are as follows;

Copy codeThe Code is as follows: // Character Set
Request. setCharacterEncoding ("UTF-8 ");

// Obtain data from the form
Out. println ("<li> username:" + request. getParameter ("username "));
Out. println ("<li> password:" + request. getParameter ("password "));
Out. println ("<li> Confirm Password:" + request. getParameter ("confpass "));

// Obtain the form options of the check box
String interests [] = request. getParameterValues ("interests ");
Out. println ("<li> interests: <br/> ");
If (interests! = Null ){
For (int I = 0; I <interests. length; I ++ ){
Out. println (interests [I] + "<br/> ");
}
}

Related Article

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.