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>
<p>要搜索的内容:</p>
<form id="form1" name="form1" method="post" action="RearchServlet">
<p>
<input type="text" name="searchString" />
</label>
<input type="submit" value="提交">
</p>
<p>
<label>
<input type="radio" name="rearchWeb" value="http://www.baidu.com/s?wd=" checked/> 百度
<input type="radio" name="rearchWeb" value="http://www.google.com/search?q="/> Google
</label>
</p>
</form>
</body>
</html>import java.io.*;import javax.servlet.*;
import javax.servlet.http.*;
public class RearchServlet extends HttpServlet{ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out = resp.getWriter();
resp.setContentType("text/html;charset=GB2312");
req.setCharacterEncoding("GB2312");
String searchString = req.getParameter("searchString");
String searchWeb = req.getParameter("rearchWeb");
if((searchString == null) ||(searchString.length()==0) ) {
reportProblem(resp,"Missing engine name");
return;
}
byte b[] = searchString.getBytes("ISO-8859-1");
searchString = new String(b,"GB2312");
if((searchWeb==null)&& (searchWeb.length()==0)) {
reportProblem(resp,"Missing search engine name");
return;
}
//searchString = URLEncoder.encode(searchString);
String searchURL = searchWeb+searchString;
if(searchURL!=null) {
resp.sendRedirect(searchURL);
}
else {
reportProblem(resp,"Unrecognized search engine");
}
out.close();
} protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req,resp);
}
public void reportProblem(HttpServletResponse resp,String message)throws IOException {
resp.sendError(resp.SC_NOT_FOUND,message);
}
}
<!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>
<p>要搜索的内容:</p>
<form id="form1" name="form1" method="post" action="RearchServlet">
<p>
<input type="text" name="searchString" />
</label>
<input type="submit" value="提交">
</p>
<p>
<label>
<input type="radio" name="rearchWeb" value="http://www.baidu.com/s?wd=" checked/> 百度
<input type="radio" name="rearchWeb" value="http://www.google.com/search?q="/> Google
</label>
</p>
</form>
</body>
</html>import java.io.*;import javax.servlet.*;
import javax.servlet.http.*;
public class RearchServlet extends HttpServlet{ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out = resp.getWriter();
resp.setContentType("text/html;charset=GB2312");
req.setCharacterEncoding("GB2312");
String searchString = req.getParameter("searchString");
String searchWeb = req.getParameter("rearchWeb");
if((searchString == null) ||(searchString.length()==0) ) {
reportProblem(resp,"Missing engine name");
return;
}
byte b[] = searchString.getBytes("ISO-8859-1");
searchString = new String(b,"GB2312");
if((searchWeb==null)&& (searchWeb.length()==0)) {
reportProblem(resp,"Missing search engine name");
return;
}
//searchString = URLEncoder.encode(searchString);
String searchURL = searchWeb+searchString;
if(searchURL!=null) {
resp.sendRedirect(searchURL);
}
else {
reportProblem(resp,"Unrecognized search engine");
}
out.close();
} protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req,resp);
}
public void reportProblem(HttpServletResponse resp,String message)throws IOException {
resp.sendError(resp.SC_NOT_FOUND,message);
}
}
你看一下对你有没有用:
1.在页面设置编码方式:UTF-8
2.写一个字符编码过滤器,过滤器中写下如下代码即可://将对象转换为HTTP
HttpServletRequest request = (HttpServletRequest)arg0;
HttpServletResponse response = (HttpServletResponse)arg1;
//设置字符编码属性
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
3.在tomcat设置端口号的地方的后面 写下如下代码:
URIEncoding=“UTF-8”
4.在传参数之前,先将字符转码:
URLEncoder.encode("要传递的参数","UTF-8");
5.先将字符转换成进制码 然后要用的时候在转换回来
。。
String searchString = req.getParameter("searchString");
String searchWeb = req.getParameter("rearchWeb");
你这两个参数可以完全获取吗?
resp.setCharacterEncoding("gb2312");
req.setCharacterEncoding("gb2312");
试一下!