關於Servlet、Jsp中的多國語言顯示(續)

來源:互聯網
上載者:User
js|servlet|顯示 續   
---

/**
* Insert the method's description here.
* Creation date: (2001-2-4 17:28:17)
* @return java.util.Enumeration
*/
public Enumeration getParameterNames() {
if (pairs == null) return null;
return pairs.keys();
}
/**
* Insert the method's description here.
* Creation date: (2001-2-4 17:33:40)
* @return java.lang.String[]
* @param name java.lang.String
*/
public String[] getParameterValues(String name) {
if (pairs == null || !pairs.containsKey(name)) return null;
ArrayList al = (ArrayList) pairs.get(name);
String[] values = new String[al.size()];
for(int i=0;i<values.length;i++)
values[i] = (String) al.get(i);
return values;
}
/**
* Insert the method's description here.
* Creation date: (2001-2-4 20:34:37)
* @param urlenc java.lang.String
*/
private void parse(String urlenc) throws java.io.IOException{
if (urlenc == null) return;
StringTokenizer tok = new StringTokenizer(urlenc,"&");
try{
while (tok.hasMoreTokens()){
String aPair = tok.nextToken();
int pos = aPair.indexOf("=");
String name = null;
String value = null;
if(pos != -1){
name = decode(aPair.substring(0,pos));
value = decode(aPair.substring(pos+1));
}else{
name = aPair;
value = "";
}
if(pairs.containsKey(name)){
ArrayList values = (ArrayList)pairs.get(name);
values.add(value);
}else{
ArrayList values = new ArrayList();
values.add(value);
pairs.put(name,values);
}
}
}catch(Exception e){
throw new java.io.IOException(e.getMessage());
}
}
/**
* Insert the method's description here.
* Creation date: (2001-2-4 20:34:37)
* @param urlenc java.lang.String
*/
private void parse(String urlenc,String encoding) throws java.io.IOException{
if (urlenc == null) return;
StringTokenizer tok = new StringTokenizer(urlenc,"&");
try{
while (tok.hasMoreTokens()){
String aPair = tok.nextToken();
int pos = aPair.indexOf("=");
String name = null;
String value = null;
if(pos != -1){
name = decode(aPair.substring(0,pos),encoding);
value = decode(aPair.substring(pos+1),encoding);
}else{
name = aPair;
value = "";
}
if(pairs.containsKey(name)){
ArrayList values = (ArrayList)pairs.get(name);
values.add(value);
}else{
ArrayList values = new ArrayList();
values.add(value);
pairs.put(name,values);
}
}
}catch(Exception e){
throw new java.io.IOException(e.getMessage());
}
}
}
這個類的功能就是讀取並儲存form提交的資訊,並實現常用的getParameter方法。
package com.hto.servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* Insert the type's description here.
* Creation date: (2001-2-5 8:28:20)
* @author: 錢衛春
*/
public class UtfBaseServlet extends HttpServlet {
public static final String PARAMS_ATTR_NAME = "PARAMS_ATTR_NAME";
/**
* Process incoming HTTP GET requests
*
* @param request Object that encapsulates the request to the servlet
* @param response Object that encapsulates the response from the servlet
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

performTask(request, response);

}
/**
* Process incoming HTTP POST requests
*
* @param request Object that encapsulates the request to the servlet
* @param response Object that encapsulates the response from the servlet
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

performTask(request, response);

}
/**
* 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.Date getDateParameter(HttpServletRequest request, String name, boolean required, java.sql.Date defValue) throws ServletException{
String value = getParameter(request,name,required,String.valueOf(defValue));
return java.sql.Date.valueOf(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 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流,用戶端能否顯示,就看用戶端的了。
  對於multipart/form-data的form提交的內容,我也提供一個類用來處理,在這個類的構造子中可以指定頁面使用的charset,預設還是UTF-8,限於篇幅不貼出源碼,如果感興趣可以mail to:vividq@china.com和我探討。
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.