1、ServletContext:
(1)ServletContext在一个web应用中只有一个,但是Servlet能有多个;
(2)生命周期
创建:
当web应用已经部署到服务器时,服务器开启时被加载;
当web应用还未部署到服务器时,服务器开启后将web应用部署到服务器后被加载;
销毁:
服务器关闭时被销毁;
服务器处于开启状态,将web应用从服务器移除时被销毁;
(3)获取:
ServletConfig.getServletContext();等价于getServletContext();
getServletContext();
两个是不同的方法:
第一个是ServletConfig接口的方法,第二个是ServletContext类的方法;
但是获取的是同一个ServletContext对象,因为只有一个;一般用后一个方法比较多;
(4)获取全局的初始化参数:[注:param参数]
举个例子:在<display-name>WEB02</display-name>后面配置
<context-param> <param-name>aa</param-name> <param-value>aaa</param-value> </context-param>
然后调用ServletContext的getInitParameter("aa")方法,参数为<param-name>aa</param-name>的参数名字
package com.oracle.web; 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 ContexServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取ServletContext对象 ServletContext context = getServletContext(); String inti = context.getInitParameter("aa"); System.out.println(inti); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
(5)获取web应用的绝对路径:举个例子
绝对路径是从根目录开始,相对路径是从当前目录开始
在WEB02的WebContent下建立a.txt → ServletContext.getRealPath("a.txt")→D:javaEE omcat-7.0.52(x86)webappsWEB02a.txt
在WEB02的WebContent的WEB-INF下建立b.txt → ServletContext.getRealPath("WEB-INF/b.txt")→D:javaEE omcat-7.0.52(x86)webappsWEB02WEB-INF.txt
在WEB02的Java Resource的src下建立c.txt → ServletContext.getRealPath("WEB-INF/classes/c.txt")→D:javaEE omcat-7.0.52(x86)webappsWEB02WEB-INFclassesc.txt
在WEB02下建立d.txt,无法访问;
package com.oracle.web;
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 ContexServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取ServletContext对象
ServletContext context = getServletContext();
String inti = context.getInitParameter("aa");
System.out.println(inti);
//获取web应用中的资源的绝对路径
String patha = context.getRealPath("a.txt");
String pathb = context.getRealPath("WEB-INF/b.txt");
String pathc = context.getRealPath("WEB-INF/classes/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);
}
}
(6)域对象:Servlet有四大域对象
存储数据的区域就是域对象[数据可以共享];
域对象的通用方法:
setAttribute(str,obj)注意向下转型
getAttribute(str);
removeAttribute(str);
举个例子:在Servlet02设置值
package com.oracle.web; 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(); context.setAttribute("name", "lisi"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
在Servlet03取值;
package com.oracle.web; 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 context = getServletContext(); String name= (String) context.getAttribute("name"); System.out.println(name); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
先访问下Servlet02
再访问Servlet03
则在console打印:
2、HttpServletResponse
(1)write方法是将内容写到response的缓冲区;
(2)设置响应行:setStatus(sc);sc = 200/302/304/404/500
(3)设置响应头:addHeather(name,value)不常用;添加
package com.oracle.Servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HeaderServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setStatus(500); response.addHeader("qipa", "fushouer"); response.getWriter().write("hello dandan..."); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
setHearder(name,value)常用;设置
package com.oracle.Servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class RefreshServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //定时刷新功能 response.setHeader("refresh", "5;url=http://www.baidu.com"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
(4)重定向:请求两次;url地址改变;
第一种方法:设置状态码为302,设置响应头为("location","/xxx地址");
package com.oracle.Servlet; import java.io.IOException; 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 { //重定向 //1、设置状态码302 response.setStatus(302); //2、设置Location头 response.setHeader("Location", "/WEB03/Servlet"); response.getWriter().write("hello 123"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package com.oracle.Servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Servlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().write("hello dandan..."); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
第二种方法:调用方法response.sendRedirect("/WEB03/Servlet");[/不要掉下];
package com.oracle.Servlet; import java.io.IOException; 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 { //重定向 //在实际开发中不会设置头 response.sendRedirect("/WEB03/Servlet"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package com.oracle.Servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Servlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().write("hello dandan..."); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
(5)中文乱码问题:[键入下列语句]
response.setContentType("text/html;charset=UTF-8");
package com.oracle.Servlet; import java.io.IOException; 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 { //response解决中文乱码问题 //response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("Hello世界"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package com.oracle.Servlet; import java.io.IOException; 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 { //response解决中文乱码问题 response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("Hello世界"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }