如何用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