JSP and servlet solvement

xiaoxiao2021-03-06  97

Chinese coding uses GBK or GB2312, the character set of the former support is large. Specifically: (1) Set in the JSP page: <% @ Page ContentType = "Text / HTML; Charset = GBK"%> (2) In the servlet, execute response.setContentType before response.getwriter () call ("" TEXT / HTML; Charset = GBK ") (3) If there is no setting in the servlet, write the TOGBK function yourself, conversion when the parameter is obtained, the code is as follows: / ** * Do coding conversion, converted into GBK. * @Param STR Requires the conversion string * @Return String Returns the converted string * / public static string Togbk (string string TEMP = Str; try {if (temp == null) {TEMP = " } IF ("null") {temp = "";} byte [] b = Temp.getbytes ("ISO8859-1"); // Split Str = New String by byte Skew STRING B, "GBK"); // Write a servlet filter in the GBK encoding method} Catch (unsupportedencodingException UEE) {} Return Str;} (4) to perform encoded conversion before each response. This avoids the above (2) (3) operation in each servlet. Filter examples are as follows: package ken.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 : *

    *
  • Encoding - the character encoding to be configured * for this request, Either confoundation or unconditionally based on * the ignore initialization Parameter. This parameter * is Required, soled. *
  • ignore - if set to true, any character encoding * specified by the client is ignored, and the value Returned by the * selectEncoding () method is set. if set to false, * selectencoding () is caled only if the * client HAS NOT Already specified an encoding. by default, this * parameter is set to "true". * * *

    although this filter can be used unchanged, IT is also easy to * subclass it and make the selectEncoding () method more * intelligent about what encoding to choose, based on characteristics of * the incoming request (such as the values ​​of the Accept- Language * and user-agent headers, or a value stashed in the current * users session. * / 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 . *

    * The default implementation unconditionally returns the value configured * by the encoding initialization parameter for this * filter. * * @param request The servlet request we are processing * / protected String selectEncoding (ServletRequest request) {Return (this.encoding);

    }

    }

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

New Post(0)