Java Web

来源:互联网 发布:域名跟网址的区别 编辑:程序博客网 时间:2024/05/21 10:56

1. ServletContext

Web 应用中 servlet 可以使用 servlet 上下文得到:
1. 在调用期间保存和检索属性的功能,并与其他 servlet 共享这些属性。
2. 读取 Web 应用中文件内容和其他静态资源的功能。
3. 互相发送请求的方式。
4. 记录错误和信息化消息的功能。

WEB 容器在启动时,它会为每个 WEB 应用程序都创建一个对应的 ServletContext 对象,它代表当前 web 应用。
ServletConfig 对象中维护了 ServletContext 对象的引用。
WEB 应用中的所有 Servlet 共享同一个 ServletContext 对象,因此 Servlet 对象之间可以通过 ServletContext 对象来实现通讯。


1.. 多个 Servlet 通过 ServletContext 对象实现数据共享
利用 ServletContext 对象存入需要共享的数据,相当于全局对象

// 获取 ServletContext 对象  ServletContext context = this.getServletContext();   // 存入共享的数据    context.setAttribute("name", "haha");

在其它的 Servlet 中利用 ServletContext 对象获取共享的数据

// 获取共享的数据   String name = this.getServletContext().getAttribute("name"); 

2.. 读取资源文件

String path =context.getRealPath("WEB-INF/classes/db.properties");

3.. 转发

ServletContext sc=getServletContext(); RequestDispatcher rd=sc.getRequestDispatcher("/a").forward(req,resp);

servletContext 是实现监听的基础

2. 域对象属性变更监听

三个域对象监听接口:

ServletContextAttributeListener; HttpSessionAttributeListener;ServletRequestAttributeListener;
  • 1) attributeAdded

当向被监听对象中增加一个属性时,web 容器就调用事件监听器的 attributeAdded 方法进行响应,这个方法接收一个事件类型的参数,监听器可以通过这个参数来获得正在增加属性的域对象和被保存到域中的属性对象:
如:
OneServlet.java:

public class OneServlet extends HttpServlet{    protected void doPost(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {        String username = req.getParameter("username");        String password = req.getParameter("password");        System.out.println(username+"-"+password);        if(username.equals(password)){            String host = req.getRemoteHost();            User user = new User(username, host, req.getSession());            req.getSession().setAttribute("user", user);    //session添加属性            resp.sendRedirect("/day09/info.jsp");        }else            resp.sendRedirect("/day09/index.jsp");        }}

MyHttpSessionListener.java:

public class MyHttpSessionListener implements HttpSessionListener, HttpSessionAttributeListener {        //监听session属性添加事件public void attributeAdded(HttpSessionBindingEvent arg0) {        HttpSession session = arg0.getSession();        User user = (User)arg0.getSession().getAttribute("user");        Map<String,User> map = (Map<String,User>)arg0.getSession().getServletContext().getAttribute("online");        if(map == null){            map = new HashMap<String, User>();        }        map.put(session.getId(), user);        //servlet上下文对象添加online的用户表,共享数据        arg0.getSession().getServletContext().setAttribute("online", map);}}

MyServletContextAttributeListener.java:

public class MyServletContextAttributeListener implements        ServletContextAttributeListener {        //监听servletContext属性变化    public void attributeAdded(ServletContextAttributeEvent scab) {        String str =MessageFormat.format(                "ServletContext域对象中添加了属性:{0},属性值是:{1}"                ,scab.getName()                ,scab.getValue());        System.out.println(str);    }}
  • attributeRemove、attributeReplace 同理

3.XML 配置

<!-- 注册针对ServletContext对象进行监听的监听器 -->   <listener>       <description>ServletContextListener监听器</description>       <listener-class>me.yuanfang.listener.MyServletContextListener</listener-class>   </listener><!-- session销毁时间(分钟) --><session-config>    <session-timeout>1</session-timeout> </session-config>
原创粉丝点击