java web hello world(二)基于Servlet理解监听

java web最开始实现是通过Servlet实现,这里就来实现下,最原始的监听是如何实现的。

第一步,创建一个基本的web项目 ,参见(java web hello world(一)

第二步,web.xml添加配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>WebProject1</display-name>
  
<!--       <filter>
        <filter-name>myfilter</filter-name>
        <filter-class>sun.fifter.MyFifter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>myfilter</filter-name>
        <url-pattern>*</url-pattern>
    </filter-mapping> -->
      <servlet>
        <servlet-name>MyFirstServlet</servlet-name>
        <servlet-class>sun.fifter.MyFirstServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyFirstServlet</servlet-name>
        <url-pattern>/MyFirstServlet</url-pattern>
    </servlet-mapping>
    
    
    <listener>
      <listener-class>sun.fifter.MyListener</listener-class>
  </listener>
  
    
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

第三步:创建一个Servlet用来实现控制和转发。

package sun.fifter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MyFirstServlet extends HttpServlet
{

    public MyFirstServlet()
    {
        // TODO Auto-generated constructor stub
    }
    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException 
    {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            RequestDispatcher dispatcher = request.getRequestDispatcher("index.html"); 
            HttpSession session = request.getSession();
            session.setAttribute("data", "szy");
            String sessionId = session.getId();
            if (session.isNew()) {
                response.getWriter().print("session创建成功,session的id是:"+sessionId);
            }else {
                response.getWriter().print("服务器已经存在该session了,session的id是:"+sessionId);
             }
            dispatcher .forward(request, response); 
        } finally {
            out.close();
        }
        

    }
    
    @Override
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        //Do some other work
    }
 
    @Override
    public String getServletInfo() {
        return "MyFirstServlet";
    }

}

第四步:创建一个类用来监听,用来监听的类要去实现一个监听的接口,

如,下面的代码是用来监听session的创建和注销的,作用可用于统计登录人数等。

package sun.fifter;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MyListener implements HttpSessionListener{
    private int userNumber = 0;

    @Override
    public void sessionCreated(HttpSessionEvent arg0)
    {
        // TODO Auto-generated method stub
        userNumber++;
        System.out.println("在线人数"+String.valueOf(userNumber));
        arg0.getSession().setAttribute("userNumber", userNumber);
        
    }
    @Override
    public void sessionDestroyed(HttpSessionEvent arg0)
    {
        // TODO Auto-generated method stub
        userNumber--;
        System.out.println(String.valueOf(userNumber));
        arg0.getSession().setAttribute("userNumber", userNumber);
        
    }
}

以上,运行结果:

看控制台输出:

以上便是监听的简单实例

 

posted on 2017-03-27 13:30  手撕高达的村长  阅读(186)  评论(0编辑  收藏  举报

导航