Chinese characters are transmitted from JSP to servlet processing

xiaoxiao2021-03-06  71

For a request.getParameter () method in JSP and Servlet, if you want to pass

Chinese characters, there are several solutions,

One: You can in String Temp = Request.GetParameter ("XX");

Temp = new string (Temp.getBytes ("ISO8859_1");

Second: Common methods are, set a filter:

1. Java file is com.esoon.shabc.utils.setcharacterencodingfilter

Source File:

Package com.esoon.shabc.utils;

Import java.io.ioException;

Import java.io.unsupportedEncodingexception;

Import java.util. *;

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;

Public Class setcharacterencodingfilter

Implements filter {

protected string encoding = null;

Protected filterconfig filter firmconfig = NULL;

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.

* /

Public Void Dofilter (ServletRequest Request, ServletResponse Response,

FILTERCHAIN ​​chain

THROWS IOException, servletexception {

// conditionally select and set the character encoding to be available

IF (ignore || (Request.getCharacterencoding () == null) {

String Encoding = SELECTENCODING (Request);

IF (Encoding! = NULL)

Request.setCharacterencoding (Encoding); // Setting the REQUEST encoded

}

// Pass Control on to the next filter

// Transfer control to the next filter

Chain.dofilter (Request, Response);

}

/ **

* Place this Filter INTO Service.

* Read the value of the initial parameters from the web-app's web.xml file

* /

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"))

THIS.IGNORE = TRUE;

Else IF (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 shop be set, return

* null .

* Select the original code of Request

* /

protected string selectencoding (servletRequest request) {

Return (this.encoding);

}

}

2. Then add in the "Web-app> in the web.xml file:

set_character_encoding

com.esoon.shabc.utils.setcharacterencodingfilter

Encoding

GB2312

set_character_encoding

/ *

3. If the submitted field is in the form, this form should be set to: Method = "POST"

Similar to:

Through this method, even if you have multiple Chinese fields, you only need in servlet.

Request.getParameter ("##");

There is no need to change back string ().

转载请注明原文地址:https://www.9cbs.com/read-87620.html

New Post(0)