Chinese coding uses GBK or GB2312, the character set of the former support is large.
specifically is:
(1) Set in the JSP page: <% @ page contentType = "text / html; charset = GBK"%>
(2) In the servlet, execute response.setContentType ("text / html; charset = GBK" before response.getwriter () call
(3) If there is no setting in the servlet, write the TOGBK function yourself, transform when the parameter is obtained, the code is as follows:
/ **
* Do coding conversion, convert to GBK.
* @Param STR Need to do a string of conversion
* @Return String Returns the converted string
* /
Public Static String TogBk (String Str) {
String Temp = STR;
Try {
IF (temp == null) {
Temp = "";
}
IF (Temp.equals ("NULL")) {
Temp = "";
}
Byte [] b = Temp.getbytes ("ISO8859-1"); // Split Stroke by byte
Str = new string (b, "gbk"); // Press the GBK encoding method to combine bytes
} catch (unsupporteencodingexception UEE) {
}
Return Str;
}
(4) Write a servlet filter to perform encoding conversions before each response. This avoids the above (2) (3) operation in each servlet. Filter examples are as follows:
Package kellerdu.util;
MPORT JAVAX.SERVLET. *;
Import javax.servlet.http. *;
Import java.io. *;
Import java.util. *;
Import net.sf.hibernate. *;
/ ** *
Filter that sets the character encoding to be used in parsing the * incoming request, either unconditionally or only if the client did not * specify a character encoding. Configuration of this filter is based on * the following initialization parameters : p> *
ignore code> initialization Parameter. This parameter * is Required, soled. li> * - ignore strong> - if set to true, any character encoding * specified by the client is ignored, and the value Returned by the *
selectEncoding () code> method is set. if set to false, * selectencoding () code> is caled only strong> if the * client HAS NOT Already specified an encoding. by default, this * parameter is set to "true". li> * ul> * * although this filter can be used unchanged, IT is also easy to * subclass it and make the selectEncoding () code> method more * intelligent about what encoding to choose, based on characteristics of * the incoming request (such as the values of the Accept- Language code> * and user-agent code> headers, or a value stashed in the current * users session. P> * / public class setENCODINGSERVLET EXTENDS HTTPSERVLET IMPLEMENTS FILTER {// ------ ------------------------------------ ------------- Instance Variables
. / ** * 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 filterfig = null
/ ** * SHOULD a Character Encoding Specified by the client be ignored? * / Protected boolean ignore = true;
/ / -------------------------------------------------------------------------------------------- --------- Public Methods
/ ** * 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 {// conditionally select and set the character encoding to be used if (ignore || (request.getCharacterEncoding () == null)) {String encoding = selectEncoding (request); if (! encoding = null) {request.setCharacterEncoding (encoding); }} / ** * User authentication * / try {httpservletRequest Req = (httpser VletRequest) Request; TBLSYSUSER User = (tblsysuser) Req.getSession (). getAttribute (loginoputer.user); if (user == null && req.getServletPath (). Indexof ("login") == -1) {//// No log in Req.GetRequestDispatcher ("/ login.jsp"). Forward (Request, Response); //Chain.dofilter (Request, Response);} else {chain.dofilter (request, response); // After each response Close Heibernate Session Hibernateutil.closeSession ();}} catch (servletexception ex) {} catch (ieException ex) {} catch (hibernateException ex) {}
. / ** * Place this filter into service * * @param filterConfig The filter configuration object * / public void init (FilterConfig filterConfig) throws ServletException {//System.out.println("SetEncodingServlet inits "); 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 f (value.equalsignorecase (" yes ")) {this.Ignore = true;} else {this.ynore = false;} try {com.yingrui.voip.hibernateUtil. CloseSession ();} catch (hibernateException ex) {system.out.println ("Hibernate Init Exception!");}}
/ / -------------------------------------------------------------------------------------------- ------ Protected Methods
/ ** * 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 * null code>. * * The default implementation unconditionally returns the value configured * by the encoding strong> initialization parameter for this * filter. * * @param request The servlet request we are processing * / protected String selectEncoding (ServletRequest request) {Return (this.encoding);
}
}