MyEclipse6+TomCat 6 下JSP乱码问题解决办法

MyEclipse6+TomCat 6 下JSP乱码问题解决办法

配置TomCat和MyEclipse6
新建立一个Servlet
HTML页代码如:

< form action = " ./FormServlet "  method = " get " >
    get
< input name = " username " >
    
< input type = " submit " >
    
</ form >
    
< form action = " ./FormServlet "  method = " post " >
    post
< input name = " username " >
    
< input type = " submit " >
    
</ form >
servlet下JSP代码如:
public   void  doGet(HttpServletRequest request, HttpServletResponse response)
            
throws  ServletException, IOException  {
request.setCharacterEncoding(
"GBK");
        response.setContentType(
"text/html;charset=GBK");
        PrintWriter out 
= response.getWriter();
        out
                .println(
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println(
"  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println(
"  <BODY>");
        out.print(
"    您输入的是 ");
        String username 
= request.getParameter("username");
        username   
= new String(username.getBytes("ISO8859-1"),"GBK");
        out.print(username);
        
        out.println(
", using the GET method");
        out.println(
"  </BODY>");
        out.println(
"</HTML>");
        out.flush(); 
        out.close();
    }

    
public   void  doPost(HttpServletRequest request, HttpServletResponse response)
            
throws  ServletException, IOException  {
        request.setCharacterEncoding(
"GBK");
        response.setContentType(
"text/html;charset=GBK");
        PrintWriter out 
= response.getWriter();
        out
                .println(
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println(
"  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println(
"  <BODY>");
        out.print(
"    您输入的是");
        out.print(request.getParameter(
"username"));
        out.println(
", using the POST method");
        out.println(
"  </BODY>");
        out.println(
"</HTML>");
        out.flush();
        out.close();
    }

HTML部分增加:
< meta http - equiv = " content-type "  content = " text/html;charset=GBK " >
即可解决汉字乱码问题

你可能感兴趣的:(MyEclipse6+TomCat 6 下JSP乱码问题解决办法)