Request的重定向方法以及中文顯示問題

前幾天老師給我們的作業當中我發現因爲我們用的瀏覽器的種類不一樣。所以大家都出現了中文變成亂碼的問題。
在LoginServlet這個Servlet這個類當中,通過使用Request的sendRedirect方法重定向一張網頁
我在firefox 瀏覽器中用輸入內容,最後出現了亂碼,然後 又在TT和IE當中試了試,沒有發現這樣的問題。說明firefox對中文支持的不是很好,因此在WEB編程中,希望能加上<meta http-equiv="content-type" content="text/html; charset=gb2312">這一句。這樣用什麼瀏覽器都會是中文的了!


login.html

<html>
    <head>
        <title>登錄頁面</title>
    </head>
    <body>
        <form action="login" method="post">
            <table>
                <tr>
                    <td>請輸用戶名:</td>
                    <td><input type="text" name="user"></td>
                </tr>
                <tr>
                    <td>請輸入密碼:</td>
                    <td><input type="text" name="password"></td>
                </tr>
                <tr>
                    <td><input type="submit" value="登陸"></td>
                    <td><input type="reset" value="重填"></td>
                </tr>
            </table>
        </form>
    </body>
</html>

LoginServlet .java

package com.wizard.chenxi.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet{
    public void doGet(HttpServletRequest req,HttpServletResponse resp)
                    throws ServletException,IOException{
        resp.setContentType("text/html;charset=gb=2312");
        
        String name = req.getParameter("user");
        String pwd = req.getParameter("password");
        
        if(name!=null && pwd!=null && name.equals("Chenxi") && pwd.equals("1234")){
            resp.sendRedirect("success.html");
            
        }else{
            resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,"服務器忙");
        }
    }// end for doGet
    public void doPost(HttpServletRequest req,HttpServletResponse resp)
                    throws ServletException,IOException{
        doGet(req,resp);
    }
}// end for class

success.html

<html>
    <head>
        <title>登錄成功</title>
    </head>
    <body>
        登錄成功,歡迎!
    </body>
</html>

web.xml
。。。。。
。。
   <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.wizard.chenxi.servlet.LoginServlet</servlet-class>
  </servlet>
    <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>

。。。。。



 
發佈了34 篇原創文章 · 獲贊 0 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章