建立和使用一个上下文监听者(Head first servlet+Jsp)(实验二)
1.创建一个监听者类
package com.example; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class MyServletContextListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent event) { } @Override public void contextInitialized(ServletContextEvent event) { ServletContext sc = event.getServletContext();//由事件得到ServletContext String dogBreed = sc.getInitParameter("breed");//用上下文得到初始化参数 Dog d = new Dog(dogBreed); sc.setAttribute("dog", d); //使用上下文来设置一个属性(名/对象对),这个属性都是Dog,现在应用的其他部分就能得到这个属性(Dog)的值了 } }
2.属性类
package com.example; public class Dog { private String breed; public Dog(String breed) { this.breed = breed; } public String getBreed() { return breed; } // public void setBreed(String breed) { // this.breed = breed; // } }
3.Servlet
package com.example; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ListenerTest */ @WebServlet("/ListenerTest") public class ListenerTest extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ListenerTest() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("test context attributes set by Listener!<br>"); out.println("<br>"); Dog dog = (Dog) getServletContext().getAttribute("dog"); //现在从ServletContext得到Dog,如果监听者正常,第一次调用服务方法之前Dog就已经放在上下文之中了。 out.println("dog's breed is:"+dog.getBreed()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ // protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // // TODO Auto-generated method stub // doGet(request, response); // } }
4.编写DD文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>FirstListener</display-name> <servlet> <servlet-name>ListenerTest</servlet-name> <servlet-class>com.example.ListenerTest</servlet-class> </servlet> <servlet-mapping> <servlet-name>ListenerTest</servlet-name> <url-pattern>/ListenTest.do</url-pattern> </servlet-mapping> <context-param> <param-name>breed</param-name> <param-value>Greet Dane</param-value> </context-param> <listener> <listener-class>com.example.MyServletContextListener</listener-class> </listener> </web-app>
测试如下: