Response

输出字节数据

  • 步骤

    1. 获取字节输出流

    2. 输出数据

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
package cn.itcast.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class demo4 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置字符编码
response.setContentType("text/html;charset=utf-8");

//1.获取字节输出流
ServletOutputStream sos = response.getOutputStream();
//2.输出数据
sos.write("你好".getBytes("utf-8"));
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}

基本上和字符输出一致,只不过使用方法不同

验证码

  1. 本质:图片

  2. 目的: 防止恶意表单注册

演示

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package cn.itcast.web.servlet;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

public class CheckCodeServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.创建一个对象,在内存中图片(验证码的图片对象)
int width=100;
int height=50;

BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//2.美化图片
//2.1 填充背景色
Graphics g = image.getGraphics();//画笔对象
g.setColor(Color.pink);//设置画笔颜色
g.fillRect(0,0,width,height);
//2.2 画边框
g.setColor(Color.BLUE);
g.drawRect(0,0,width-1,height-1);

String str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
//生成随机角标
Random ran=new Random();

for (int i=1;i<=4;i++){
int index = ran.nextInt(str.length());
//获取字符
char ch = str.charAt(index);//随即字符
//2.3 写验证码
g.drawString(ch+"",width/5*i,height/2);
}

//2.4画干扰线
g.setColor(Color.GREEN);

//随机生成坐标点
for(int i=0;i<10;i++){
int x1 = ran.nextInt(width);
int x2 = ran.nextInt(width);

int y1=ran.nextInt(height);
int y2 = ran.nextInt(height);
g.drawLine(x1,x2,y1,y2);
}

//3.将图片输出到页面展示
ImageIO.write(image,"jpg",response.getOutputStream());

}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}

这样我们每刷新一次就会变一次

ServletContext对象

  1. 概念:代表整个web应用,可以和程序的容器(服务器)来通信

  2. 获取:

    1. 通过request对象来获取 request.getServletContext();

    2. 通过HttpServlet获取 this.getServletContext();

  3. 功能:

    1. 获取MIME类型

      • MIME类型:在互联网通信过程中定义的一种文件数据类型

      • 格式: 大类型/小类型 text/html image/jpeg

      • 获取:String getMimeType(String file)

    2. 域对象:共享数据

      1. setAttribute(String name,Object value)

      2. getAttribute(String name)

      3. removeAttribute(String name)

    3. 获取文件的真实(服务器)路径

获取

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
package 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 {
@Override
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 context2 = this.getServletContext();

System.out.println(context1==context2);

}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}

运行结果

1
true

证明两个获取方式是一样的

获取MIME类型

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
36
37
38
package 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 {
@Override
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();

//3. 定义文件名称
String filename="a.jpg";

//4.获取MIME类型
String mimeType = context.getMimeType(filename);
System.out.println(mimeType);

}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}

运行结果

1
image/jpeg

可以在web.xml中查看更多mime类型

域对象

context1

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
package 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 {
@Override
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");

}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}

context2

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
package 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 {
@Override
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);
}

@Override
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
36
package 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 {
@Override
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);
}

@Override
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

会话技术

  1. 会话:一次会话中包含多次请求和响应

    • 一次会话:浏览器第一次给服务器资源发送请求,会话建立,直到有一方断开为止
  2. 功能:在一次会话的范围内,共享数据

  3. 方式:

    1. 客户端会话技术:Cookie

    2. 服务器端会话技术:Session

快速入门

  • 使用步骤:

    1. 创建Cookie对象 new Cookie(String name,String value)

    2. 发送Cookie对象 response.addCookie(Cookie cookie)

    3. 获取Cookie对象,拿到数据 Cookie[] request.getCookies()

demo1创建并发送Cookie

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
package 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 {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.创建Cookie
Cookie c=new Cookie("msg","hello");
//2.发送Coookie
response.addCookie(c);

}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}

demo2获取Cookie

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
package 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 {
@Override
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);
}
}
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}

先访问demo1,再放问demo2
运行结果

1
2
3
JSESSIONID : B0644875B54B93E70FF232624A6F4FA6
msg : hello
Phpstorm-d069594e : 0d583fa6-ad80-4735-a385-c10cee7ee32f

第一个和最后一个都是自带的,不用管,msg是获取到了