欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java web乱码问题的解决方法

程序员文章站 2022-03-09 22:15:39
...

最近学习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();
		}
	}

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

相关标签: 乱码