I have few questions.
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FormServlet extends HttpServlet {
protected void doService (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
GET
, POST
, DELETE
, PUT
(for more see specification section 5.1.1 ). doService
methods. If you meant void service()
then I advise you not to mess with it unless you really know what you're doing. If all you need is to use doService
call it from doGet
, doPost
(as someone already suggested). Ok, examples:
public class DoesNothingServlet extends HttpServlet {} //does what the name implies
public class FormServlet extends HttpServlet { //what you want to do
protected doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doService(request,response)
}
protected void doService (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//Do something
}
}
No, it's not mandatory. Since HttpServlet
is an abstract class, there are abstract implementations of all doXXX
methods, and you don't have to implement them if you don't want to.
Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site. A subclass of HttpServlet must override at least one method, usually one of these:
- doGet, if the servlet supports HTTP GET requests
- doPost, for HTTP POST requests
- doPut, for HTTP PUT requests
- doDelete, for HTTP DELETE requests
- init and destroy, to manage resources that are held for the life of the servlet
- getServletInfo, which the servlet uses to provide information about itself
From your doGet and doPost methods, you can call your doService(..,..) method if you wish.
You will note that the service()
method originates from the javax.servlet.GenericServlet
and not javax.servlet.http.HttpServlet
.
If you want to do something with a servlet which doesn't involve the http protocol , I would say go ahead.
In the Head First Servlets and JSP they explain this in detail. 99% You will make use of the HttpServlet
.
By extending GenericServlet
, the servlet would run regardless of the content submitted. As long as the URL is fired the service()
method will execute.
My understanding, if I got you correct, is you want your HTTP GET, POST, (PUT, DELETE) to call your doService
method. If that's the case, you can do this.
protected doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doService(request,response)
}
protected doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doService(request,response)
}
If you extends HttpServlet
you don't have to override doGet
and doPost
as it's already implemented by HttpServlet
. Servlet request get handled by the service()
method which then (based on the HTTP request method) calls its relevant doXXX
method.
I wouldn't mess with the service()
method though, unless you know what you're doing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.