request和response解决乱码问题

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 {
//      test1(response);
//      test2(response);
        //test3(response);
        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);

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值