杂谈:Servlet(2)
Servlet的方法剖析:
1.service()方法里面做了什么?
2.doGet()与doPost()做了什么?应该怎么写?
回答
1.service()方法里面做了什么?

如果你的service方法中没有写super.service(req, resp); 那么后果是doget()和dopost()方法永远不会调用.这是为什么呢?
我们可以查看一下HttpServlet的源代码:(Ps:已经有删除 , 补录中有完整的代码....)
------------------------Service()--------------------------------------------------
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String method = req.getMethod();
if (method.equals(METHOD_GET)) {//如果" 请求 "是get方法 , 那就调用doget , 否则看看是不是post
long lastModified = getLastModified(req);
if (lastModified == -1) {
doGet(req, resp);//调用
}else if (method.equals(METHOD_POST)) {
doPost(req, resp);
.......后面是诺干个if-else语句
从这个源代码中就可以看出Service()主要是干什么了吧-----他就是用来区分是get方法还是post方法的
2.doGet里面做了什么?
下卖个关子 , 请看这句代码对不对,会不会出现异常.....

答案是:一定会出现异常,原因就是你写了一个super.doGet();
这个会调用父类的doGet()方法 , 这样做的结果就是: 会出现405(请求错误)或者是illegalXXXException
Why? 为什么doget()不可以调用父类的方法?
原因就是:

这段源代码就是在告诉我们:无论如何,他都会调用requset.sendError方法的 ,
如果你还想在super.doGet()下面获取参数的话那就是错误的 ,因为sendError之后上下文已经改变了 , 你就没法获取参数了;
例如:
这个:

然后调用之后:

-------补录------------------------------------------------
/**
* Receives standard HTTP requests from the public
* <code>service</code> method and dispatches
* them to the <code>do</code><i>Method</i> methods defined in
* this class. This method is an HTTP-specific version of the
* {@link javax.servlet.Servlet#service} method. There's no
* need to override this method.
*
* @param req the {@link HttpServletRequest} object that
* contains the request the client made of
* the servlet
*
* @param resp the {@link HttpServletResponse} object that
* contains the response the servlet returns
* to the client
*
* @exception IOException if an input or output error occurs
* while the servlet is handling the
* HTTP request
*
* @exception ServletException if the HTTP request
* cannot be handled
*
* @see javax.servlet.Servlet#service
*/
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;
try {
ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
} catch (IllegalArgumentException iae) {
// Invalid date header - proceed as if none was set
ifModifiedSince = -1;
}
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);
}
}
} else if (method.equals(METHOD_HEAD)) {
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);
} else if (method.equals(METHOD_POST)) {
doPost(req, resp);
} else if (method.equals(METHOD_PUT)) {
doPut(req, resp);
} else if (method.equals(METHOD_DELETE)) {
doDelete(req, resp);
} else if (method.equals(METHOD_OPTIONS)) {
doOptions(req,resp);
} else if (method.equals(METHOD_TRACE)) {
doTrace(req,resp);
} else {
//
// Note that this means NO servlet supports whatever
// method was requested, anywhere on this server.
//
String errMsg = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
}
}
杂谈:Servlet(2)的更多相关文章
- 【杂谈】从CGI到Servlet
访问服务器的静态页面 每个Web服务器都运行着一个HTTP服务软件,用于响应web浏览器的请求,返回客户想要的页面.HTTP服务器都会有一个文件夹用于放置相关的页面文件,默认是 /user/loca ...
- (转)Servlet初始化、运行、销毁全部过程
Servlet初始化.运行.销毁全部过程 (2012-07-05 10:41:26) 标签: 杂谈 分类: java基础面试知识 Servlet的生命周期是由servlet的容器来控制的.分为3个阶段 ...
- servlet文件下载
创建web工程servlet,新建DownloadServlet.java package com.xmyself.servlet; import java.io.File; import java. ...
- java中servlet的各种路径
1. web.xml中<url-pattern>路径,(叫它Servlet路径!) > 要么以“*”开关,要么为“/”开头 2. 转发和包含路径 > *****以“/”开头:相 ...
- Servlet监听器笔记总结
监听器Listener的概念 监听器的概念很好理解,顾名思义,就是监视目标动作或状态的变化,目标一旦状态发生变化或者有动作,则立马做出反应. Servlet中的也有实现监听器的机制,就是Listene ...
- JavaWeb——Servlet
一.基本概念 Servlet是运行在Web服务器上的小程序,通过http协议和客户端进行交互. 这里的客户端一般为浏览器,发送http请求(request)给服务器(如Tomcat).服务器接收到请求 ...
- servlet 简介,待完善
什么是Servlet?① Servlet就是JAVA 类② Servlet是一个继承HttpServlet类的类③ 这个在服务器端运行,用以处理客户端的请求 Servlet相关包的介绍--javax. ...
- java web学习总结(五) -------------------servlet开发(一)
一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...
- servlet使用入门
创建web工程servlet,然后新建TestServlet.java package com.xmyself.servlet; import java.io.IOException; import ...
随机推荐
- BZOJ 2568 比特集合
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2568 题意:维护一个集合S,支持以下操作: (1)INS M : 将元素 M 插入 ...
- 手把手教你把Vim改装成一个IDE编程环境(图文)
http://blog.csdn.net/wooin/article/details/1858917
- Create Custom Modal Dialog Windows For User Input In Oracle Forms
An example is given below to how to create a modal dialog window in Oracle Forms for asking user inp ...
- windows tomcat 优化
windows tomcat 优化 1. tomcat conf server.xml 在server.xml中修改以一部分,增加节点数目,可以很好的提高性能: <Connector port ...
- 折半查找&clock函数
#include <stdio.h>#include <time.h> #define CLOCKS_PER_SEC ((clock_t)1000) int binsearch ...
- CUBRID学习笔记 25 数据类型2
---恢复内容开始--- 6枚举类型 语法 <enum_type> : ENUM '(' <char_string_literal_list> ')' <char_str ...
- poj 1410 线段相交判断
http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- HDU 3496 Watch The Movie(看电影)
HDU 3496 Watch The Movie(看电影) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] New sem ...
- iOS - OC NSDictionary 字典
前言 @interface NSDictionary<__covariant KeyType, __covariant ObjectType> : NSObject <NSCopyi ...
- XP_版本
1. Windows XP sp3 cd 和Windows XP sp3 cd vl的区别?VL的意思是大客户版,就是使用VL的KEY安装的系统是不需要激活的,不带VL的是安装完后需要激活的零售版 2 ...