过滤器解决Struts2中的中文乱码问题

index.jsp:

Html代码 复制代码 收藏代码
  1. <%@ page language="java"import="java.util.*"pageEncoding="utf-8"%> 
  2. <%@ page contentType="text/html; charset=UTF-8"%> 
  3. <
  4. String path = request.getContextPath(); 
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
  6. %> 
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  8. <html> 
  9.   <head> 
  10.     <base href="<%=basePath%>">   
  11.     <title>My JSP 'index.jsp' starting page</title> 
  12.     <meta http-equiv="pragma"content="no-cache"> 
  13.     <meta http-equiv="cache-control"content="no-cache"> 
  14.     <meta http-equiv="expires"content="0">    
  15.     <meta http-equiv="keywords"content="keyword1,keyword2,keyword3"> 
  16.     <meta http-equiv="description"content="This is my page"> 
  17.   </head>  
  18.   <body> 
  19.   <form action="test1"method="post"> 
  20.     用户名:<inputtype="text"name="username"/><br/> 
  21.     密 码:<inputtype="password"name="password"> 
  22.   <input type="submit"value="提交"> 
  23.   </form> 
  24.   </body> 
  25. </html> 
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page contentType="text/html; charset=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>
    <base href="<%=basePath%>">  
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
  </head> 
  <body>
  <form action="test1" method="post">
    用户名:<input type="text" name="username"/><br/>
    密 码:<input type="password" name="password">
  <input type="submit" value="提交">
  </form>
  </body>
</html>


success.jsp:

Html代码 复制代码 收藏代码
  1. <%@ page language="java"import="java.util.*"pageEncoding="utf-8"%> 
  2. <
  3. String path = request.getContextPath(); 
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
  5. %> 
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  7. <html> 
  8.   <head> 
  9.     <base href="<%=basePath%>">    
  10.     <title>My JSP 'success.jsp' starting page</title>    
  11.     <meta http-equiv="pragma"content="no-cache"> 
  12.     <meta http-equiv="cache-control"content="no-cache"> 
  13.     <meta http-equiv="expires"content="0">    
  14.     <meta http-equiv="keywords"content="keyword1,keyword2,keyword3"> 
  15.     <meta http-equiv="description"content="This is my page"> 
  16.   </head> 
  17.   <body> 
  18.     登录成功!欢迎:${username} 
  19.   </body> 
  20. </html> 
<%@ 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>
    <base href="<%=basePath%>">   
    <title>My JSP 'success.jsp' starting page</title>   
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
  </head>
  <body>
    登录成功!欢迎:${username}
  </body>
</html>


error.jsp:

Html代码 复制代码 收藏代码
  1. <%@ page language="java"import="java.util.*"pageEncoding="utf-8"%> 
  2. <
  3. String path = request.getContextPath(); 
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
  5. %> 
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  7. <html> 
  8.   <head> 
  9.     <base href="<%=basePath%>">    
  10.     <title>My JSP 'error.jsp' starting page</title>    
  11.     <meta http-equiv="pragma"content="no-cache"> 
  12.     <meta http-equiv="cache-control"content="no-cache"> 
  13.     <meta http-equiv="expires"content="0">    
  14.     <meta http-equiv="keywords"content="keyword1,keyword2,keyword3"> 
  15.     <meta http-equiv="description"content="This is my page"> 
  16.   </head>  
  17.   <body> 
  18.     登录失败! 
  19.     <a href="index.jsp">返回重新登录</a> 
  20.   </body> 
  21. </html> 
<%@ 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>
    <base href="<%=basePath%>">   
    <title>My JSP 'error.jsp' starting page</title>   
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
  </head> 
  <body>
    登录失败!
    <a href="index.jsp">返回重新登录</a>
  </body>
</html>


LoginAction.java:

Java代码 复制代码 收藏代码
  1. package com.cz.action; 
  2. import java.io.UnsupportedEncodingException; 
  3. import javax.servlet.http.HttpServletRequest; 
  4. import org.apache.struts2.ServletActionContext; 
  5. import com.opensymphony.xwork2.ActionContext; 
  6. import com.opensymphony.xwork2.ActionSupport; 
  7. publicclass LoginActionextends ActionSupport { 
  8.     private String username; 
  9.     private String password; 
  10.     public String getUsername() { 
  11.         return username; 
  12.     } 
  13.     publicvoid setUsername(String username) { 
  14.         this.username = username; 
  15.     } 
  16.     public String getPassword() { 
  17.         return password; 
  18.     } 
  19.     publicvoid setPassword(String password) { 
  20.         this.password = password; 
  21.     }  
  22.     public String execute(){ 
  23.         System.out.println("到达execute。。。。"); 
  24.         returnnull;        
  25.     }    
  26.     public String login(){          
  27.         System.out.println("到达login。。。。用户名:"+username); 
  28.         if("啊".equals(username) &&"123".equals(password)){ 
  29.             return"success"
  30.         } 
  31.         return"login"
  32.     } 
package com.cz.action;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    } 
    public String execute(){
        System.out.println("到达execute。。。。");
        return null;       
    }   
    public String login(){         
        System.out.println("到达login。。。。用户名:"+username);
        if("啊".equals(username) && "123".equals(password)){
            return "success";
        }
        return "login";
    }
}


过滤器FilterEncoding.java

Java代码 复制代码 收藏代码
  1. import java.io.IOException; 
  2. import javax.servlet.Filter; 
  3. import javax.servlet.FilterChain; 
  4. import javax.servlet.FilterConfig; 
  5. import javax.servlet.ServletException; 
  6. import javax.servlet.ServletRequest; 
  7. import javax.servlet.ServletResponse; 
  8. publicclass FilterEncodingimplements Filter { 
  9.     protected String encoding;// 接收字符编码 
  10.     protected FilterConfig filterConfig;// 初始化配置 
  11.     publicvoid init(FilterConfig filterConfig)throws ServletException { 
  12.         // 从web.xml文件中读取encoding的值 
  13.         encoding = filterConfig.getInitParameter("encoding"); 
  14.     } 
  15.     // doFilter方法 
  16.     publicvoid doFilter(ServletRequest request, ServletResponse response, 
  17.             FilterChain chain) throws IOException, ServletException { 
  18.         request.setCharacterEncoding(encoding); 
  19.         chain.doFilter(request, response); 
  20.     } 
  21.     publicvoid destroy() { 
  22.  
  23.     } 
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;
public class FilterEncoding implements Filter {
    protected String encoding; // 接收字符编码
    protected FilterConfig filterConfig; // 初始化配置
    public void init(FilterConfig filterConfig) throws ServletException {
        // 从web.xml文件中读取encoding的值
        encoding = filterConfig.getInitParameter("encoding");
    }
    // doFilter方法
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding(encoding);
        chain.doFilter(request, response);
    }
    public void destroy() {

    }
}


web.xml:

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0"encoding="UTF-8"?> 
  2. <web-app version="2.5" 
  3.     xmlns="http://java.sun.com/xml/ns/javaee" 
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
  7.   <welcome-file-list> 
  8.     <welcome-file>index.jsp</welcome-file> 
  9.   </welcome-file-list> 
  10.     <filter> 
  11.         <filter-name>struts2</filter-name> 
  12.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>        
  13.     </filter> 
  14.     <filter>  
  15.          <filter-name>encoding</filter-name>  
  16.              <filter-class>FilterEncoding </filter-class>  
  17.          <init-param>  
  18.               <param-name>encoding</param-name>  
  19.               <param-value>utf-8</param-value>  
  20.          </init-param>  
  21.     </filter>    
  22.     <filter-mapping> 
  23.          <filter-name>encoding</filter-name> 
  24.          <url-pattern>/*</url-pattern> 
  25.     </filter-mapping> 
  26.     <filter-mapping> 
  27.         <filter-name>struts2</filter-name> 
  28.         <url-pattern>/*</url-pattern> 
  29.     </filter-mapping> 
  30. </web-app> 


struts.xml:

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0"encoding="UTF-8"?> 
  2. <!DOCTYPE struts PUBLIC 
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd"> 
  5. <struts> 
  6. <package name="default"namespace="/"extends="struts-default">       
  7.         <actionname="test1"class="com.cz.action.LoginAction"method="login"> 
  8.             <resultname="success">/success.jsp</result> 
  9.             <resultname="login">/error.jsp</result> 
  10.         </action> 
  11.   </package> 
  12. </struts>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值