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应用方案.随着 ...
随机推荐
- iOS取得AddressBook联系人信息
新建一个CContact类用于存放联系人信息,下面是该类的代码: CContact.h代码: 01 #import <Foundation/Foundation.h> 02 03 ...
- 通过angularJS官方案例快速入门
官方案例-angular-phonecat angularJS官方提供了一个官方案例给大家进行循序渐进的学习,但是如果之前没有接触过node.js以及git的同学这个案例拿着也无从下手-这里就介绍一下 ...
- GJM:用C#实现网络爬虫(二) [转载]
上一篇<用C#实现网络爬虫(一)>我们实现了网络通信的部分,接下来继续讨论爬虫的实现 3. 保存页面文件 这一部分可简单可复杂,如果只要简单地把HTML代码全部保存下来的话,直接存文件就行 ...
- CSS代码记录
1. 内容横向滚动的代码 .ul { display: box; display: -webkit-box; width: 250px; background: yellow; overflow-y: ...
- ie7下<a></a>标签不反应
view中: <a href="Trading?id=@dr["id"]"> <div class="sy_img_div" ...
- 封装Nvelocity的渲染方法
public class CommonHelper { /// <summary> /// 用data数据填充templatename模板,渲染返回html返回 /// </summ ...
- 如何排查sharepoint2010用户配置文件同步服务启动问题
用户配置文件同步服务与 Microsoft Forefront Identity Manager (FIM) 交互,以与外部系统(如目录服务和业务系统)同步配置文件信息.启用用户配置文件同步服务时,将 ...
- Arcgis创建SDE_Geometry、SDO_Geometry的区别【转】
1. SDO_GEOMETRY Oracle Spatial在MDSYS模式下定义了一系列几何类型.函数来支持空间数据的存储和使用,最为人耳熟能详的就是SDO_GEOMETRY这种类型——当然,Arc ...
- 之一:CABasicAnimation - 基本动画
嗷呜嗷呜嗷呜 // 将视图作为属性方便后面执行多个不同动画 _myView = [[UIView alloc] init]; _myView.layer.position = CGPointMake( ...
- Android 在C代码中调用logcat
本文给<Android java传递int类型数组给C>中添加C代码中调用logcat的功能 Android.mk文件增加以下内容 LOCAL_LDLIBS += -llog C代码中增加 ...