Java web乱码问题的解决方法

最近学习jsp遇到了很多的乱码问题,意识到了工欲善其事,必先利其器。要想不出现乱码,就得面面俱到,要把所有与字符编码有关的东西全部设置为utf-8。

  1. tomcat的编码,找到tomcat的文件夹,在其conf文件夹下的server.xml将解码格式修改为utf-8,像这样:
    <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>

  2. 编译工具的编码,我用的是MyEclipse,eclipse应该也差不多,windows->preferences->workspace,在里面将utf-8改回GBK,因为在实际编码中发现,这里改成utf-8反而会出现乱码。

  3. jsp的编码,同样是windows->preferences->JSP,将Encoding改为UTF-8。具体到某个页面时就将首部改为page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="utf-8"

  4. 而有的时候上述方法依然不能解决问题,那就是传输过程中出了问题,那么就得将后台接收到的数据再次转码,在读取数据的位置加入request.setCharacterEncoding("utf-8") ,下面是我的操作实例。

//前台
<form action="webServiceServlet" method="post">
	    请输入您的问题:
	    	<input type="text" class="acount"  name="answer">
	  		<input type="submit" >
		</form>
//后台修改前
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		Connection conn = new DButil().getConnect();
		
		String sql="select * from help where  question like '%"+new String(request.getParameter("answer")!=null?request.getParameter("answer"):"0")+"%'";
		try {
			Statement st = conn.createStatement();
			System.out.println(sql);
			ResultSet rs = st.executeQuery(sql);
			help help = new help();
			while(rs.next()) {
				help.setId(rs.getInt("id"));
				help.setquestion(rs.getString("question"));
				help.answer(rs.getString("answer"));
				
			}
			//将数据库中查询到的信息封装在help对象中,help对象保存在request中,之后将通过request对象将数据传递到页面
			//需要的话也可以只传递一个参数request.setAttribute("id", help.getId());
			request.setAttribute("help", help);
			//这里是转发,从Servlet跳转到showinfo.jsp页面,并且带上request和response对象中原有的参数
			request.getRequestDispatcher("/Showanswer.jsp").forward(request, response);

		} catch (SQLException e) {
			e.printStackTrace();
		}
//后台修改后
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//request请求时,将存储的客户端向服务端发送的信息转换为utf-8编码
//		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		//response.setCharacterEncoding("utf-8");



		Connection conn = new DButil().getConnect();
		
		String sql="select * from help where  question like '%"+new String(request.getParameter("answer")!=null?request.getParameter("answer"):"0")+"%'";
		try {
			Statement st = conn.createStatement();
			System.out.println(sql);
			ResultSet rs = st.executeQuery(sql);
			help help = new help();
			while(rs.next()) {
				help.setId(rs.getInt("id"));
				help.setquestion(rs.getString("question"));
				help.answer(rs.getString("answer"));
				
			}
			//将数据库中查询到的信息封装在help对象中,help对象保存在request中,之后将通过request对象将数据传递到页面
			//需要的话也可以只传递一个参数request.setAttribute("id", help.getId());
			request.setAttribute("help", help);
			//这里是转发,从Servlet跳转到showinfo.jsp页面,并且带上request和response对象中原有的参数
			request.getRequestDispatcher("/Showanswer.jsp").forward(request, response);

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

之前获得的数据是乱码的,后来修改完之后,终于获得到了正常的结果,就一点小问题,花了我半天的时间。
要有耐心,有耐心,耐心,耐心,耐心。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值