Servlet/JSP-03 HttpServlet
一. GenericServlet
GenericServlet本身是一个抽象类,并且实现了Servlet和ServletConfig接口
其在内部定义了一个私有的ServletConfig类型的变量config,并在init(ServletConfig config)为其赋值,然后通过config实现其他方法。
service(ServletRequest servletrequest, ServletResponse servletresponse)为抽象方法。具体代码如下:
public abstract class GenericServlet implements Servlet, ServletConfig, Serializable
{
private transient ServletConfig config; public GenericServlet() { }
@override
public void init(ServletConfig config) throws ServletException
{
this.config = config;
init();
} // 此方法供我们实现的Servlet初始化时使用
public void init() throws ServletException {}
@override
public ServletConfig getServletConfig()
{
return config;
}
@override
public String getInitParameter(String name)
{
return getServletConfig().getInitParameter(name);
} public abstract void service(ServletRequest servletrequest, ServletResponse servletresponse)
throws ServletException, IOException; // 其他方法略
... }
二. HttpServlet
为了简化开发,Servlet容器提供了HttpServlet的实现,其继承了GenericServlet.
1. Servlet容器为我们封装了HttpServletRequest, HttpServletResponse类型的参数,传入service方法,在service方法内强转后传入对应的doXXX方法
2. 开发时继承HttpServlet,只需要重写doXXX方法即可,处理相应方式的HTTP请求。
public abstract class HttpServlet extends GenericServlet
{
// doXXX方法被子类覆盖
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(405, msg);
else
resp.sendError(400, msg);
} //doPost, doPut, doDelete类似实现, doHead, doTrace, doOption略复杂
...... //通过此方法调用不同HTTP请求方式的处理方法doXXX
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String method = req.getMethod();
if(method.equals("GET"))
{
long lastModified = getLastModified(req);
if(lastModified == -1L)
{
doGet(req, resp);
} else
{
long ifModifiedSince;
try
{
ifModifiedSince = req.getDateHeader("If-Modified-Since");
}
catch(IllegalArgumentException iae)
{
ifModifiedSince = -1L;
}
if(ifModifiedSince < (lastModified / 1000L) * 1000L)
{
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else
{
resp.setStatus(304);
}
}
} else
if(method.equals("HEAD"))
{
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);
} else
if(method.equals("POST"))
doPost(req, resp);
else
if(method.equals("PUT"))
doPut(req, resp);
else
if(method.equals("DELETE"))
doDelete(req, resp);
else
if(method.equals("OPTIONS"))
doOptions(req, resp);
else
if(method.equals("TRACE"))
{
doTrace(req, resp);
} else
{
String errMsg = lStrings.getString("http.method_not_implemented");
Object errArgs[] = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(501, errMsg);
}
} //此为GenericServlet中方法,最终调用HttpServlet内实现的service方法
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);
} }
Servlet/JSP-03 HttpServlet的更多相关文章
- jsp使用c:forEach报错 javax.servlet.jsp.PageContext.getELContext()Ljavax/el/ELContext的问题
今天发现了一个折磨我一天的问题: 在jsp文件中使用 <c:forEach items="${checkResult}" var="item"> & ...
- Struts框架——(一)用Servlet + JSP演示Struts基本原理
一. 用Servlet + JSP演示Struts基本原理 struts是开源项目.它通过采用 Java Servlet/JSP 技术,实现了基于Java EE Web应用的MVC的应用框架.Stru ...
- Javabean+servlet+JSP(html)实例应用
大家都知道Javabean+servlet+JSP是最简单的MVC模式.的确,在一个小型的项目中,这个模式完全够用. 它优雅并且简洁.加上jQueryui的完美展示效果,让这个模式看起来非常合适.当然 ...
- 报错:严重: Servlet.service() for servlet [jsp] in context with path [/20161116-Struts2-6] threw exception [/index.jsp (line: 13, column: 20) No tag "textfiled" defined in tag library imported with prefix
严重: Servlet.service() for servlet [jsp] in context with path [/20161116-Struts2-6] threw exception [ ...
- servlet+jsp+java实现Web 应用
servlet+jsp+java实现Web 应用 用java来构建一个web应用是特别容易的事情,jsp和php很像,可以嵌套在html中.程序的结构很简单,也很清楚,本文主要记录下大概的开发过程和环 ...
- javaweb学习总结(二十二)——基于Servlet+JSP+JavaBean开发模式的用户登录注册
一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...
- 在Eclipse中配置Tomcat 创建和运行Servlet/JSP
在Eclipse中配置Tomcat 创建和运行Servlet/JSP 步骤一:在Eclipse中配置Tomcat(注意下载Eclipse IDE for Java EE Developers) (1) ...
- Servlet&jsp基础:第五部分
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- Servlet&jsp基础:第一部分
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- Servlet&JSP中的知识点
先sun提出的是Servlet体系,这个体系使得使用JAVA的程序员也能开发基于B/S架构的WEB应用程序,使用Servlet类将HTTP请求和响应封装在标准JAVA类中来实现各种WEB应用方案.随着 ...
随机推荐
- [moka同学笔记]Yii2.0显示页匿名函数设置$value
匿名函数设置$value <?= GridView::widget([ 'dataProvider' => $dataProvider, 'columns' => [ // ['cl ...
- linux实用的日志分析脚本
日志分析 随意的tail一个access_log文件,下面是一条经典的访问记录 218.19.140.242 – - [10/Dec/2010:09:31:17 +0800] “GET /query/ ...
- ecshop适应PHP7的修改
说实话,ecshop这个系统,到目前也没见怎么推出新版本,如果是新项目,不太建议使用它.不过,因为我一直以来都在使用中,所以不得不更改让其适应PHP新版本.现在PHP 7已经出发行版了,所以更改来继续 ...
- Sending e-mail
E-mail functionality uses the Apache Commons Email library under the hood. You can use theplay.libs. ...
- 使用 HTML5 WebGL 实现逼真的云朵效果
这里给大家展示一个使用 HTML5 WebGL 实现超逼真的云朵效果.WebGL 是一项在网页浏览器呈现3D画面的技术,有别于过去需要安装浏览器插件,通过 WebGL 的技术,只需要编写网页代码即可实 ...
- jQuery的eq方法
定义和用法eq() 方法将匹配元素集缩减值指定 index 上的一个. 语法.eq(index) 其中的index :整数,指示元素的位置(最小为 0).如果是负数,则从集合中的最后一个元素往回计数. ...
- Spring(2) ------ 依赖注入
spring框架为我们提供了三种注入方式,分别是set注入,构造方法注入,接口注入. 1.set注入: 采用属性的set方法进行初始化,就成为set注入. 1)给普通字符类型赋值. public cl ...
- 刀锋上前行!绕过Ramint蠕虫病毒直接脱壳
系统 : Windows xp 程序 : 某游戏客户端 程序下载地址 :不提供 要求 : 脱去压缩壳 使用工具 : OD & PEID & LordPE & Import RE ...
- firefox中flash经常崩溃
建议: 1.安装flashblck插件 2.添加配置文件 在C:\Windows\SysWOW64\Macromed\Flash添加mmc.cfg. mmc.cfg的内容: SlientAutoUpd ...
- CP强制覆盖
发现在Fedora 10 /ubutun 里面用cp -fr src dest,即使加了-f也是不能强行覆盖的,这时怎么回事的呢?一两个文件还好说,就输几个yes吧,但是要是n多文件怎么办,那还不输死 ...