免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
jsp與servlet中的中文問題解決


中文編碼采用GBK或者gb2312,前者支持的字符集合要大。
具體是:
(1)Jsp頁面中設(shè)定:<%@ page contentType="text/html; charset=GBK" %>
(2)Servlet中,在response.getWriter()調(diào)用之前,執(zhí)行response.setContentType(”text/html; charset=GBK")
(3)如果在Servlet中沒有設(shè)定,自行編寫toGBK函數(shù),在取得參數(shù)時候進行轉(zhuǎn)換,代碼如下:
 /**
    * 做編碼的轉(zhuǎn)換,轉(zhuǎn)換成GBK。
    * @param str  需要做轉(zhuǎn)換的字符串
    * @return  String   返回轉(zhuǎn)換后的字符串
    */
    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"); //按字節(jié)將串拆分
            str = new String(b, "GBK"); //按GBK編碼方式將字節(jié)組合
        } catch (UnsupportedEncodingException uee) {
        }
        return str;
    }
    (4)編寫一個Servlet Filter,在每次響應(yīng)之前進行編碼的轉(zhuǎn)換。這樣可以避免在每個Servlet中進行上述的(2)(3)操作。Filter示例如下:
 package kellerdu.util;
    mport javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import net.sf.hibernate.*;


/**
 * <p>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>
 * <ul>
 * <li><strong>encoding</strong> - The character encoding to be configured
 *     for this request, either conditionally or unconditionally based on
 *     the <code>ignore</code> initialization parameter.  This parameter
 *     is required, so there is no default.</li>
 * <li><strong>ignore</strong> - If set to true, any character encoding
 *     specified by the client is ignored, and the value returned by the
 *     <code>selectEncoding()</code> method is set.  If set to false,
 *     <code>selectEncoding()</code> is called <strong>only</strong> if the
 *     client has not already specified an encoding.  By default, this
 *     parameter is set to "true".</li>
 * </ul>
 *
 * <p>Although this filter can be used unchanged, it is also easy to
 * subclass it and make the <code>selectEncoding()</code> method more
 * intelligent about what encoding to choose, based on characteristics of
 * the incoming request (such as the values of the <code>Accept-Language</code>
 * and <code>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 filterConfig = 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);
            }
        }
        /**
         * 用戶認證
         */
        try {
           HttpServletRequest req = (HttpServletRequest) request;
           TblSysUser user = (TblSysUser) req.getSession().getAttribute(
               LoginOper.USER);
           if (user == null && req.getServletPath().indexOf("login") == -1) {//沒有登錄
              req.getRequestDispatcher("/login.jsp").forward(request,response);
              //chain.doFilter(request, response);
           } else {
               chain.doFilter(request, response);
        //每次響應(yīng)之后關(guān)閉Heibernate Session
              HibernateUtil.closeSession();
           }
       } catch (ServletException ex) {
       } catch (IOException 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 if (value.equalsIgnoreCase("yes")) {
            this.ignore = true;
        } else {
            this.ignore = 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
     * <code>null</code>.
     * <p>
     * The default implementation unconditionally returns the value configured
     * by the <strong>encoding</strong> initialization parameter for this
     * filter.
     *
     * @param request The servlet request we are processing
     */
    protected String selectEncoding(ServletRequest request) {
        return (this.encoding);

    }

}

相應(yīng)的web.xml配置如下(這里采用Struts):
  <filter>
    <filter-name>setencodingservlet</filter-name>
    <filter-class>kellerdu.util.SetEncodingServlet</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>GBK</param-value>
    </init-param>
    <init-param>
      <param-name>ignore</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>setencodingservlet</filter-name>
    <url-pattern>*.jsp</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>setencodingservlet</filter-name>
    <url-pattern>*.do</url-pattern>
  </filter-mapping>


本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Mysql與JSP網(wǎng)頁中文亂碼問題的解決方案
Jsp中文亂碼問題的描述及解決方法
Servlet中文亂碼解決
java 解決字符集的亂碼問題
"struts中文問題","struts國際化問題"的終極解決方案
亂碼問題總結(jié)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服