HttpServlet
Let's review the class structure diagram mentioned in the previous section:
As you can see, HttpServlet inherits Genericservlet, but it is also an abstract class that cannot be used directly and can only be inherited.
There are two commonly used methods in HttpServlet:
Doget
void Doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException
The method is invoked when the browser is accessed in Get mode.
DoPost
void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException
The method is invoked when the browser is accessed by post.
The processing methods inside these two functions are essentially the same as the Genericservlet.service () functions described in the previous section.
Other HTTP requests also have a corresponding method:
HTTP request category |
The method of HttpServlet |
Get |
Doget () |
POST |
DoPost () |
Head |
Dohead () |
Put |
DoPut () |
DELETE |
Dodelete () |
HttpServletRequest
doGet()
and doPost()
The two parameters of the function are HttpServletRequest
and HttpServletResponse
objects.
HttpServletRequest
interface represents a browser request, which you can use to get any information that the browser sends to the server. For PHP programmers, this class is a bit like $_GET
, $_POST
,, and $_SERVER
the contents of several variables. Its common methods are as follows:
getParameter
String getParameter(String name)
Gets the parameter value that corresponds to the specified variable name name. The method is actually a method of the parent interface javax.servlet.ServletRequest
. If a GET request gets the argument after the query string, the POST request gets the parameters in the <form> form. Similar to PHP $_GET
and $_POST
arrays.
getParameterValues
String[] getParameterValues(String name)
This method is similar to that of getParameter()
. This method should be used when you want to get <input type= "Check" > a form property that returns multiple values.
getMethod
String getMethod()
Returns a string "GET"
or "POST"
.
getRequestURI
String getRequestURI()
Gets the URI of the request (excluding the query string). Equivalent to PHP $_SERVER['REQUEST_URI']
.
getServletPath
String getServletPath()
Gets the path of the servlet. Equivalent to PHP $_SERVER['PHP_SELF']
.
getPathInfo
String getPathInfo()
Get PathInfo. Equivalent to PHP $_SERVER['PATH_INFO']
.
setCharacterEncoding
void setCharacterEncoding(String new)
Sets the encoding of the request. When you need to process Chinese characters, you must set the correct character encoding through this method, otherwise you will not be able to correctly read the text from the browser.
There are many useful ways you can refer to the interface documentation yourself.
HttpServletResponse
The HttpServletResponse interface is used to control what the server sends to the client, equivalent to PHP echo
, and header
so on.
setContentType
void setContentType(String type)
Sets the type of the return value. The usual HTML content can be set to "text/html; charset=UTF-8"
wait, while dynamically generated pictures can be set to "image/gif"
wait. Before outputting Chinese characters, it is important to specify the character encoding of the output by this method. Equivalent to writing in PHP header("Content-Type: image/gif")
.
ServletOutputStream
ServletOutputStream getOutputStream() throws IOException
This method is needed to get the output stream when binary data is sent to the client.
getWriter
PrintWriter getWriter() throws IOException
This method is needed to get the output stream when text data is sent to the client.
Sample Programs
When we created the servlet in the previous section, our husband became Java code and added it to the Web.xml servlet section. You can actually build the servlet,eclicpse directly in the Web.xml servlet section to automatically generate Java code for us.
This time we'll create a form submission program that submits data through an HTML form and then reads the data in the servlet and displays 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 the htmlpost.html to refer to the source code in this section. Source code Download:
Httppost_jb51net.zip
Right-click the servlets in the deployment descriptor and select New->servlet.
Follow the image below to enter the package name com.idv2.learnjsp at the Java package, enter the class name HttpPost at the class name, and click Next.
A servlet mapping configuration interface appears, and the appropriate description is entered. Note The URL mappings below, which is the URL to use when accessing the servlet from the browser.
The next step is to select the properties of the newly created class, usually by default. But our servlet only needs to handle the post method, so just select Dopost in the overloaded list below.
Finally click Finish to complete the servlet setup, and Eclipse automatically generates the Httppost.java file framework in the SRC directory of Java code.
Edit Java code, you can refer to the following source code download.
Httppost_jb51net.zip
In fact, the main content of this code is through the getparameter or getparametervalues method to get the data submitted by the client. The code fragment is as follows;
Copy Code code as follows:
Character
Request.setcharacterencoding ("UTF-8");
Get the data from the form
Out.println ("<li> User name:" + request.getparameter ("username"));
Out.println ("<li> Password:" + request.getparameter ("password"));
Out.println ("<li> Confirm Password:" + request.getparameter ("Confpass"));
Get form options for check boxes
String interests[] = request.getparametervalues ("interests");
Out.println ("<li> Hobbies: <br/>");
if (interests!= null) {
for (int i = 0; i < interests.length; i++) {
Out.println (Interests[i] + "<br/>");
}
}