程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> 過濾器鏈實現中文轉碼和文字屏蔽功能

過濾器鏈實現中文轉碼和文字屏蔽功能

編輯:關於JSP

編寫一個過濾器鏈實現如下功能:
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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved