package com.javaweb;
import java.io.IOException;
import javax.management.modelmbean.RequiredModelMBean;
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;
import com.sun.net.httpserver.Filter.Chain;
public class FilterTest implements Filter {
public void destroy() {
// TODO Auto-generated method stub
System.out.println("销毁");
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest request=(HttpServletRequest)arg0;
ServletResponse response=(ServletResponse)arg1;
FilterChain chain=(FilterChain)arg2;
request.setCharacterEncoding("gb2312");
String charContent=request.getParameter("charContent");
if(charContent!=null)
{
if(charContent.indexOf("晕")==-1)
{
chain.doFilter(request, response);
}
else
{
request.getRequestDispatcher("SendFailure.jsp").forward(request, response);
}
}
else
{
chain.doFilter(request, response);
}
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
System.out.println("初始化");
}
}
CharForm.jsp
<%@ page language="java" import="java.util.*" contentType="text/html;charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<form action="ShowContent.jsp" method="post">
账号:<input type="text" name="username"/><br>
发信:<br>
<textarea name="charContent" rows="20" cols="40"></textarea><br>
<input type="submit" value="发送"/>
<input type="reset" value="重置"/>
</form>
</body>
</html>
SendFailure.jsp
<%@ page language="java" import="java.util.*" contentType="text/html;charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<h1>发言失败</h1>
</body>
</html>
ShowContent.jsp
<%@ page language="java" import="java.util.*" contentType="text/html;charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<center>
<h1>发言成功</h1>
账号:${param.username }<br>
内容:${param.charContent }<br>
</center>
</body>
</html>