域对象获取全局配置信息与服务器上文件真实路径
(1.域对象(在一定范围内 存储信息的对象)
ServletContext 范围:整个程序中都可以访问到 并且只有一个(单例对象)
每个servlet都可以访问到这个域对象
获取ServletContext对象的方式
方式1:从ServletConfig对象中获取
方式2:从父类中直接获取
注意:所有的域对象都有 设置 获取 删除的方法
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ServletContext application = this.getServletConfig().getServletContext();
application.setAttribute("username,"zhangsan");
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Object object = this.getservletContext().getAttribute("username");
System.out.println(object);
(2.设置全局配置信息
在web.xml中进行全局信息的配置
在<servlet></servlet>外添加全局信息
<context-param>
<param-name>key</param-name>
<param-value>value</param-value>
</context-param>
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext application = this.getServletContext();
String value = application.getInitParameter("key");
System.out.println(value);
}
(3.获取服务器上的真实路径 并读取
使用Conttext域对象获取 可以获取到服务器上任意资源路径
private void fun1() throws IOException, FileNotFoundException {
ServletContext application = this.getservletContext();
String path = application.getRealPath("/WEB/INF?classes/a.properties");
Properties properties = new Properties();
properties.load(new FileInputStream(path));
System.out.println(properties.getProperty("key"));
}
请求转发与请求重定向
(4.请求转发与请求重定向
public class Test01 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("要借钱");
System.out.println("没有 找test02")
ServletContext application = this.getServletContext();
RequestDispatcher dispatcher = application.getRuquestDispatcher();
dispatcher.forward(request,response);
System.out.println("钱已搞定");
public class Test02 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("有钱 这是test02");
执行结果:
要借钱
没有 找test02
有钱 这是test02
钱已搞定
浏览器发起请求(请求Servlet)
Servlet给浏览器一个响应
在响应中会携带一个重定向响应头(头中有重定向的访问地址)
浏览器接到这个响应后 发现重定向响应头
再一次发起请求 去访问重定向响应头中的地址
public class Test03 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("要借钱");
System.out.println("没有 去找test04");
rewponse.setHeader("location","/sh-web-servlet/test04");
response.setStatus(302);
System.out.println("借钱去了");
}
public class Test04 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(钱被借走);
执行结果:
要借钱
没有 去找test04
借钱去了
钱被借走
服务器响应对象
(5.HTTPServletResponse 服务器响应对象
响应对象中有:
响应行 http/1.1 状态码200
响应头 告诉浏览器要做什么 例如响应给你的文件需要下载 以什么编码格式解析数据
响应体 响应回浏览器的数据
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置服务器的编码格式 tomcat默认的编码格式 iso-8859-1
// response.setCharacterEncoding("UTF-8")
// 告诉浏览器要使用什么编码格式来查看 添加响应头
// response.setHeader("Content-type","text/html;charset=UTF-8")
// 代替以上两句的方法
response.setContextType("text/html;charset=UTF-8")
// 给浏览器响应一句话
// 从响应对象 HttpServletResponse 中获取对象
// 注意:这个流对象不是自己创建 要从响应中获取
PrintWriter out = response.getWrite()
out.write("很开心")
}
// 刷新响应
private void fun1(HttpServletResponse response) throws IOException {
// 添加刷新头(每秒刷新一次)
response.setIntHeader("refresh",1)
// 添加随机数
response.getWriter().write(Math.random() + "")
}
// 跳转响应
private void fun3(HttpServletResponse response) throws IOException {
// 设置响应的字符集
response.seetContentType("text/html;charset=UTF-8")
// 3秒后 跳转一个请求地址
response.setHeader("refresh","3;url=http://www.baidu.com")
response.getWriter().write("3秒后 跳转 很开心")
服务器上下载文件
(6.下载文件
用户发送请求 请求到访问Servlet
Servlet处理请求(把服务器上的图片 以流的形式 使用response 响应给用户浏览器)
首先 把名字为 团子大家族.png的图片放到工程src文件夹下
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取服务器上的图片路径
String path = this.getServletContext().getRealPath("/WEB-INF/classes/团子大家族.png")
// 字符串切割 获取图片的名字
int index = path.lastIndexof("/")
String filename = path.substring(index + 1)
// 修改文件名字的字符集
filename = new String(filename.getBytes(),"iso-8859-1")
// 添加响应头(需要拼接文件的名字)
response.setHeader("content-disposition","attachment;filename=" + filename)
// 告诉浏览器文件下载的格式 添加响应头
response.setHeader("content-type","image/png")
// 从服务器中读取图片 边读边写
FineInputStream fis = new FileInputstream(path)
// 注意: 需要获取response当中 字节流
ServletOutputstream sos = response.getOutputStream()
int len = 0
byte[] b = new byte[1024]
while ((len = fis.read(b)) != -1) {
// 响应回浏览器
// 如果只是单纯的把图片响应回去
// 浏览器并不知道你要干什么(下载或浏览)
// 需要通过响应头 通知浏览器 该文件是给你下载用的
sos.write(b, 0, len)
}
// 注意:自己创建的流 自己关
fis.close()
}