过滤器链实现中文转码和文字屏蔽功能
编写一个过滤器链实现如下功能:
1. 编写过滤器程序EncodingFilter,实现POST方式下的中文转码。
2. 编写过滤器程序liuyanfilter,实现将用户输入的用户名“张”转换成“李”,将用户输入的留言内容“剑桥”转换成“****”。
程序其他功能页面:
1. 编写liuyanform.jsp该页面用于完成表单的输入,其中包括:用户名、留言内容、提交按钮。
2. 编写liuyanservlet在该程序中的doPost()方法中使用请求转发的形式将程序跳转到liuyanresult.jsp
3. 编写liuyanresult.jsp程序,在该页面中输出用户提交的用户名及留言内容。
注:两个过滤器程序均对liuyanservlet程序进行过滤
[html]
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<html>
<head>
</head>
<body>
<form action="MessageServlet" method="post">
<table>
<tr>
<td>
标题:
<input type="text" name="title" size="30">
</td>
</tr>
<tr>
<td>
内容:
<textarea rows="5" cols="40" name="content"></textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
[html]
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<html>
<head>
</head>
<body>
用户名 :<%=request.getAttribute("title")%><br/>
留言信息:<%=request.getAttribute("content")%>
</body>
</html>
[html]
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>CharacterFilter</filter-name>
<filter-class>com.mars.filter.CharacterFilter</filter-class>
</filter>
<filter>
<filter-name>WordFilter</filter-name>
<filter-class>com.mars.filter.WordFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterFilter</filter-name>
<url-pattern>/MessageServlet</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>WordFilter</filter-name>
<url-pattern>/MessageServlet</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>MessageServlet</servlet-name>
<servlet-class>com.mars.servlet.MessageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MessageServlet</servlet-name>
<url-pattern>/MessageServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>submit.jsp</welcome-file>
</welcome-file-list>
</web-app>
[java]
package com.mars.filter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
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 javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
public class CharacterFilter implements Filter {
public void init(FilterConfig arg0) throws ServletException {
System.out.println("调用init");
}
public void destroy() {
System.out.println("调用destroy");
}
// 解决post方式的乱码问题
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
response.setContentType("text/html;charset=GBK");
chain.doFilter(request, response);
//chain.doFilter(req, resp);
}
}
[java]
package com.mars.filter;
import java.io.IOException;
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 javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
public class WordFilter implements Filter {
private FilterConfig config = null;
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse resp=(HttpServletResponse)response;
String title = req.getParameter("title");
String content = req.getParameter("content");
title = title.replace("张", "李");
content = content.replace("剑桥", "****");
req.setAttribute("title", title);
req.setAttribute("content", content);
chain.doFilter(request, response);
}
public void destroy() {
config = null;
}
}
[java]
package com.mars.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MessageServlet extends HttpServlet {
/* public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("result.jsp").forward(request,
response);
}
}
摘自 Mars学IT