HttpServlet概念与ServletContext对象
HttpServlet概念
创建类的时候继承HttpServle
实现步骤:
1)创建类继承HttpServlet类
2)覆盖doGet和doPost
3)在web.xml中进行servlet的配置
HttpServlet继承了GenericServlet,而GenericServlet实现Servlet接口, 所以我们可以继承HttpServlet来相当于创建一个Servlet
HttpServlet重写service()方法:
1.在该方法中先将ServletRequest和ServletResponse
强转为了HttpServletRequest和HttpServletResponse。
2.然调用重载的service()方法,并将刚刚强转得到对象传递到重载的方法中。
如果是GET请求,则调用doGet(HttpServletRequest request , HttpServletResponse response)
如果是POST请求,则调用doPost(HttpServletRequest request , HttpServletResponse response)
当通过继承HttpServlet来创建一个Servlet时,只需要根据要处理的请求的类型,来重写不同的方法。
处理get请求,则重写doGet()
处理post请求,则重写doPost()
ServletContext对象:
什么是 ServletContext?
ServletContext代表是一个web应用的环境(上下文)对象,
ServletContext对象一个web应用只有一个,但是一个web应用可以又多个Servlet对象!!!
ServletContext对象的生命周期?
web应用被卸载,服务器关闭
获得 ServletContext 如何获得?
ServletContext Context = getServletContext();
String getServletName() – 获取当前Servlet在web.xml中配置的名字
String getInitParameter(String name) –获取当前Servlet指定名称的初始化参数的值
根据相对路径获取绝对路径
package com.oracle.demo01; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Servlet01 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取servletContext对象 ServletContext context=getServletContext(); //获取相对于服务器的相对路径获取绝对路径 String patha=context.getRealPath("WEB-INF/classes/a.txt"); String pathb=context.getRealPath("b.txt"); String pathc=context.getRealPath("WEB-INF/c.txt"); System.out.println(patha); System.out.println(pathb); System.out.println(pathc); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
ServletContext是一个域对象,用来存储数据区域就是域对象
域对象的通用的方法:
setAtrribute(String name,Object obj);(往里存键值对 键只能是String)
getAttribute(String name);(根据建取值,取到的值是object类型,需要向下转型)
removeAttribute(String name);(移出)
package com.oracle.demo01; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Servlet02 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取ServletContext对象 ServletContext context=getServletContext(); //往ServletContext域中设置值 context.setAttribute("name","xiahouyuan"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package com.oracle.demo01; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Servlet03 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取ServletContext对象 ServletContext context=getServletContext(); //获取ServletContext域里面的值 String name=(String)context.getAttribute("name"); response.getWriter().write(name); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }