|
10Java金币
总提示The requested resource is not available,请大家看看哈
CountFilter代码:(src/com/cn/CountFilter.java)
package com.cn;
import java.io.IOException;
import javax.Servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
/**
* Servlet Filter implementation class CountFilter
*/
@WebFilter("/CountFilter")
public class CountFilter implements Filter {
private int count;
public void init(FilterConfig fConfig) throws ServletException {
String param=fConfig.getInitParameter("count");
count=Integer.valueOf(param);
}
public CountFilter() {
// TODO Auto-generated constructor stub
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
count++;
HttpServletRequest req=(HttpServletRequest)request;
ServletContext context=req.getSession().getServletContext();
context.setAttribute("count", count);
chain.doFilter(request, response);
}
public void destroy() {
// TODO Auto-generated method stub
}
}
web.xml代码:(WebContent/WEB-INF/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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>seventh</display-name>
<filter>
<filter-name>CountFilter</filter-name>
<filter-class>com.cn.CountFilter</filter-class>
<init-param>
<param-name>count</param-name>
<param-value>5000</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CountFilter</filter-name>
<url-pattern>/index.jsp</url-pattern>
< /filter-mapping>
<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>
index.jsp代码:(WebContent/index.jsp)
< %@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
< !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
< html>
< head>
< meta http-equiv="Content-Type" content="text/html; charset=GB18030">
< title>Insert title here</title>
< /head>
< body>
welcome!
< %=application.getAttribute("count") %>
< /body>
< /html>
谢了!
|
|