JavaWeb基础

1.ServletContext

  1. web容器在启动时候,它为每个web程序都创建了一个对应的ServletContext对象。
  2. ServletContext对象代表了当前的web应用。
  3. 通过ServletContext接口做如下操作,此外还有很多见源码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    ServletContext context = this.getServletContext();
    //共享资源
    context.getAttribute();
    //读取资源文件
    context.getResourceAsStream();
    //请求转发
    context.getRequestDispatcher();
    //获取初始化参数
    context.getInitParameter();

1.1共享数据

共享类

1
2
3
4
5
//1.this调用ServletContext对象用于设置各种资源
ServletContext con = this.getServletContext();
String username = "李晓明";
//设置义一个属性以键值对的方式,作为共享资源
con.setAttribute("username",username);

接收共享类

1
2
3
4
5
6
7
//1.同样获取ServletContext对象
ServletContext con = this.getServletContext();
//2.通过键获取值
String username = (String) con.getAttribute("username");
//3.把数据在网页输出
PrintWriter out = resp.getWriter();
out.println(username);

1.2读取资源文件

1
2
3
4
5
6
7
8
9
10
11
ServletContext sct = this.getServletContext();
//1.通过context获取一个流对象,指定配置文件在web中的路径
InputStream in = sct.getResourceAsStream("/WEB-INF/classes/jdbc.properties");
//2.读取流文件
Properties prop = new Properties();
prop.load(in);
//3.输出到前端页面
PrintWriter out = resp.getWriter();
out.println(prop.getProperty("driverClassName"));
out.println(prop.getProperty("username"));
out.println(prop.getProperty("password"));

1.3请求转发(常用)

1
2
3
4
5
6
7
8
//1.获取Context对象
ServletContext sct = this.getServletContext();
//2.转发的请求路径
RequestDispatcher rd = sct.getRequestDispatcher("/指定路径文件");
//3.发出上面的指令,通过本类会直接跳到/指定路径资源页面下 '/' 代表当前工程!
rd.forward(req,resp);
//另外一种方式直接使用doGet/doPost里面的请求形参
req.getRequestDispatcher("/指定路径文件").forward(req,resp);

1.4获取初始化参数(了解)

获取类

1
2
3
4
5
6
7
//1.获取context对象
ServletContext sct = this.getServletContext();
//2.获取xml中配置的初始参数
String ipr = sct.getInitParameter("xml中的标识");
//3.输出数据到网页
PrintWriter out = resp.getWriter();
out.println(ipr);

xml参数

1
2
3
4
<context-param>
<param-name>一个标识</param-name>
<param-value>具体参数</param-value>
</context-param>

2.Response

简单的文件下载

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
//1.两种方式获得文件路径
String localPath = "文件绝对路径 taget下!";
//通过context获取的路径在tomcat包下,并非我们的web目录下!
//String localPath = this.getServletContext().getRealPath("/中国代码.jpg");
System.out.println("控制台:获取文本路径"+localPath);

//2.下载的文件名是啥 通过String方法截取
String filename = localPath.substring(localPath.lastIndexOf("\\")+1);

//3.让浏览器支持(Content-Disposition)我们要下载的东西,使用URLEncoder设置文件名称不乱码
resp.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(filename,"utf-8"));

//4.下载文件的输入流
FileInputStream fsm = new FileInputStream(localPath);

//5.获取输出流对象
ServletOutputStream serverOSM = resp.getOutputStream();

//6.缓冲区
int len = 0;
byte[] bytes = new byte[1024];
//把文件写入缓冲区,使用输出流把数据发送到客户端
while ((len = fsm.read(bytes)) != -1){
serverOSM.write(bytes,0,len);
}
//7.释放流[略]

3.Request

3.1前后端交互

后端 处理数据

1
2
3
4
5
6
7
8
9
10
11
//1.处理请求
String name = req.getParameter("name");
String pass = req.getParameter("password");
//处理多数据,如前端的checkbox 使用数组元素
String[] hobbies = req.getParameterValues("hobbies");

//2.反馈信息到前端
//方式1.重定向
//resp.sendRedirect("/rt/success.jsp");
//方式2.通过请求转发 /就代表当前项目无需再写具体路径
req.getRequestDispatcher("/success.jsp").forward(req,resp);

前端 登录页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<div style="text-align: center">
<%--这里提交的路径,需要寻找到项目的类路径--%>
<form action="${pageContext.request.contextPath}/login" method="post">
用户名:<input type="text" name="name"><br>
密码:<input type="password" name="password"><br>
爱好
<input type="checkbox" name="hobbies" value="Girls">Girls
<input type="checkbox" name="hobbies" value="Code">Code
<input type="checkbox" name="hobbies" value="Math">Math
<input type="checkbox" name="hobbies" value="Tinnies">Tinnies
<input type="submit">
<br>
</form>
</div>
<!--success.jsp页面可以随便写一句话-->

后端在xml中的配置 前端读取url-pattern

1
2
3
4
5
6
7
8
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>qingqiu.LoginCdx</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>

3.2重定向和转发

两者都可以实现跳转页面

重定向

  1. 就是通过各种方法将各种网络请求重新定个方向转到其它位置
  2. 由浏览器端进行的页面跳转
  3. 重定向的时候,url栏会发生变化 302
  4. resp.sendRedirect(“具体路径”);

转发

  1. 由服务器端进行的页面跳转。
  2. 请求转发:一种在服务器内部的资源跳转方式。
  3. 请求转发时候,url栏不产生变化 307
  4. req.getRequestDispatcher(“具体路径”).forward(req,resp)

JavaWeb基础
http://example.com/2022/07/12/JavaWeb/JavaWeb基础/
Author
John Doe
Posted on
July 12, 2022
Licensed under