Serlvet接口只定义了一个服务方法就是service,而HttpServlet类实现了该方法并且要求调用下列的方法之一:
doGet:处理GET请求
doPost:处理POST请求
当发出客户端请求的时候,调用service 方法并传递一个请求和响应对象。Servlet首先判断该请求是GET 操作还是POST 操作。然后它调用下面的一个方法:doGet 或 doPost。如果请求是GET就调用doGet方法,如果请求是POST就调用doPost方法。doGet和doPost都接受请求(HttpServletRequest)和响应(HttpServletResponse)。
doget 是接收网页用get方法时调用的 ==
dopost 是用来接收post方法的 ==
get方法就象你在网页的地址栏里看到的一堆乱码,也就是url后面有参数
post就是用表单传过去的,就好象把数据都打成包发过去一样
如果不知道用的什么方法,你可以在servlet里把功能都写到一起
然后在一个方法里调用另一个方法就可以了
比如
public void doPost(HttpServletRequest request,HttpServletResponse)
throws ServletException,IOException
{
doGet(request,response);
}
doGet()和doPost()方法源码解析
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
2. //获取协议
3. String protocol = req.getProtocol();
4. //获取http.method_get_not_supported的国际化字符串
5. String msg = lStrings.getString("http.method_get_not_supported");
6. if(protocol.endsWith("1.1")) {
7. //如果是HTTP/1.1,返回405禁止访问方法错误
8. resp.sendError(405, msg);
9. } else {
10. //如果不是HTTP/1.1,返回400错误的请求错误
11. resp.sendError(400, msg);
12. }
13.
14. }
15.
16. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
17. String protocol = req.getProtocol();
18. String msg = lStrings.getString("http.method_post_not_supported");
19. if(protocol.endsWith("1.1")) {
20. resp.sendError(405, msg);
21. } else {
22. resp.sendError(400, msg);
23. }
24.
25. }
两个方法如果不被重写,那执行时默认会调用HttpServlet的代码,首先获取协议,然后获取国际化字符串,最后判断协议,如果协议为HTTP/1.1,返回405禁止访问方法错误,//如果不是HTTP/1.1,返回400错误的请求错误。
Servlet -doGet() doPost()原理
一、自定义类只需要重写doGet(HttpServletRequest request, HttpServletResponse response) 和doPost(HttpServletRequest request, HttpServletResponse, response)的原因:
1、在HttpServlet接口中重写了父类的service(ServletRequest request, ServletResponse response),在这里面主要完成了将父类的request和response对象转化成专门用于处理HTTP请求响应的HttpServletRequest和HttpServletResponse对象,然后再调用一个重载的service方法,该方法接收HttpServletRequest和HttpServletResponse对象:
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException
{
HttpServletRequest request;
HttpServletResponse response;
try {
request = (HttpServletRequest) req;
response = (HttpServletResponse) res;
} catch (ClassCastException e) {
throw new ServletException("non-HTTP request or response");
}
service(request, response);
}
}
2、在重载的service方法中,调用了doGet(HttpServletRequest request, HttpServletResponse)和doPost(HttpServletRequest request, HttpServletResponse),用于处理Get和Post请求和响应:
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String method = req.getMethod();
if (method.equals(METHOD_GET)) {
long lastModified = getLastModified(req);
if (lastModified == -1) {
// servlet doesn't support if-modified-since, no reason
// to go through further expensive logic
doGet(req, resp);
} else {
long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
if (ifModifiedSince < (lastModified / 1000 * 1000)) {
// If the servlet mod time is later, call doGet()
// Round down to the nearest second for a proper compare
// A ifModifiedSince of -1 will always be less
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else {
resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
}
}
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_get_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}
由上可知,Servlet引擎在处理用户请求时所调用的service(ServletRequest req, ServletResponse rep)最终会执行doGet和doPost方法中的代码完成。这就是在自定义的Servlet类中,只要继承了HttpServlet就只需要重写doGet和doPost即可。
二、Servlet的生命周期:
1、加载和实例化Servlet对象:由Servlet窗口负责加载Servlet,当Servlet窗口启动或是检测到需要这个Servlet响应第一个请求时,将加载这个Servlet。加载的方式是通过反射的API去调用Servelet的无参构造方法完成,所以在自定义的Servlet中不应该出现带参数的构造方法,如果需要定义有参的构造方法,则一定要把无参的也写出来。
2、初始化Servlet对象:调用init()方法完成相应的初始化,如读取初始化参数、连接DB等。
3、处理请求:调用service()方法处理请求。之前一这是init()方法执行成功。
4、销毁Servlet对象:调用detroy()方法。