qq_41830943
漆黑的羊驼
采纳率0%
2018-12-22 16:32

经servlet跳转后,跳转后的jsp页面原页面上的中文问号乱码

学生党,在做期末的课程设计

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        execute(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        execute(req, resp);
    }

    public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if (request.getParameter("ID").equals("admin") && request.getParameter("password").equals("admin")){
            request.getRequestDispatcher("../administrator.jsp").forward(request,response);
        }else {
            StudentAccount account = new StudentAccount();
            AdminDAO adminDAO = new AdminDAO();
            account.setID(Integer.valueOf(request.getParameter("ID")));
            account.setPassword(request.getParameter("password"));
            StudentAccount stu = adminDAO.findStuByID(account.getID());
            PrintWriter writer = response.getWriter();
            if (stu == null) {
                writer.print("<script language='javascript'>" +
                        "alert('The account does not exist!');" +
                        "window.location.href='../index.jsp';" +
                        "</script>");
            } else {
                if (account.getID() == stu.getID() && account.getPassword().equals(stu.getPassword())){
                    request.getRequestDispatcher("../studentInfo.jsp").forward(request, response);
                }else {
                    writer.print("<script language='javascript'>" +
                            "alert('Wrong password!');" +
                            "window.location.href='../index.jsp';" +
                            "</script>");
                }
            }

        }
    }

这是我的LoginServlet,用于登录,然后奇怪的是,假如是登录到administrator.jsp页面的话是正常的,但是跳转到studentInfo页面上,原页面上的中文就直接乱码了,图片说明
然后就是直接进入那个页面的话,中文就是没有乱码的
图片说明
萌新求解

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • qq_40391363 大大泡泡糖1996 3年前

    在execute方法里加上request.setCharacterEncoding("utf-8");和response.setContentType("text/html; charset=utf-8");
    PS:如果不起作用,可以 new String(参数.getBytes,"编码")
    再者你可以在Post或者Get任意方法里写操作就行了,然后在另外一个方法调用写好的,不用第三个方法了。如:
    doGet(){
    doPost()
    }这种形式

    点赞 1 评论 复制链接分享
  • weixin_43312564 魔龍 3年前

    解决post请求中的中文乱码问题:
    在doGet()方法里面加入:request.setCharacterEncoding("utf-8");

    点赞 评论 复制链接分享

相关推荐