读书人

怎么用servlet转换汉字编码

发布时间: 2012-01-10 21:26:51 作者: rapoo

如何用servlet转换汉字编码,
如何将汉字转换成搜索引擎所需要的url编码,比如百度。
这是我的代码,用英文关键字可以,但中文关键字就是乱码
只帖doGet()出来
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String searchString = request.getParameter( "searchString ");
if(searchString == null||searchString.length()==0){
reportProblem(response, "Missing search string. "); //reportProblem()是一个自己定义的方法
//返回错误信息。
return;
}
searchString =URLEncoder.encode(searchString, "GBK "); //将seachStirng中的特殊字符转化成地址栏中字符,
//如空格转化成+号.
String numResults = request.getParameter( "numResults ");
if(numResults == null || numResults.equals( "0 ")||numResults.length()==0)
{
numResults= "10 " ; //如果没有传递这个分页参数,就默认(搜索到的记录)第页为10个记录;
}
String searchEngine =request.getParameter( "searchEngine ");
if(searchEngine==null)
{
reportProblem(response, "Missing search engine! ");
}
//自己定义的类,SearchSpe完成构建完成的URL搜过路径!,让我们能搜索到信息。
SearchSpec[] commonSpecs = SearchSpec.getCommonSpecs();
for(int i=0;i <commonSpecs.length;i++)
{
SearchSpec searchSpec =commonSpecs[i];
if(searchSpec.getName().equals(searchEngine))
{
String url = searchSpec.makeURL(searchString,numResults);
response.sendRedirect(url);
return;
}
}//for
reportProblem(response, "Unrecognized search engine. ");
}
private void reportProblem(HttpServletResponse response,String message)
throws IOException { //自己定义的返回出错信息
response.sendError(response.SC_NOT_FOUND, " <H2> Error: "+message+ " </H2> ");
}

//searchspec类
public class SearchSpec {
private String name, baseURL, numResultsSuffix;



private static SearchSpec[] commonSpecs = { //q后面跟要搜索的东西!,num后,跟页数
new SearchSpec( "google ", "http://www.google.com/search?q= ", "&num= "),
new SearchSpec( "infoseek ", "http://infoseek.go.com/Titles?qt= ",
"&nh= "),
new SearchSpec( "lycos ", "http://lycospro.lycos.com/cgi-bin/ "
+ "pursuit?query= ", "&nmaxhits= "),
new SearchSpec( "hotbot ", "http://www.hotbot.com/?MT= ", "&DC= ") };

public SearchSpec(String name, String baseURL, String numResultsSuffix) {
this.name = name;
this.baseURL = baseURL;
this.numResultsSuffix = numResultsSuffix;

}

public String makeURL(String searchString, String numResults) {
return (baseURL + searchString + numResultsSuffix + numResults);
}

public String getName() {
return name;
}

public static SearchSpec[] getCommonSpecs() {
return commonSpecs;
}

}
//html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN " "http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=UTF-8 ">
<title> Insert title here </title>
</head>
<body>
<form method = post action= "./SearchEnginesServlet ">
Search String:
<input type= "text " name= "searchString "> <br>
<input type= "text " name= "numResults " value=10 size=3> <br> <!--搜索到的,每页显示多少记录-->
<input type = "radio " name= "searchEngine " value= "google "> Google|
<input type = "radio " name= "searchEngine " value= "infoseek "> Infoseek|
<input type = "radio " name= "searchEngine " value= "hotbot "> HotBot
<br>
<input type = "submit " value= "Search ">
</form>
</body>
</html>

[解决办法]
request.setCharacterEncoding( "gb2312 ")不好用吗?

读书人网 >Java Web开发

热点推荐