Response
输出字节数据
步骤
获取字节输出流
输出数据
1 | package cn.itcast.web.servlet; |
基本上和字符输出一致,只不过使用方法不同
验证码
本质:图片
目的: 防止恶意表单注册
演示
1 | package cn.itcast.web.servlet; |
这样我们每刷新一次就会变一次
ServletContext对象
概念:代表整个web应用,可以和程序的容器(服务器)来通信
获取:
通过request对象来获取 request.getServletContext();
通过HttpServlet获取 this.getServletContext();
功能:
获取MIME类型
MIME类型:在互联网通信过程中定义的一种文件数据类型
格式: 大类型/小类型 text/html image/jpeg
获取:String getMimeType(String file)
域对象:共享数据
setAttribute(String name,Object value)
getAttribute(String name)
removeAttribute(String name)
获取文件的真实(服务器)路径
获取
1 | package cn.itcast.web.servletcontext; |
运行结果1
true
证明两个获取方式是一样的
获取MIME类型
1 | package cn.itcast.web.servletcontext; |
运行结果1
image/jpeg
可以在web.xml中查看更多mime类型
域对象
context11
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34package cn.itcast.web.servletcontext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Context1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/**ServletContext对象获取
* 1.通过request对象获取
* request.getServletContext();
* 2. 通过HttpServlet获取
* this.getServletContext();
*/
// //1. 通过request对象获取
// ServletContext context1= request.getSession().getServletContext();
//2. 通过HttpServlet获取
ServletContext context = this.getServletContext();
//设置数据
context.setAttribute("msg","haha");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
context21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34package cn.itcast.web.servletcontext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Context2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/**ServletContext对象获取
* 1.通过request对象获取
* request.getServletContext();
* 2. 通过HttpServlet获取
* this.getServletContext();
*/
// //1. 通过request对象获取
// ServletContext context1= request.getSession().getServletContext();
//2. 通过HttpServlet获取
ServletContext context = this.getServletContext();
//获取数据
Object msg = context.getAttribute("msg");
System.out.println(msg);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
我们并没有做任何的转发或者重定向的操作
只需要先访问context1设置键值对,再访问context2便会有了
也就是共享了所有用户的请求数据,这个对象很不安全,而且生命周期长,容易造成内存占用大和一系列安全问题
获取文件服务路径
- 方法:String getRealPath(String path);
分别在src,web,和WEB-INF下创建三个文件,分别为a.txt,b.txt,c.txt
以b.txt为例1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36package cn.itcast.web.servletcontext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
public class Context3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/**ServletContext对象获取
* 1.通过request对象获取
* request.getServletContext();
* 2. 通过HttpServlet获取
* this.getServletContext();
*/
// //通过request对象获取
// ServletContext context1= request.getSession().getServletContext();
//通过HttpServlet获取
ServletContext context = this.getServletContext();
//获取文件的服务器路径
String realPath = context.getRealPath("/b.txt");
System.out.println(realPath);
File file=new File(realPath);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
运行结果1
F:\apache-tomcat-8.5.57-windows-x64\apache-tomcat-8.5.57\webapps\response\b.txt
c.txt只需要改为/WEB-INF/c.txt
a.txt比较麻烦,src下的所有东西都将放到WEB-INF下的classes,需要改为/WEB-INF/classes/a.txt
会话技术
会话:一次会话中包含多次请求和响应
- 一次会话:浏览器第一次给服务器资源发送请求,会话建立,直到有一方断开为止
功能:在一次会话的范围内,共享数据
方式:
客户端会话技术:Cookie
服务器端会话技术:Session
快速入门
使用步骤:
创建Cookie对象 new Cookie(String name,String value)
发送Cookie对象 response.addCookie(Cookie cookie)
获取Cookie对象,拿到数据 Cookie[] request.getCookies()
demo1创建并发送Cookie1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27package cn.itcast.cookie;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Cookie快速入门
*/
public class demo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.创建Cookie
Cookie c=new Cookie("msg","hello");
//2.发送Coookie
response.addCookie(c);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
demo2获取Cookie1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30package cn.itcast.cookie;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class demo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//3.获取Cookie
Cookie[] cs = request.getCookies();
//遍历Cookie
if(cs!=null){
for (Cookie c : cs) {
String name = c.getName();
String value = c.getValue();
System.out.println(name +" : "+ value);
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
先访问demo1,再放问demo2
运行结果1
2
3JSESSIONID : B0644875B54B93E70FF232624A6F4FA6
msg : hello
Phpstorm-d069594e : 0d583fa6-ad80-4735-a385-c10cee7ee32f
第一个和最后一个都是自带的,不用管,msg是获取到了