请求指点一下jsp乱码(初学者)
先通过一个jsp表单把数据post给servlet,然后servlet把数据存储在Bean中,最后Servlet转发到jsp页面显示数据。
<%@ 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>
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); }}
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; } }
<%@ 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>
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中加入以下过滤器
<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>
[解决办法]