- 打印本文 关闭窗口
- Servlet、Jsp中的多国语言显示
- 作者:采集员 文章来源:来源于网络 点击数: 更新时间:2005/9/10 14:27:40
- >* Insert the method's description here.
* Creation date: (2001-2-5 8:52:43)
* @return int
* @param request javax.servlet.http.HttpServletRequest
* @param name java.lang.String
* @param required boolean
* @param defValue int
*/
public static double getDoubleParameter(HttpServletRequest request, String name, boolean required, double defValue) throws ServletException{
String value = getParameter(request,name,required,String.valueOf(defValue));
return Double.parseDouble(value);
}
/**
* Insert the method's description here.
* Creation date: (2001-2-5 8:52:43)
* @return int
* @param request javax.servlet.http.HttpServletRequest
* @param name java.lang.String
* @param required boolean
* @param defValue int
*/
public static float getFloatParameter(HttpServletRequest request, String name, boolean required, float defValue) throws ServletException{
String value = getParameter(request,name,required,String.valueOf(defValue));
return Float.parseFloat(value);
}
/**
* Insert the method's description here.
* Creation date: (2001-2-5 8:52:43)
* @return int
* @param request javax.servlet.http.HttpServletRequest
* @param name java.lang.String
* @param required boolean
* @param defValue int
*/
public static int getIntParameter(HttpServletRequest request, String name, boolean required, int defValue) throws ServletException{
String value = getParameter(request,name,required,String.valueOf(defValue));
return Integer.parseInt(value);
}
/**
* Insert the method's description here.
* Creation date: (2001-2-5 8:43:36)
* @return java.lang.String
* @param request javax.servlet.http.HttpServletRequest
* @param name java.lang.String
* @param required boolean
* @param defValue java.lang.String
*/
public static String getParameter(HttpServletRequest request, String name, boolean required, String defValue) throws ServletException{
if(request.getAttribute(UtfBaseServlet.PARAMS_ATTR_NAME) != null) {
UTF8ParameterReader params = (UTF8ParameterReader)request.getAttribute(UtfBaseServlet.PARAMS_ATTR_NAME);
if (params.getParameter(name) != null) return params.getParameter(name);
if (required) throw new ServletException("The Parameter "+name+" Required but not provided!");
else return defValue;
}else{
if (request.getParameter(name) != null) return request.getParameter(name);
if (required) throw new ServletException("The Parameter "+name+" Required but not provided!");
else return defValue;
}
}
/**
* Returns the servlet info string.
*/
public String getServletInfo() {return super.getServletInfo();
}
/**
* Insert the method's description here.
* Creation date: (2001-2-5 8:52:43)
* @return int
* @param request javax.servlet.http.HttpServletRequest
* @param name java.lang.String
* @param required boolean
* @param defValue int
*/
public static java.sql.Timestamp getTimestampParameter(HttpServletRequest request, String name, boolean required, java.sql.Timestamp defValue) throws ServletException{
String value = getParameter(request,name,required,String.valueOf(defValue));
return java.sql.Timestamp.valueOf(value);
}
/**
* Initializes the servlet.
*/
public void init() {
// insert code to initialize the servlet here}
/**
* Process incoming requests for information
*
* @param request Object that encapsulates the request to the servlet
* @param response Object that encapsulates the response from the servlet
*/
public void performTask(HttpServletRequest request, HttpServletResponse response) {try
{
// Insert user code from here.}
catch(Throwable theException)
{
// uncomment the following line when unexpected exceptions
// are occuring to aid in debugging the problem.
//theException.printStackTrace();
}
}
/**
* Insert the method's description here.
* Creation date: (2001-2-5 8:31:54)
* @param request javax.servlet.ServletRequest
* @param response javax.servlet.ServletResponse
* @exception javax.servlet.ServletException The exception description.
* @exception java.io.IOException The exception description.
*/
public void service(ServletRequest request, ServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
String content = request.getContentType();
if(content == null || content != null && content.toLowerCase().startsWith("application/x-www-form-urlencoded"))
request.setAttribute(PARAMS_ATTR_NAME,new UTF8ParameterReader((HttpServletRequest)request));
super.service(request,response);
}
}
这个就是Servlet基类,它覆盖了父类的service方法,在调用父类service前,创建了UTF8ParameterReader对象,其中保存了form中提交的信息。然后把这个对象作为一个Attribute保存到Request对象中。然后照样调用父类的service方法。
对于继承这个类的Servlet,要注意的是,"标准"getParameter在也不能读到post的数据,因为在这之前这个类中已经从ServletInputStream中读出了数据了。所以应该使用该类中提供的getParameter方法。
剩下的就是输出问题了,我们要把输出的信息,转为UTF8的二进制流输出。只要我们设置Content-Type时指定charset为UTF8,然后使用PrintWriter输出,那么这些转换是自动进行的,Servlet中这样设置:
response.setContentType("text/html;charset=UTF8");
Jsp中这样设置:
<%@ page contentType="text/html;charset=UTF8"%>
这样就可以保证输出是UTF8流,客户端能否显示,就看客户端的了。 - 打印本文 关闭窗口