前几天老师给我们的作业当中我发现因为我们用的浏览器的种类不一样。所以大家都出现了中文变成乱码的问题。
在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>
。。。。。
在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>
。。。。。