1:ServletContext对象是一个为整个web应用提供共享的内存,任何请求都可以访问里面的内容
2:如何实现在服务启动的时候就动态的加入到里面的内容:我们需要做的有:
1) 实现servletContextListerner接口 并将要共享的通过setAttribute(name,data)方法提交到内存中去
2)应用项目在通过getAttribute(name)将数据或到
在配置文件中加入
这样监视器就设置好了
以下通用应用调用上面的数据
Session和request也可以设置相应的监视器来处理数据
2:如何实现在服务启动的时候就动态的加入到里面的内容:我们需要做的有:
1) 实现servletContextListerner接口 并将要共享的通过setAttribute(name,data)方法提交到内存中去
2)应用项目在通过getAttribute(name)将数据或到
package ServletContextTest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import util.ConnectTool;
public class ServletContextLTest implements ServletContextListener{
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("this is last destroyeed");
}
public void contextInitialized(ServletContextEvent sce) {
ServletContext sct=sce.getServletContext();
Map<Integer,String> depts=new HashMap<Integer,String>();
Connection connection=null;
PreparedStatement pstm=null;
ResultSet rs=null;
try{
connection=ConnectTool.getConnection();
String sql="select deptNo,dname from dept";
pstm=connection.prepareStatement(sql);
rs=pstm.executeQuery();
/*put the data into the deptNames */
while(rs.next()){
depts.put(rs.getInt(1), rs.getString(2));
}
sct.setAttribute("dept", depts);
System.out.println("======listener test is beginning============================");
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectTool.releasersc(rs, pstm, connection);
}
}
}
在配置文件中加入
<listener>
<listener-class>ServletContextTest.ServletContextLTest</listener-class>
</listener>
这样监视器就设置好了
以下通用应用调用上面的数据
package ServletContextTest;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
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 CreateEmployee extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext sct=getServletConfig().getServletContext();
Map<Integer,String> dept=(Map<Integer,String>)sct.getAttribute("dept");
Set<Integer> key=dept.keySet();
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<form action='/register' action='post'>");
out.println("<table alignb='center'>");
out.println("<tr>");
out.println("<td>");
out.println("username:");
out.println("</td>");
out.println("<td>");
out.println("<input type='text' name='username'");
out.println("</tr>");
out.println("<tr>");
out.println("<td>");
out.println("city:");
out.println("</td>");
out.println("<td>");
out.println("<select name='dept'");
for(Integer i:key){
out.println("<option value='"+i+"'>"+dept.get(i)+"</option>");
}
out.println("</select>");
out.println("</td>");
out.println("<tr>");
out.println("</table>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
out.flush();
}
}
Session和request也可以设置相应的监视器来处理数据