当前位置: 代码迷 >> Web前端 >> 署理模式 WEB没有过不了的编码(gbk)
  详细解决方案

署理模式 WEB没有过不了的编码(gbk)

热度:218   发布时间:2012-11-01 11:11:31.0
代理模式 WEB没有过不了的编码(gbk)

//过滤器用到的代理模式类

package com.newer;
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;


public class MyFileFilter implements Filter {

?
?private FilterConfig filterConfig;
?public void destroy() {
??// TODO Auto-generated method stub

?}

?public void doFilter(ServletRequest request, ServletResponse response,
???FilterChain chain) throws IOException, ServletException {
??// TODO Auto-generated method stub
??javax.servlet.http.HttpServletRequest req=(javax.servlet.http.HttpServletRequest)request;
?????? MyRequest r=new MyRequest(req);
?????? chain.doFilter(r, response);
?}

?public void init(FilterConfig filterConfig) throws ServletException {
??// TODO Auto-generated method stub
??this.filterConfig = filterConfig;

?}

}

//代理类
class MyRequest extends javax.servlet.http.HttpServletRequestWrapper{
?? MyRequest(javax.servlet.http.HttpServletRequest request){
???? super(request);
?? }

?? public String getParameter(String name){
???? String temp=this.getRequest().getParameter(name);
???? try {
?????? temp = new String(temp.getBytes("ISO-8859-1"), "GBK");
???? }
???? catch (UnsupportedEncodingException ex) {
?????? ex.printStackTrace();
???? }
???? return temp;
?? }
?}

?

//?web.xml 文件内容

?

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
?xmlns="http://java.sun.com/xml/ns/javaee"
?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
?xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
?http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
? <filter>
??? <filter-name>file</filter-name>
?? <filter-class>com.newer.MyFileFilter</filter-class>
? </filter>
? <filter-mapping>
??? <filter-name>file</filter-name>
??? <url-pattern>*.jsp</url-pattern>
? </filter-mapping>
? <welcome-file-list>
??? <welcome-file>index.jsp</welcome-file>
? </welcome-file-list>
</web-app>

//建两个jsp文件

//index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
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>
??? <form action="ok.jsp" method="post">
???? <input type="text" name="name">
???? <input type="submit" value="submit">
??? </form>
? </body>
</html>

//ok.jsp

?

<%@ page language="java" contentType="text/html; charset=gb2312"
??? pageEncoding="gb2312"? %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
?
? <%=request.getParameter("name")%>
</body>
</html>

?

//不管你的提交方式是POST 还是GET? 都能过滤编码问题..

?

大家要是能有一个更好的过滤编码..发出来一起学习啊,

?


1 楼 wangdi 2007-11-08  
怎么论坛图片链接有错误了?
  相关解决方案