在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>
。。。。。