当前位置: 代码迷 >> JavaScript >> JSP页面乱码有关问题
  详细解决方案

JSP页面乱码有关问题

热度:226   发布时间:2012-11-09 10:18:48.0
JSP页面乱码问题
<a href="Test?method=河南省郑州市">测试</a>

//servlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}
public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String method=request.getParameter("method");
		method=this.convert(method);
		out.print(method);
		System.out.println("编码转换之后:"+method);
		out.flush();
		out.close();
	}
//编码转换方法
public String convert(String target){
		System.out.println("编码转换之前:" + target);
		try {
			return new String(target.trim().getBytes("ISO-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			return target;
		}
	}
  相关解决方案