web.xml
?
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
?xmlns="http://java.sun.com/xml/ns/javaee"
?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
?xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
?http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
? <welcome-file-list>
??? <welcome-file>index.jsp</welcome-file>
? </welcome-file-list>
?
?
?
? <listener>
?? <listener-class>
??? com.huawei.forest.CallQueueLisener
??? </listener-class>
? </listener>
?
? <servlet>
???? <servlet-name>loginServlet</servlet-name>
???? <servlet-class>com.huawei.forest.LoginServlet</servlet-class>
???? <load-on-startup>0</load-on-startup>
? </servlet>
? <servlet-mapping>
??? <servlet-name>loginServlet</servlet-name>
??? <url-pattern>/login</url-pattern>
? </servlet-mapping>
?
?
? <filter>
???? <filter-name>auditFilter</filter-name>
???? <filter-class>com.huawei.forest.AuditFilter</filter-class>
? </filter>
?
? <filter-mapping>
????? <filter-name>auditFilter</filter-name>
????? <url-pattern>*.do</url-pattern>
? </filter-mapping>
?
</web-app>
?
LoginServlet.java
?
package com.huawei.forest;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
?/**
? *
? */
?private static final long serialVersionUID = 1L;
?@Override
?protected void doGet(HttpServletRequest req, HttpServletResponse resp)
???throws ServletException, IOException {
??// TODO Auto-generated method stub
??super.doGet(req, resp);
?}
?@Override
?protected void doPost(HttpServletRequest req, HttpServletResponse resp)
???throws ServletException, IOException {
??String username = req.getParameter("username");
??String password = req.getParameter("password");
??resp.setContentType("text/html;charset=gb2312");
??PrintWriter? out = resp.getWriter();
??out.print("<html><body>");
??if (username.equals("admin") && password.equals("admin"))
??{
???out.print(" you login in successful!..."+username);
??}
??else
??{
???out.print(" sorry,you login failed!..."+username);
??}
??out.print("</body></html>");
??
?}
?@Override
?public void init() throws ServletException {
??// TODO Auto-generated method stub
??System.out.println("The servlet is init.....");
?}
}
AuditFilte.java
?
package com.huawei.forest;
import java.io.IOException;
import java.util.Date;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
public class AuditFilter implements Filter{
?private FilterConfig config;
?public void destroy() {
??// TODO Auto-generated method stub
??this.config= null;
?}
?public void doFilter(ServletRequest request, ServletResponse response,
???FilterChain chain) throws IOException, ServletException {
//??HttpServletRequest httpRequest = (HttpServletRequest)request;
//??String username = httpRequest.getRemoteUser();
??System.out.println("The request will be filted.....");
//??if (username !=null)
//??{
//???config.getServletContext().log("User:"+username+"\nTime:"+new Date());
//??}
//??chain.doFilter(httpRequest, response);
?}
?public void init(FilterConfig arg0) throws ServletException {
??// TODO Auto-generated method stub
??System.out.println("the filter is starting.......");
?}
}
?
?
callqueuelicenser.java
?
?
package com.huawei.forest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class CallQueueLisener implements ServletContextListener{
?public void contextDestroyed(ServletContextEvent sce) {
??System.out.println("The listener is destroying.........");
?}
?public void contextInitialized(ServletContextEvent sce) {
??System.out.println("The listener is starting.........");
??
//??try {
//???InputStream inputFile = this.getClass().getClassLoader().getResourceAsStream("vpn.properties");
//???System.out.println(inputFile.toString());
//???Properties pro=new Properties();
//???pro.load(inputFile);
//???inputFile.close();
//???System.out.println(pro.getProperty("seeLocation"));
//???
//???File f =new File("vpn.properties");
//???if (f.exists())
//???{
//????System.out.println("0000000");
//????f.delete();
//???}
//???FileOutputStream fos =new FileOutputStream("1234.properties");
//???
//???
//???pro.setProperty("NewFile", "----------------------");
//???System.out.println(pro.getProperty("seeLocation"));
//???pro.store(fos, null);
//???fos.flush();
//????? fos.close();
//???
//??} catch (FileNotFoundException e1) {
//???// TODO Auto-generated catch block
//???System.out.println("------------------------");
//???e1.printStackTrace();
//??} catch (IOException e) {
//???// TODO Auto-generated catch block
//???e.printStackTrace();
//??}
//??
//??Timer timer = new Timer();
//??timer.scheduleAtFixedRate(new MyTask(), 0, 1000);
?
?}
?
}
?
?
?
?
?
?
?
?
index.jsp
?
?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
?<head><title>This is just a test jsp...</title></head>
?<script type="text/javascript">????
??? window.onbeforeunload = onbeforeunload_handler;????
??? window.onunload = onunload_handler;????
??? function onbeforeunload_handler(){???? //监听IE关闭时间
??????? var warning="确认退出?";????????????
??????? return warning;
??????? var n = window.event.screenX - window.screenLeft;??
??????? var b = n > document.documentElement.scrollWidth-20;??
??????? if(b && window.event.clientY < 0 || window.event.altKey){??
??????? alert("是关闭而非刷新");??
????? window.event.returnValue = "";?????? }else{??
??????? alert("是刷新而非关闭");??
???????????
??? }????
????????
?? function onunload_handler(){????
??????? var warning="谢谢光临";????
??????? alert(warning);????
??? }????
??
</script>
?<body>
??<a>This jsp is to test the IE lisener.....</a>
??<form action="login" method="post">
????? <table>
??????? <td><input type="text" name="username" size=15/></td>
??????? <td><input type="password" name="password" size=15/></td>
??????? <input type="submit" value="login"/>
??????? <input type="reset" value="cancel"/>
????? </table>
??</form>
?</body>
</html>
?
?
?
?
b
啊!
1