LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: menglianjing

jsp怎么转码

[复制链接]
发表于 2004-6-24 20:05:34 | 显示全部楼层
package com.demo;

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;

/**
* @author hantsy
*
* Set page encoding as utf-8 to all .jsp files.
*
*
*/
public class CharacterEncodingFilter implements Filter {

        public void init(FilterConfig config) throws ServletException {

        }

        public void doFilter(
                ServletRequest request,
                ServletResponse response,
                FilterChain chain)
                throws IOException, ServletException {
                        HttpServletRequest req=(HttpServletRequest) request;
                        req.setCharacterEncoding("utf-8");
                        chain.doFilter(request,response);

        }
        public void destroy() {

        }

}
发表于 2004-6-24 20:07:15 | 显示全部楼层
<filter>
        <filter-name>Charset</filter-name>
        <display-name>Charset</display-name>
        <description>Filter charset</description>
        <filter-class>com.demo.CharacterEncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Charset</filter-name>
        <url-pattern>/servlet/*</url-pattern>
    </filter-mapping>
web.xml加入配置。。。一劳永逸。。。我这个简化了。。。具体可以参考tomcat文档。。。
发表于 2004-6-24 20:10:18 | 显示全部楼层
最初由 hantsy 发表
<filter>
        <filter-name>Charset</filter-name>
        <display-name>Charset</display-name>
        <description>Filter charset</description>
        <filter-class>com.demo.CharacterEncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Charset</filter-name>
        <url-pattern>/servlet/*</url-pattern>
    </filter-mapping>
web.xml加入配置。。。一劳永逸。。。从此就不用关心编码了。。。我这个简化了。。。具体可以参考tomcat文档。。。
 楼主| 发表于 2004-6-24 20:35:56 | 显示全部楼层

re

非也!!这样不行的,不信可以自己试
发表于 2004-6-24 20:39:51 | 显示全部楼层
数据丢失前好像看到过menglianjing兄也发XML配置转码的问题,当时没有留意看,现在能不能在拿出来讨论一下啊,呵呵...
发表于 2004-6-24 20:51:30 | 显示全部楼层

回复: re

最初由 menglianjing 发表
非也!!这样不行的,不信可以自己试

这个我不和你争,,,我已经在项目中用过多次了。。。
发表于 2004-6-24 20:58:04 | 显示全部楼层
apache上有原例子。。。我懒嘛,,,所以简化了。。。
原例子是lang是作为一个参数配置的。。。
发表于 2004-6-25 21:11:05 | 显示全部楼层
这样的确可以的

[PHP]

<web-app>
  <context-param>
    <param-name>pageCount</param-name>
    <param-value>20</param-value>
  </context-param>
  <filter>
    <filter-name>EncodingFilter</filter-name>
    <display-name>EncodingFilter</display-name>
    <description>no description</description>
    <filter-class>com.bupticet.filter.EncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>GBK</param-value>
    </init-param>
  </filter>
  <filter>
    <filter-name>SignOnFilter</filter-name>
    <display-name>SignOnFilter</display-name>
    <description>no description</description>
    <filter-class>com.bupticet.filter.SignOnFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>SignOnFilter</filter-name>
    <url-pattern>/servlet/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>SignOnFilter</filter-name>
    <url-pattern>*.jsp</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>SignOnFilter</filter-name>
    <url-pattern>*.htm</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>SignOnFilter</filter-name>
    <url-pattern>*.js</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>SignOnFilter</filter-name>
    <url-pattern>*.css</url-pattern>
  </filter-mapping>

[/PHP]
 楼主| 发表于 2004-6-25 21:49:48 | 显示全部楼层

re

下次贴图让你们看
发表于 2004-6-28 17:07:44 | 显示全部楼层
解决汉字编码问题
1.修改web.xml文件,在<web-app>标签后增加如下内容.

<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.company.demo.util.SetEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>

2.创建类文件(SetEncodingFilter.java)

package com.company.demo.util;
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.UnavailableException;

/**
* Set All HttpRequest Encoding

*/
public class SetEncodingFilter
implements Filter {
/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = null;

/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;

/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;

/**
* Take this filter out of service.
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}

/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(
ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
request.setCharacterEncoding(selectEncoding(request));
}
chain.doFilter(request, response);
}

/**
* Place this filter into service.
* @param filterConfig The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException {

this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}

/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}

/**
* Returns the filterConfig.
* @return FilterConfig
*/
public FilterConfig getFilterConfig() {
return filterConfig;
}

/**
* Sets the filterConfig.
* @param filterConfig The filterConfig to set
*/
public void setFilterConfig(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}

}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表