关于Java Servlet 的编码的问题!
我用MYEclipse编一个输入用户名和密码的程序,用Servlet实现得到该用户名和密码,并用浏览器浏览,通过jsvalidation来验证,运行之后输入英文字母可以实...
我用MYEclipse编一个输入用户名和密码的程序,用Servlet实现得到该用户名和密码,并用浏览器浏览,通过jsvalidation来验证,运行之后输入英文字母可以实现,但是不能输入中文……html文件为login.html;servlet为loginservlet.java……
login.html代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<script language="javascript" src="/webproject/js/validation-framework.js"></script>
<p>请输入用户名和密码:</p>
<form id="form1" name="form1" method="post" action="/webproject/servlet/login" onsubmit="return doValidate(this)">
<table width="232" border="0">
<tr>
<th width="77" scope="row">用户名:</th>
<td width="145"><label>
<input name="username" type="text" id="username" size="18" maxlength="20" />
</label></td>
</tr>
<tr>
<th scope="row">密码:</th>
<td><input name="password" type="password" id="password" size="18" maxlength="20" /></td>
</tr>
<tr>
<th scope="row"><label>
<input type="submit" name="Submit" value="提交" />
</label></th>
<td><label>
<input name="reset" type="reset" id="reset" value="重置" />
</label></td> </tr> </table></form><p> </p></body></html>
loginservlet.java代码如下:
package com.sheshe;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = -6488843091690538218L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
request.setCharacterEncoding("utf-8");
out.println("input username:"+request.getParameter("username")+"<br>");
out.println("input password:"+request.getParameter("password")+"<br>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
} 展开
login.html代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<script language="javascript" src="/webproject/js/validation-framework.js"></script>
<p>请输入用户名和密码:</p>
<form id="form1" name="form1" method="post" action="/webproject/servlet/login" onsubmit="return doValidate(this)">
<table width="232" border="0">
<tr>
<th width="77" scope="row">用户名:</th>
<td width="145"><label>
<input name="username" type="text" id="username" size="18" maxlength="20" />
</label></td>
</tr>
<tr>
<th scope="row">密码:</th>
<td><input name="password" type="password" id="password" size="18" maxlength="20" /></td>
</tr>
<tr>
<th scope="row"><label>
<input type="submit" name="Submit" value="提交" />
</label></th>
<td><label>
<input name="reset" type="reset" id="reset" value="重置" />
</label></td> </tr> </table></form><p> </p></body></html>
loginservlet.java代码如下:
package com.sheshe;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = -6488843091690538218L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
request.setCharacterEncoding("utf-8");
out.println("input username:"+request.getParameter("username")+"<br>");
out.println("input password:"+request.getParameter("password")+"<br>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询