Servlet Class2
1.ServletContext接口
ServletContext代表是一个web应用的环境(上下文)对象,ServletContext对象 内部封装是该web应用的信息,ServletContext对象一个web应用只有一个,ServletContext在该web项目被加载时创建,在web项目卸载时或者服务器关闭时被销毁。
通过实例化一个ServletContext实现类对象,我们可以设置全局变量或者获取配置文件。
2.应用
(1)初始化参数设置与获取
实例化ServletContext对象:
ServletContext servletContext = config.getServletContext(); ServletContext servletContext = this.getServletContext();
在XML设置初始化参数:
<context-param> <param-name>JDBC</param-name> <param-value>com.mysql.jdbc.Driver</param-value> </context-param>
获取参数:
String name = context.getInitParameter("JDBC");
(2)全局属性设置与获取
context.setAttribute("user", "admin");
String user = context.getAttribute("user");
ServletContext域对象的作用范围:整个web应用(所有的web资源都可以随意向 servletcontext域中存取数据,数据可以共享)
(3)获取web应用内配置文件的绝对路径
String path1 = context.getRealPath("123.txt"); String path2 = context.getRealPath("456.txt"); String path3 = context.getRealPath("789.txt");
不论该文件在web应用内的那个文件夹下,都能被获取到,可用于获取配置文件。
3.response响应
服务器接收到一个请求时,会生成一个响应对象,即response,我们可以通过response来实现不同的操作。
(1)响应html代码
public class LoginServlet extends HttpServlet{ @Override protected void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException { String user = request.getParameter("user"); String pwd = request.getParameter("pwd"); String result = null; if(user.equals("admin")&pwd.equals("123456")) result= "<h3 style='color:green'>登录成功</h3>"; else result="<h3 style='color:red'>账户或密码错误</h3>"; response.getWriter().println(result); } }
(2)设置响应头
public class ResponseServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.addHeader("Adam", "wink"); response.addIntHeader("GOD", 123); response.addDateHeader("when", 19820052566222L); String html = "www.baidu.com"; response.setStatus(302); response.setHeader("Location", html);//实现页面302跳转 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=UTF-8"); response.getWriter().print("<h2>页面将在5s后跳转至百度!</h2>"); response.setHeader("refresh", "5;url=http://www.baidu.com");//定时自动跳转 }
(3)页面跳转:
客户端跳转:
两次请求,第一次为请求servlet的虚拟路径,第二次为请求html页面
两次响应,第一次为302,页面跳转,第二次为200,跳转页面加载
String html = "www.baidu.com"; response.setStatus(302); response.setHeader("Location", html);//实现页面302跳转
服务端跳转:
一次请求,请求servlet,页面是在服务器内部打开
一次响应,200,跳转页面打开
服务端跳转不会改变当前的url,是一次请求,所以可以实现请求值的传递
public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); String password = request.getParameter("password"); if ("admin".equals(name) && "123".equals(password)) { request.getRequestDispatcher("success.html").forward(request, response); } } }