日期:2014-05-18  浏览次数:20774 次

如何才能通过POST或GET正确获得中文输出?
servlet:

package cc.openhome;

import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class HelloServlet
 */
@WebServlet("/encoding")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
    @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExceptionIOException {
// TODO Auto-generated method stub
     String name = request.getParameter("nameGet");
     name = new String(name.getBytes("ISO-8859-1"), "UTF-8");
    
     PrintWriter out = response.getWriter();
     response.setContentType("text/html;charset=utf-8");
     out.println("<html>");
     out.println("<head>");
     out.println("<title>Hello</title>");
     out.println("</head>");
     out.println("<body>" + name + "</body>");
     out.println("</html>");
}
    /**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
    @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
     request.setCharacterEncoding("UTF-8");