package fengke.servlet; |
import java.io.IOException; |
import java.io.PrintWriter; |
import javax.servlet.ServletException; |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletResponse; |
/** |
* 解决乱码问题的方式 |
* @author 锋客 |
* |
*/ |
public class Character extends HttpServlet { |
public void doGet(HttpServletRequest request, HttpServletResponse response) |
throws ServletException, IOException { |
response.setContentType( "text/html" ); |
this .doPost(request, response); |
} |
|
public void doPost(HttpServletRequest request, HttpServletResponse response) |
throws ServletException, IOException { |
|
response.setContentType( "text/html" ); |
/*1.采用设置字符集的方式 |
* request.setCharacterEncoding("utf-8"); |
*/ |
/*2.设置过滤器 ====CoreFilter.java |
* 配置web.xml |
*/ |
/* |
* 3.采用转码的方式 |
* String s = new String(request.getParameter("c").getBytes("ISO8859-1"),"UTF-8"); |
*/ |
|
String s = request.getParameter( "c" ); |
System.out.println(s); |
} |
|
} |
=============================================== |
<%@ 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 '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" > |
<!-- |
<link rel= "stylesheet" type= "text/css" href= "styles.css" > |
--> |
</head> |
<!-- 解决乱码问题 --> |
<body> |
<table> |
<form action= "Character" method= "post" > |
<tr> |
<td><input type= "text" name= "c" value= "中国" ></td> |
<td><input type= "submit" value= "提交" ></td> |
</tr> |
</form> |
</table> |
</body> |
</html> |
========================================================= |
<?xml version= "1.0" encoding= "UTF-8" ?> |
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "http://java.sun.com/xml/ns/javaee" xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id= "WebApp_ID" version= "3.0" > |
<display-name>Character</display-name> |
<servlet> |
<description>This is the description of my J2EE component</description> |
<display-name>This is the display name of my J2EE component</display-name> |
<servlet-name>Character</servlet-name> |
<servlet- class >fengke.servlet.Character</servlet- class > |
</servlet> |
<servlet-mapping> |
<servlet-name>Character</servlet-name> |
<url-pattern>/Character</url-pattern> |
</servlet-mapping> |
<welcome-file-list> |
<welcome-file>index.html</welcome-file> |
<welcome-file>index.htm</welcome-file> |
<welcome-file>index.jsp</welcome-file> |
<welcome-file> default .html</welcome-file> |
<welcome-file> default .htm</welcome-file> |
<welcome-file> default .jsp</welcome-file> |
</welcome-file-list> |
<!-- |
<filter> |
<filter-name>encodingFilter</filter-name> |
<filter- class >fengke.filter.CoreFilter</filter- class > |
<init-param> |
<param-name>encoding</param-name> |
<param-value>utf- 8 </param-value> |
</init-param> |
</filter> |
|
<filter-mapping> |
<filter-name>encodingFilter</filter-name> |
<url-pattern>/*</url-pattern> |
</filter-mapping> |
--> |
</web-app> |
==================================================================== |
package fengke.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; |
import javax.servlet.http.HttpSession; |
//过滤器处理表单传到servlet的乱码问题 |
public class CoreFilter implements Filter{ |
//自写一个request换掉原来的request,重写里面的getParemeter方法,可以设置编码 |
class MyRequest extends HttpServletRequestWrapper{ |
|
@Override |
public String getParameter(String param) { |
String value = null ; |
try { |
//post |
super .setCharacterEncoding(encoding); //把编码转换为encoding |
value = super .getParameter(param); |
if ( super .getMethod().equalsIgnoreCase( "GET" )){ |
if (value!= null ){ |
value = new String(value.getBytes( "iso8859-1" ),encoding); |
} |
} |
} catch (UnsupportedEncodingException e) { |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
return value; |
} |
public MyRequest(HttpServletRequest request) { |
super (request); |
} |
|
} |
protected String encoding= null ; |
public void destroy() { //销毁 |
// TODO Auto-generated method stub |
this .encoding= null ; |
} |
//对编码问题进行转换 |
public void doFilter(ServletRequest request, ServletResponse response, |
FilterChain chain) throws IOException, ServletException { |
// TODO Auto-generated method stub |
response.setContentType( "text/html;charset=" +encoding); |
//过滤未登录用户 |
HttpServletRequest req = (HttpServletRequest) request; |
HttpServletResponse resp = (HttpServletResponse) response; |
String path=req.getServletPath(); |
String param=req.getQueryString(); |
if (path!= null ){ |
path=path+ "?" +param; //全请求路径 |
} |
if (path.endsWith( "myAddress" )||path.endsWith( "myJingDong" )||path.indexOf( "myShouCang" )!=- 1 ||path.endsWith( "updateUser" )||path.indexOf( "showOrder" )!=- 1 ||path.indexOf( "showValidOrder" )!=- 1 ||path.indexOf( "showCancelOrder" )!=- 1 ||path.indexOf( "fillOrder" )!=- 1 ){ |
HttpSession session = req.getSession(); |
String userName = (String) session.getAttribute( "username" ); |
if (userName == null ){ |
session.setAttribute( "url" , path.replaceFirst( "/" , "" )); |
System.out.println(session.getAttribute( "url" )); |
resp.sendRedirect( "user.do?op=loginAction" ); |
return ; |
} |
} |
//把过滤器给下一个过滤器或资源处理器 |
chain.doFilter( new MyRequest((HttpServletRequest) request), response); |
} |
public void init(FilterConfig filterConfig) throws ServletException { |
// TODO Auto-generated method stub |
this .encoding=filterConfig.getInitParameter( "encoding" ); //encoding在web.xml中指定 |
} |
} |