菜鸟学Android笔记(二十六):Response数据输出

一、Response的概念

response主要是向客户端输出内容,也就是你输出什么,客户端就显示什么。

setCharactorEncoding是设置字符集,如出现汉字,以UTF8进行编码。
SetContentType 设置页面的ContentType为text/html; 字符集为utf-8

注意:什么是servlet容器

servlet没有main()方法。他们受控于另一个Java应用,这个Java应用成为容器。servlet容器指的是部署servlet的容器,比如tomcat。需要运行在servlet容器中

二、response------响应的意思,就是将服务器对浏览器的响应封装成这个对象。

              |-----HttpServletResponse是实现response的通用类

三、入门案例:如何向客户端(浏览器)输出一段文本?

第一步:建立一个OutServlet.java在classes文件中

package com.java;

import java.io.IOException;

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

public class OutServlet extends HttpServlet{

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.getOutputStream().write("sdfjksdlfjsjf".getBytes());
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req,resp);
	}
	
}

第二步:在web.xml中部署访问路径

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  
  <servlet>
        <servlet-name>OutServlet</servlet-name>
        <servlet-class>com.java.OutServlet</servlet-class>
    </servlet>
     <servlet-mapping>
        <servlet-name>OutServlet</servlet-name>
        <url-pattern>/java/OutServlet</url-pattern>
    </servlet-mapping>
  
</web-app>
第三步:在浏览器中输入“http://localhost/Day04/java/OutServlet“回车浏览器便会输出

如果输出的是中文会不会乱码?

package com.java;

import java.io.IOException;

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

public class OutServlet extends HttpServlet{

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.getOutputStream().write("中文".getBytes());
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req,resp);
	}
	
}
在浏览器中输入回车,可以看出,并没有乱码,原因是浏览器用的是GBK,服务器设置的是浏览器默认的编码

如果设置服务器向浏览器输出的数据是UTF-8编码会怎么样?(这个时候就显示response在浏览器编码设置的作用了,如何设置呢?看下面OutServlet.java的编码)

package com.java;

import java.io.IOException;

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

public class OutServlet extends HttpServlet{

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.getOutputStream().write("中国".getBytes("utf-8"));
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req,resp);
	}
	
}

当然不出所料,浏览器显示的是乱码

如何解决这个问题?只要我们在服务器向浏览器输出数据时,提醒浏览器用UTF-8来接收就可以了

package com.java;

import java.io.IOException;

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

public class OutServlet extends HttpServlet{

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.getOutputStream().write("中国".getBytes("utf-8"));
		resp.setHeader("Content-Type", "text/html;charset=utf-8");
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req,resp);
	}
	
}

我们可以直接设置服务器解析的编码为UTF-8,而不用设置成这样:
"中国".getBytes("utf-8")
代码如下:

package com.java;

import java.io.IOException;

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

public class OutServlet extends HttpServlet{

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.getOutputStream().write("中国".getBytes());
		//设置将发送到客户端的响应的字符编码
		resp.setCharacterEncoding("utf-8");
		
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req,resp);
	}
	
}

但此时为什么没有乱码?(原因可以根据服务器是什么编码而默认成什么编码)

但我们为了规范,任然要设置浏览器接收的编码:

package com.java;

import java.io.IOException;

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

public class OutServlet extends HttpServlet{

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//给浏览器设置utf-8,可以省去
		resp.setHeader("Content-Type", "text/html;charset=utf-8");
		//可以简写成为这样:
		//resp.setContentType("text/html;charset=utf-8");
		//给服务器设置UTF-8
		resp.setCharacterEncoding("utf-8");
		
		
		resp.getWriter().write("中国");//服务器默认的是iosXX,即非UTF-8
		
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req,resp);
	}
	
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值