Response
package bull04.out;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class OutPrintServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test4(response);
}
/**
* 字符流:
* 设置浏览器打开的时候的编码
* resposne.setHeader(“Content-Type”,”text/html;charset=UTF-8”);
* 设置response的缓冲区的编码
* response.setCharacterEncoding(“UTF-8”);
* @param response
* @throws IOException
*/
private void test4(HttpServletResponse response) throws IOException {
response.setContentType("text/html;charset=UTF-8");
response.getWriter().print("字符流");
}
/**
* 字节流:
* 设置浏览器默认打开的编码:
* resposne.setHeader(“Content-Type”,”text/html;charset=UTF-8”);
* 设置中文字节取出的时候编码.
* “中文”.getBytes(“UTF-8”);
* @param response
* @throws IOException
* @throws UnsupportedEncodingException
*/
private void test3(HttpServletResponse response) throws UnsupportedEncodingException, IOException {
response.setHeader("Content-type", "text/html;charset=UTF-8");
response.getOutputStream().write("字节流".getBytes("UTF-8"));
}
private void test2(HttpServletResponse response) throws IOException {
response.getWriter().print("Hello Response...");
}
private void test1(HttpServletResponse response) throws IOException {
response.getOutputStream().write("Hello OutputStream Response...".getBytes());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
Request
package bull05.Request;
/**
* 解决Request中文乱码问题
*/
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RequestServlet2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* doGet解决方法
*/
String name = new String(request.getParameter("nameget").getBytes("ISO-8859-1"),"UTF-8");
System.out.println("doGet方式:"+name);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* doPost解决方法
*/
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("namepost");
System.out.println("doPost方式:"+name);
}
}