<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>welcome.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head>
<body>
<form action="welcome" method="post">
请输入用户名<input type="text" name="user"><p>
<input type="submit" name="提交">
</form>
</body>
</html>
package com;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;public class WelcomeYou extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException
{
req.setCharacterEncoding("gb2312");
String user=req.getParameter("user");
String welcomeInfo="Welcome you, "+user;
resp.setCharacterEncoding("gb2312");
resp.setContentType("text/html");
PrintWriter out=resp.getWriter();
out.println("<html><head><title>");
out.println("Welcome Page");
out.println("</title></head>");
out.println("<body>");
out.println(welcomeInfo);
out.println("</body></html>");
out.close();
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException
{
doGet(req,resp);
}
}
再MYECLIPSE中测试是乱码
<html>
<head>
<title>welcome.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head>
<body>
<form action="welcome" method="post">
请输入用户名<input type="text" name="user"><p>
<input type="submit" name="提交">
</form>
</body>
</html>
package com;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;public class WelcomeYou extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException
{
req.setCharacterEncoding("gb2312");
String user=req.getParameter("user");
String welcomeInfo="Welcome you, "+user;
resp.setCharacterEncoding("gb2312");
resp.setContentType("text/html");
PrintWriter out=resp.getWriter();
out.println("<html><head><title>");
out.println("Welcome Page");
out.println("</title></head>");
out.println("<body>");
out.println(welcomeInfo);
out.println("</body></html>");
out.close();
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException
{
doGet(req,resp);
}
}
再MYECLIPSE中测试是乱码
String welcomeInfo="Welcome you, "+user;
把这里的user 去掉,尝试一下.如果还有问题,看看浏览器的编码是不是中文.你是打开所有中文都乱吗,找别的中文页试试.
servlet类doGet方法中加上 response.setContentType("text/html;charset=GBK");
<filter>
<filter-name> CharacterEncodingFilter </filter-name>
<filter-class>
...CharacterEncodingFilter
</filter-class>
<init-param>
<param-name> encoding </param-name>
<param-value> GB2312 </param-value>
</init-param>
<init-param>
<param-name> ignore </param-name>
<param-value> true </param-value>
</init-param>
</filter>