Servlet:处理请求和发送响应的过程是由一种叫做Servlet的程序来完成的,通过servlet可以实现动态web开发。
<!-- *号通配符格式:"*.扩展名" or "/{path}/*" --> <servlet> <servlet-name>ServletDemo1</servlet-name> <servlet-class>gacl.servlet.study.ServletDemo1</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletDemo1</servlet-name> <url-pattern>/*</url-pattern>
1. 响应过程
Java Servlet API(Servlet <– HttpServlet):Servlet容器(tomcat)和servlet之间的接口,定义了serlvet的各种方法,以及作为参数的对象类(如:ServletRequest和ServletResponse)
1. Tomcat将http请求文本接收并解析,然后封装成HttpServletRequest类型的request对象交给servlet(所有的HTTP头数据读可以通过request对象调用对应的方法查询到)
2.Tomcat同时会要响应的信息封装为HttpServletResponse类型的response对象,通过设置response属性就可以控制要输出到浏览器的内容,然后将response交给tomcat,tomcat就会将其变成响应文本的格式发送给浏览器
2. servlet生命周期
1. load-on-startup参数 > 决定装载并创建Servlet实例对象的时机,同时调用init()方法初始化该 Servlet(对于多次请求,init()方法只执行一次
2. 执行service(ServletRequest req,ServletResponse res)方法,根据请求方法不同调用doGet() 或者 doPost()等。
3. 服务器关闭时,执行destroy()方法,并将servlet卸载。
3. Servlet结构
1. Servlet接口
2. ServletConfig接口
3. GenericServlet类
GenericServlet类: 实现了ServletConfig接口,可以直接获得ServletContext
public void init(ServletConfig config) throws ServletException{ this.config = congid; this.init(); } public void init() throws ServletException{ //override(用户只需重写init()方法,而当servlet初始化时,将会调用init(ServletConfig config)方法. }
4. HttpServlet类
Servlet* 继承结构(*表示Request或Response):
Servlet* <– httpServlet* <– *Facade(req,res的实际类型)
1. request和response在Tomcat提交给Servlet时被转换为Servlet* 类型,通过service(ServletRequest req, ServletResponse res)方法转换为HttpServlet*
2. service(HttpServletRequest req, HttpServletResponse res)方法进行调用(doPost(),doGet()等)
5. ServletContext对象
config = getServletConfig(); //多用于Servlet的init(ServletConfig config)方法中 config.getInitParameter("key")和config.getInitParameterNames(); ServletContext context1 = config.getServletContext(); ServletContext context2 = this.getServletContext(); //继承自GenericServlet类的方法,GenericServlet实现了Servletconfig类 context.getRequestDispatcher("/servlet/Servlet1").forward(request, response); //调用forward方法转发给servlet1.(绝对路径) context.getInitParameter("key")和context.getInitParameterNames();
1. web项目中共享数据
setAttribute(String name, Object obj) 在web项目范围内存放内容,以便让在web项目中所有的servlet读能访问到
getAttribute(String name) 通过指定名称获得内容
removeAttribute(String name) 通过指定名称移除内容
2. web项目初始化参数 — 在web.xml的context-param>标签中配置
getInitPatameter(String name) //通过指定名称获取初始化值
getInitParameterNames() //获得枚举类型
3. 获取web项目资源
String realPath = getServletContext().getRealPath(“/WEB-INF/web.xml”);
String src = getResourcePaths(“/WEB-INF’);
InputStream in = getServletContext().getResourceAsStream(“/WEB-INF/web.xml”);
//servlet实例 import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletDemo1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }
原文地址:https://www.cnblogs.com/kiqi/p/10456509.html