2 okaxina OKAXINA 于 2016.05.04 17:33 提问

Servlet、html的编码问题

编写了一个html文件,直接使用浏览器打开之后是我期望的显示“欢迎光临”
而在一个Servlet中使用请求转发,forward(request,response),跳转的页面却是乱码。
这是为什么呢?

index.html

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <h1>欢迎光临!</h1>
    </body>
</html>

HandleLoginServlet.java

 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author OKAXINA
 */
@WebServlet(urlPatterns = {"/HandleLoginServlet"})
public class HandleLoginServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try{
            /* TODO output your page here. You may use following sample code. */
            String username = request.getParameter("username");
            String userpass = request.getParameter("userpass");
            if("admin".equalsIgnoreCase(username) && "1234".equals(userpass)){
                RequestDispatcher rd = request.getRequestDispatcher("index.html");
                rd.forward(request, response);
            }else{
                out.print("用户名或密码错误,请重新输入!");
                RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
                rd.include(request, response);
            }
        }finally{
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

3个回答

CSDNXIAOD
CSDNXIAOD   2016.05.04 17:42

HTML编码问题
关于html、servlet、msql中的编码问题
Servlet的编码问题
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

qinjisheng_11
qinjisheng_11   2016.05.04 23:57

试试request.setCharacterEncoding("GBK");

OKAXINA
OKAXINA 设置成GBK也没用,而且就算有用也想知道UTF-8应该怎么弄
一年多之前 回复
qq_22792869
qq_22792869   2016.07.18 19:20

你可以右键用记事本打开再另存为 保存对话框最下面一栏有一个编码格式换成utf-8试试

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!