ITPub博客

首页 > 应用开发 > IT综合 > jsp web应用中文化

jsp web应用中文化

原创 IT综合 作者:vmoemplus 时间:2007-05-14 23:26:36 0 删除 编辑

jsp web应用中文化,包括过滤器,字符编码等 这个更好用

<%@ page pageEncoding="GBK" language="java" contentType="text/html,charset=GBK" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>

[@more@]

web.xml设置字符过滤器,实质是个Servlet

encodefilter
com.util.EncodeFilter


encodefilter
/*

package com.util;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class EncodeFilter extends HttpServlet implements Filter {
private FilterConfig filterConfig;

//Handle the passed-in FilterConfig
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}

//Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) {

try {
// if(request.getCharacterEncoding().equals("ISO8859_1"))
request.setCharacterEncoding("gbk"); ----过滤器设置字符编码
filterChain.doFilter(request, response);
}
catch(ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
}
catch(IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
}

//Clean up resources
public void destroy() {
}
}

<%@ page pageEncoding="GBK" language="java" contentType="text/html,charset=GBK" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>

tomcat支持中文名的处理方式
在server.xml配置文件中:
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="utf-8"/> --设置URIEncoding字符charset
<!-- Note : To disable connection timeouts, set connectionTimeout value to 0 -->


jsp页面中设置
<%@ page contentType="text/html; charset=GB2312" %> --最好使用UTF-8统一编码

设置HttpServletRequest对象字符编码
request.setCharacterEncoding("GB2312");--ActionServlet,通常使用过滤器更佳,预先处理所有HTTP请求
HttpServletRequest.setCharacterEncoding(String encoding);

response.setContentType("text/html;charset=GB2312")


appication.properties

native2ascii -encoding GB2312 application.prop application_zh_CN.properties
application_zh_CN.properties --中文显示Unicode编码
application_en.properties --英文直接显示

通用日志记录--日志消息级别 :FATAL,ERROR,WARN,INFO,DEBUG,TRACE,方法小写 commons-logging.properties
org.apache.commons.logging.Log
org.apache.commons.logging.LogFactory
public static Log getLog(Class clazz) throw LogConfigurationException

public static Log getLog(String name) throw LogConfigurationException

常用日志实现
Log4J日志器 classpath: log4j.properties
log4j.rootLogger=[p优先级],appenderName名字
Logger
Appender
日志级别DEBUG,INFO,WARN,FATAL
Layout

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9194732/viewspace-914999/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    43
  • 访问量
    57496