servlet乱码 解决方法 2种方法
生活随笔
收集整理的這篇文章主要介紹了
servlet乱码 解决方法 2种方法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
public class ResponseDemo1 extends HttpServlet {public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {test1(resp);}
//方法1:public void test1(HttpServletResponse resp) throws IOException, UnsupportedEncodingException {resp.setHeader("Content-type", "text/html;charset=utf-8");String data = "中國";OutputStream output = resp.getOutputStream();//程序以什么編碼輸出,那么一定要設(shè)置瀏覽為相對(duì)應(yīng)的編碼打開.output.write(data.getBytes("utf-8"));}//方法2://模擬meta標(biāo)簽,設(shè)置charset為utf-8,這個(gè)方法也行. //用html技術(shù)中的meta標(biāo)簽?zāi)M了一個(gè)http響應(yīng)頭,來控制瀏覽器的行為public void test2(HttpServletResponse response) throws Exception{String data = "中國_第二個(gè)";OutputStream output= response.getOutputStream();output.write("<meta http-equiv='content-type' content='text/html;charset=UTF-8'>".getBytes());output.write(data.getBytes());}
?
//方法1:public void test1(HttpServletResponse resp) throws IOException, UnsupportedEncodingException {resp.setHeader("Content-type", "text/html;charset=utf-8");String data = "中國";OutputStream output = resp.getOutputStream();//程序以什么編碼輸出,那么一定要設(shè)置瀏覽為相對(duì)應(yīng)的編碼打開.output.write(data.getBytes("utf-8"));}//方法2://模擬meta標(biāo)簽,設(shè)置charset為utf-8,這個(gè)方法也行. //用html技術(shù)中的meta標(biāo)簽?zāi)M了一個(gè)http響應(yīng)頭,來控制瀏覽器的行為public void test2(HttpServletResponse response) throws Exception{String data = "中國_第二個(gè)";OutputStream output= response.getOutputStream();output.write("<meta http-equiv='content-type' content='text/html;charset=UTF-8'>".getBytes());output.write(data.getBytes());}
?
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }?//********************************情況2:*************************************
public class ResponseDemo2 extends HttpServlet {private static final long serialVersionUID = 1L;public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {test1(response);}public void test1(HttpServletResponse response) throws IOException {// 方法1:// 要設(shè)置response,所使用的碼表,以控制reponse以什么碼表向?yàn)g覽器寫入數(shù)據(jù)// response.setCharacterEncoding("utf-8");// 同時(shí)設(shè)置瀏覽器以何種碼表打開,指定瀏覽以什么 碼表打開服務(wù)器發(fā)送的數(shù)據(jù) // response.setHeader("content-type", "text/html;charset=utf-8");// 方法2:response.setContentType("text/html;charset=utf-8");String data = "中國";PrintWriter outputStream = response.getWriter();outputStream.write(data);}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}?
?
總結(jié)
以上是生活随笔為你收集整理的servlet乱码 解决方法 2种方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos关于”running yum
- 下一篇: 连着三天梦到同一个人是什么意思