servlet吧 关注:1,441贴子:1,219
  • 3回复贴,共1

求高人解答,servlet 中文传值jsp全部变成问号的问题

只看楼主收藏回复

首先 我用的Netbeans 没有任何问题 可以传出 中文。
package edu.ahau.servlet;
import edu.ahau.entity.Student;
import static edu.ahau.utils.JdbcUtils.closeAll;
import static edu.ahau.utils.JdbcUtils.getConn;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
PrintWriter out = response.getWriter();
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String fid1 = request.getParameter("fid");
int fid = Integer.parseInt(fid1);
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from Student where id = "+fid;
try {
conn = getConn();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
Student p = null;
if(rs.next()){
p = new Student();
p.setId(rs.getInt("id"));
p.setName(rs.getString("name"));
p.setGender(rs.getString("gender"));
p.setAddress(rs.getString("address"));
p.setEmail(rs.getString("email"));
p.setGradeid(rs.getString("gradeId"));
request.setAttribute("p", p);
System.out.println(p);
request.getRequestDispatcher("studentinfo.jsp").forward(request, response);
}else{
out.print("<script language='javascript'>alert('中文');</script>");
}
} catch (Exception e) {
throw new RuntimeException(e);
}finally{
closeAll(rs, ps, conn);
out.close();
}
}
}

但是 同样的代码 在myecplise中 就出了问题 中文全变成了 问号

然后必须把response.setContentType("text/html;charset=UTF-8"); 中的 utf-8改成gbk或者gb2312
请问这是为什么啊!!!求大佬解答。


IP属地:上海1楼2017-06-29 11:44回复
    用过一次 gbk后 改成utf-8 就能够显示中文了。。。


    IP属地:上海2楼2017-06-29 11:53
    回复
      页面的提交方式 改成post


      3楼2017-11-22 15:50
      回复
        乱码了


        来自Android客户端4楼2017-12-02 21:20
        回复