数据加载中……

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">
即可解决汉字乱码问题

posted on 2008-05-09 14:45 曼迪 阅读(530) 评论(1)  编辑  收藏 所属分类: JSP

评论

# re: MyEclipse6+TomCat 6 下JSP乱码问题解决办法[未登录]  回复  更多评论   

有时这个也不能解决问题的!
2008-09-06 21:28 | aa

只有注册用户登录后才能发表评论。


网站导航: