编码转换过滤器
Posted siwuxie095
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编码转换过滤器相关的知识,希望对你有一定的参考价值。
------------------siwuxie095
使用过滤器进行编码转换
在 Java Web 开发中,经常会遇到乱码的问题,统一字符编码是
解决乱码问题的非常有效的手段
一个简单的编码转换过滤器实例:
使用过滤器对请求中的参数信息进行编码转换
工程结构目录如下:
后端代码:
LoginServlet.java:
package com.siwuxie095.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
// LoginServlet 继承自 HttpServlet public class LoginServlet extends HttpServlet { /** * 用于序列化和反序列化的 ID */ private static final long serialVersionUID = -7740192486028671728L;
//覆盖父类 HttpServlet 的 doGet() 方法 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("===== doGet ====="); //在 doGet() 方法里调用 doPost() 方法 //这样,GET请求和POST请求可以共用一套处理逻辑 doPost(req, resp); }
//覆盖父类 HttpServlet 的 doPost() 方法 @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("===== doPost ====="); String userName=req.getParameter("uname"); String password=req.getParameter("upwd");
System.out.println("用户名:"+userName); System.out.println("密码:"+password);
String forward=null;
if (userName.equals("李白")&&password.equals("8888")) { forward="/success.jsp"; }else { forward="/error.jsp"; }
RequestDispatcher rd=req.getRequestDispatcher(forward); rd.forward(req, resp); }
} |
EncodingFilter.java:
package com.siwuxie095.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;
public class EncodingFilter implements Filter {
//声明一个成员变量 用来保存当前应用的字符集名称 private String charEncoding=null;
public EncodingFilter() {
}
public void init(FilterConfig fConfig) throws ServletException { //在部署描述符中设置该应用的默认字符编码集 在init方法中获取到该设置 charEncoding=fConfig.getInitParameter("encoding"); //如果字符编码的名称没有设置 就抛出一个异常 if (charEncoding==null) { throw new ServletException("EncodingFilter中的编码设置为空!!!"); } }
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //如果当前应用的默认编码,与请求中的编码值不一致 if (!charEncoding.equals(request.getCharacterEncoding())) { //那么就将请求中的编码设置成当前默认的编码设置 request.setCharacterEncoding(charEncoding); }
//将响应的编码设置也改成当前默认的编码设置 response.setCharacterEncoding(charEncoding); chain.doFilter(request, response); }
public void destroy() {
}
} |
前端代码:
login.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>登录页面</title>
<script type="text/javascript"> function check(form){ if(document.forms.loginForm.uname.value==""){ alert("请输入用户名!"); document.forms.loginForm.uname.focus(); return false; } if(document.forms.loginForm.upwd.value==""){ alert("请输入密码!"); document.forms.loginForm.upwd.focus(); return false; } } </script>
<style type="text/css"> body { color: #000; font-size =14px; margin: 20px, auto; } </style>
</head> <body>
<!-- 添加表单,url在部署描述符中进行配置,使用post方式来提交 --> <form action="<%= request.getContextPath() %>/loginServlet" method="post" name="loginForm"> <table border="1" cellspacing="0" cellpadding="5" bordercolor="silver" align="center"> <tr> <td colspan="2" align="center" bgcolor="#E8E8E8">用户登录</td> </tr> <tr> <td>用户名:</td> <td><input type="text" name="uname" /></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="upwd" /></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="submit" onclick="return check(this);" /> <input type="reset" name="reset" /> </td> </tr> </table> </form>
</body> </html> |
success.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>登录成功提示页面</title>
<style type="text/css"> body { color: #000; font-size =14px; margin: 20px, auto; }
#message { text-align: center; } </style>
</head> <body>
<div id="message"> 登录成功!<br/> 您提交的信息为:<br/> 用户名:<%= request.getParameter("uname") %><br/> 密码:<%= request.getParameter("upwd") %><br/> <a href="<%= request.getContextPath() %>/login.jsp">返回登录页面</a> </div>
</body> </html> |
error.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>登录失败提示页面</title>
<style type="text/css"> body { color: #000; font-size =14px;以上是关于编码转换过滤器的主要内容,如果未能解决你的问题,请参考以下文章 |