service
Not commonly used, slightly
statement
Override the service method
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException){
…
}
doGet
GET request
When accessing a link, if not specified access method, the default GET , used to send a few parameters to the server, query, write parameters in the access link in
format
Link ? Parameter 1 = value 1 & parameter 2 = value 2 & ... & parameter N = the value of N
Features
For handling the browser sent a GET request (to access the link if the request method is not specified, defaults are GET),
statement
protected void doGet(HttpServletRequest request, HttpServletResponse response){
…
}
doPost
POST request
Send larger amounts of data to the server, the request mode is designated POST , the parameters written in the POST request header, the access link Not visible
format
Generally when the form submit button to set the "commit" event
Features
Sent from the server for processing Post request
statement
protected void doGet(HttpServletRequest request, HttpServletResponse response){
…
}
HttpServletRequest
Acquiring request information, such as the request header, the request mode, parameters
Common method |
Explanation |
return value |
setCharacterEncoding("UTF-8") |
Character set information setting request |
|
getParameter ( " parameter name ") |
Gets parameter values |
String |
getAttribute ( " property name ") |
Gets the property value |
Object |
the setAttribute ( " attribute " value ) |
In req added attribute = value |
|
getRequestDispatcher(其他servlet路径).forward(request,response) |
Forwarding the request to another servlet processing |
|
parameter and attribute the difference between:
parameter can only be acquired, can not be set, or is submitted by the form obtained connection parameters, return the string
attribute can be set and retrieve their own, may be added to any type of value, return Object type data, to be cast
HttpServletResponse
Set return information, sending data / files / images to the browser, such as the status code, the page jumps, etc.
Common method |
Explanation |
return value |
sendRedict ( page ) |
Redirect, the equivalent of a hyperlink |
|
Forwarding and Redirection
Forwarded
For different servlet transfer of data between, to complete a different job, may be used before forwarding setAttribute Add Property
Redirect
That is a hyperlink, the link address parameter passing by, POST information will be lost
the difference
|
Forwarded |
Redirect |
grammar |
request.sendRedirect ( URL ) |
response.getRequestDispatcher(其他servlet路径).forward(request,response) |
speed |
slow |
fast |
parameter |
Passing through the link, Request information will be lost |
通过attribute设置后,连同当前的request信息一并传递 |
配置文件
<servlet>
<servlet-name>配置名<servlet-name>
<servlet-class>Servlet类路径</servlet-class>
<servlet>
<servlet-mapping>
<servlet-name>上面的配置名</servlet-name>
<url-pattern>该servlet的访问链接</url-pattern>
<!--可有多个访问链接,指向同一个jsp文件-->
<servlet-mapping>