首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

请求指点一下jsp乱码(菜鸟)

2012-05-05 
请求指点一下jsp乱码(初学者)先通过一个jsp表单把数据post给servlet,然后servlet把数据存储在Bean中,最后S

请求指点一下jsp乱码(初学者)
先通过一个jsp表单把数据post给servlet,然后servlet把数据存储在Bean中,最后Servlet转发到jsp页面显示数据。

HTML code
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>ues javaBean in jsp and servlet</title></head><body><form action="customer.do" method="post"> <table border="1">   <tr>     <td>客户名</td><td><input type="text" name="custName"></td>     <td>邮件地址</td><td><input type="text" name="email"></td>     <td>电话</td><td><input type="text" name="phone"></td>     <td><input type="submit" value="submit"></td>     <td><input type="reset" value="reset"></td>   </tr> </table></form></body></html>


Servlet:
Java code
package info.control;import info.model.CustomerBean;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;/** * Servlet implementation class CustomerServlet */public class CustomerServlet extends HttpServlet {    private static final long serialVersionUID = 1L;    /**     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)     */    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub    }    /**     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)     */    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub            //设置属性        CustomerBean customer=new CustomerBean();        customer.setCustName(request.getParameter("custName"));        customer.setEmail(request.getParameter("email"));        customer.setPhone(request.getParameter("phone"));        // 在session作用域共享customer对象        HttpSession session=request.getSession();        synchronized(session)        {            session.setAttribute("customer", customer);        }        //转发到jsp页面        RequestDispatcher view =request.getRequestDispatcher("/displayCustomer.jsp");        view.forward(request, response);    }}


javaBean:

Java code
package info.model;public class CustomerBean implements java.io.Serializable {    /**     *      */    private static final long serialVersionUID = 1L;    private String custName;    private String email;    private String phone;    // 属性访问    public String getCustName()    {        return this.custName;    }    public String getEmail()    {        return this.email;    }    public String getPhone()    {        return this.phone;    }    //修改方法    public void setCustName(String custName)    {        this.custName=custName;    }    public void setEmail(String email)    {        this.email=email;    }    public void setPhone(String phone)    {        this.phone=phone;    }    } 



显示数据的jsp:

HTML code
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><jsp:useBean id="customer" class="info.model.CustomerBean" scope="session" >  <jsp:setProperty name="customer" property="*" /></jsp:useBean><html><head><title>display customer</title></head><body> <table border="1">  <tr>      <td>客户名:</td>      <td><jsp:getProperty name="customer" property="custName" /></td>  </tr>  <tr>    <td>地址:</td>    <td><jsp:getProperty name="customer" property="email" /></td>  </tr>  <tr>    <td>电话</td>    <td><jsp:getProperty name="customer" property="phone"/></td>  </tr> </table></body></html>


问题出来了:先访问http://localhost:8080/UseBean/inputCustomer.jsp ,中文显示正常,但是当填完表单提交后最后显示的通过表单传过来的中文都乱码了,比如:我在表单【客户名】一栏填写【张三】,最后这个【张三】就乱码了,我google了一些,但是网上说的好像又适合我这个特例,我刚开始接触javaWEB这块,我对SE中的乱码有一些了解,对于WEB这块的乱码不是很熟悉,想通过这个demo了解熟悉一下WEB中乱码是怎么回事,希望大家指点一下,谢谢了。

[解决办法]
填个英文名看行不行啊 ?
[解决办法]
你在servlet中打印一下吧 应该也是乱码了 

试试这样
Java code
customer.setCustName(new String(request.getParameter("custName").getBytes(),"utf-8"));
[解决办法]
指点指点:指指点点~
把body内容再定义为utf-8试试~
[解决办法]
哈哈,都这样一步步走过来的,虽然说不上深层次的原理,但是可以建议几步:
1.打断点看看CustomerServlet ,request.getParameter( "custName ") 等传过来是否乱码
2.是的话在post 方法第一行添加req.setCharacterEncoding( "utf-8 ");
3.不是的话在跳转之前添加
response.setContentType( "text/html;charset=UTF-8 ");
response.setCharacterEncoding( "UTF-8 ");
4.试试吧,
[解决办法]
通用的解决方案写个过滤器试试
[解决办法]
看看这个博客:http://blog.csdn.net/wenjie4892543/article/details/6697326
[解决办法]
楼主,我的博客:http://blog.csdn.net/wangbaoyin/article/details/7457087
[解决办法]
你项目的编码是什么编码格式,或者说JAVA类文件的编码。是UTF-8吗?
还有就是
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
[解决办法]
导入spring包,在web.xml中加入以下过滤器
XML code
<filter>   <filter-name>characterEncodingFilter</filter-name>    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    <init-param>     <param-name>encoding</param-name>     <param-value>utf8</param-value>    </init-param>   </filter>
[解决办法]
探讨

导入spring包,在web.xml中加入以下过滤器
XML code
<filter>
<filter-name>characterEncodingFilter</filter-name>


<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-……


[解决办法]
5L的方法就可以的撒
[解决办法]
req.setCharacterEncoding( "gbk "); 

[解决办法]
楼主在servlet中加
response.setContentType("text/html;charset=UTF-8");试试

热点排行