今天(2021.9.24)开始学习一些有难度的知识了,如题
先安装和配置好Tomcat,具体下载和idea的配置可以参考
目录
开发步骤
Tomcat根据Servlet规范调用Servlet接口实现类规则
1.Tomacat有权创建Servlet接口实现类实例对象
2.Tomcat根据实例对象调用service
开发步骤:
1、创建一个java类继承与HttpServlet父类,使之成为一个Servlet接口实现类
2、重写HttpServlet父类的两个方法,doGet或doPost
3、将Servlet接口实现类信息【注册】到Tomcat服务器
网站——web——web-inf——web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--servlet接口实现类类路径地址交给Tomat-->
<servlet>
<servlet-name>oneServlet</servlet-name>
<servlet-class>com.bjpowernode.controller.OneServlet</servlet-class>
</servlet>
<!--为servlet接口实现类提供一个简短的别名-->
<servlet-mapping>
<servlet-name>oneServlet</servlet-name>
<url-pattern>/one</url-pattern>
</servlet-mapping>
</web-app>
在这个web.xml写
结果:
Servlet对象生命周期
1、网站中所有的Servlet接口实现类的实例对象,只能由Http服务器负责创建,开发人员不能手动创建Servlet接口实现类的实例对象
2、在默认情况,http服务器接收到对于当前Servlet接口实现类第一次请求时自动创建这个Servlet接口实现类的实例对象
在手动配置情况下,要求HTTP服务器启动时找到那个创建某个Servlet接口实现类
<load-on-startup>30</load-on-startup><!--填写一个大于0的整数即可-->
3、在Http服务器运行期间,一个Servlet接口实现类只能被创建一个实例对象
4、销毁时间:http服务器关闭,将网站中所有的Servlet对象进行销毁
创建一个Servlet:
package com.bjpowernode.controller;
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 OneServlet extends HttpServlet {
public OneServlet(){
System.out.println("oneServlet被创建实例对象");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("OneServlet doGet is run..");
}
}
同理,创建一个TwoServlet实例对象
看xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>OneServlet</servlet-name>
<servlet-class>com.bjpowernode.controller.OneServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>TwoServlet</servlet-name>
<servlet-class>com.bjpowernode.controller.TwoServlet</servlet-class>
<!--通知Tomcat在启动时负责TwoServlet实例对象-->
<load-on-startup>9</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>OneServlet</servlet-name>
<url-pattern>/one</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>TwoServlet</servlet-name>
<url-pattern>/two</url-pattern>
</servlet-mapping>
</web-app>
结果:
当在网页访问:
出现:
httpServletResponse接口
此接口来自于Servlet规范中,在Tomcat中存在setvlet-api.jar;
此接口实现类由Http服务器负责;
负责将doGet、doPost方法结果写入到响应体交给浏览器;
开发人员将此接口修饰的对象称为相应对象;
主要功能:
将执行结果以二进制写入到响应体中;
设置响应头中contant-type属性,从而控制浏览器使用,对应编译器将响应体二进制竖直编译为文字、图片、视频、命令;
设置响应头中location属性,将一个请求地址赋值给location,从而控制浏览器向指定服务器发送请求;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int i = 50;
PrintWriter out = response.getWriter();
out.write(i);//ascii码,返回2
out.print(i);//50
// String result = "java<br/>Mysql<br/>Html<br/>";
// out.println(result);
}
结果如注释
带有html标签的:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");//设置相应头content-type
PrintWriter out = response.getWriter();
String result = "java<br/>Mysql<br/>Html<br/>";
out.println(result);
}
结果:
中文字符:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");//设置相应头content-type
PrintWriter out = response.getWriter();
String result = "java<br/>好好学习<br/>天天向上<br/>";
out.println(result);
}
加上charset为utf-8即可显示中文字符
浏览器在接收到响应包后,如发现location属性,则自动通过地址栏向location指定网址发送请求行为【请求地址,请求方式,请求参数】
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String result = "http://www.baidu.com";
//通过响应对象,将地址赋值给响应头中的location属性
response.sendRedirect(result);//[响应头 location=“http://www.baidu.com”]
/*浏览器在接收到响应包后,如发现location属性,则自动通过地址栏向location指定网址发送请求行为【请求地址,请求方式,请求参数】*/
}
结果:直接跳转到baidu.com
HttpRequest接口
此接口实现类由Http服务器提供,HttpServletRequest接口修饰的对象称为【请求对象】,负责在doGet/doPost方法运行时读取Http请求协议包中的信息
作用:
可以Htt请求协议包中【请求行】信息
读取保存在Http请求协议包中【请求头】或【请求体】中请求参数信息
可以代替浏览器向Http服务器申请资源文件调用
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String url = request.getRequestURL().toString();
String method = request.getMethod();
String uri = request.getRequestURI();
System.out.println("URL:" + url);
System.out.println("method:" + method);
System.out.println("uri:" + uri);
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()){
String paramName = (String)paramNames.nextElement();//获取请求参数名
String value = request.getParameter(paramName);
System.out.println("请求参数名:"+ paramName + "请求参数值:" + value);
}
写一个html去点击访问,
<center>
<a href="/myweb/two?userName=mike&password=123456">点击跳转</a>;
</center>
结果:
请求对象和响应对象的生命周期
1、在Http服务器接收到浏览器发送的Http请求协议包之后,自动为当前的Http请求协议包生成一个请求对象和一个响应对象;
2、在Http服务器调用doGet/doPost方法是,负责将请求对象和响应对象作为实参传递到方法,确保doGet/doPost正确执行;
3、在Http服务器准备推送Http响应协议包之前,负责将本次请求关联的请求对象和响应对象销毁;
请求对象和响应对象 生命周期贯穿一次请求的处理过程中
多个Servlet之间调用
前提:某些浏览器发送请求,需服务器多个Servlet协同处理。但浏览器只能访问一个Servlet,导致用户需要手动通过浏览器发起多次请求才能得到服务,增加了用户获得服务难度
提高用户使用感受规则:无论本次请求涉及多少个Servlet,用户只需手动通知浏览器发起一次请求
多个Servlet之间调用规则:
(1)重定向
(2)请求转发
重定向解决方案
例如:
OneServlet:
package com;
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 OneServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("OneServlet负责做饭");
//重定向解决方案
response.sendRedirect("/myWeb/two");
}
}
TwoServlet:
package com;
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 TwoServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("TwoServlet负责吃饭");
}
}
连接Tomcat,访问浏览器,java结果:
浏览器Network结果:
也可以将其它网站资源文件地址发送给浏览器(http://ip地址:端口号/网站名/资源)
如将OneServlet改成:
package com;
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 OneServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("OneServlet负责做饭");
//重定向解决方案
response.sendRedirect("http://www.baidu.com");
}
}
结果就是执行完OneServlet就跳转到baidu
请求次数:浏览器至少要发送两次请求,只有第一次请求是用户手动发送,后续请求都是自动发送的;
请求方式:重定向解决方案中,通过地址栏通知浏览器发起下一次请求,因此通过重定向解决方案调用的资源文件接收的请求方式一定是Get;
缺点:需要在浏览器和服务器多次往返,时间消耗在往返次数上,增加用户等待服务时间;
请求转发方案
原理:用户第一次手动方式要求浏览器访问OneServlet,OneServlet工作完毕后,通过当前的请求对象代替浏览器向Tomcat发送请求,申请用TwoServlet。Tomcat在接收到这个请求后,自动调用TwoServlet来完成剩余任务
实现命令:请求对象代替浏览器向Tomcat发送请求
1、通过当前对象生成资源文件申请报告对象
RequestDispatcher report = request.getRequestDispatcher("/资源文件名") //一定要以/开头
2、将报告对象发送给Tomcat
report.forward(当前请求对象 , 当前响应对象)
例子:
OneServlet:
package com;
import javax.servlet.RequestDispatcher;
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 OneServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("One做饭");
//请求转发方案
// 1、通过当前对象生成资源文件申请报告对象
RequestDispatcher report = request.getRequestDispatcher("/two"); //一定要以/开头
// 2、将报告对象发送给Tomcat
report.forward(request,response);
}
}
TwoServelet:
package com;
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 TwoServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("吃饭了了了了了哦了了了了了了了了了了");
}
}
结果:
浏览器只发送了一次请求
请求地址:只能向Tomcat服务器申请调用档期那网站下资源文件地址;
请求方式:看浏览器第一次发送的请求方式,get和post都有可能,参与本次请求的搜友Servlet共享同一个请求协议包,因此,这些Servlet接收的请求方式于浏览器发送的请求方式保持一致
多个Servlet数据共享方案
数据共享:OneServlet工作完毕后,将产生数据交给TwoServlet使用;
Servlet规范中提供四种数据共享方案
1、ServletContext接口
2、Cookie类
3、HttpSession接口
4、HttpServletRequest接口
ServletContext接口
来自Servlet规范中的一个接口,在Tomcat中存在servlet-api.jar在Tomcat中负责提供这个接口实现类;
如果两个Servlet来自同一个网站,彼此之间通过网站的ServletContext实例对象实现数据共享;
开发人员习惯于将ServlerContext对象称为全局作用域对象
原理:每个网站都存在一个作用域对象,这个作用域对象相当于一个Map,在者网站中oneServlet可以将一个数据存入到全局作用域对象,当前网站中其它Servlet此时都可以从全局作用域对象中得到这个数据进行使用
全局作用域对象生命周期:全局作用域生命周期贯穿网站整个运行期间
1、在Http服务器启动过程中,自动为当前网站在内存中创建一个全局作用域对象;
2、在Http运行期间,一个网站中只有一个全局作用域对象;
3、在Http服务器运行期间,全局作用域对象处于存活状态;
4、在Http准备关闭时,负责将当前网站中全局作用域进行销毁;
例子
OneServlet:将数据放入全局作用域
package com;
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 OneServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//通过请求对象向Tomcat索要全局作用域对象
ServletContext application = request.getServletContext();
//将数据添加到全局作用域对象中,作为共享对象
application.setAttribute("key1",10086);
}
}
TwoServelt:读取全局作用域中的数据
package com;
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 TwoServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//通过请求对象向Tomcat索要全局作用域对象
ServletContext application = request.getServletContext();
//从全局作用域中取得指定关键字对应的值
Integer num = (Integer)application.getAttribute("key1");
System.out.println(num);
}
}
浏览器先访问one后访问two,结果在控制台输出:
Cookie
Cookie来自于Servlet规范中的一个工具类,存在于Tomcat提供servlet-api.jar中
如果两个Servlet来自于同一个网站,并且为同一个浏览器/用户提供服务,此时借助于Cookie对象进行数据共享
Cookie存放当前用户的私人数据,在共享数据过程中提高服务信息
原理:用户通过浏览器第一次向MyWeb网站发送请求申请OneServlet,OneServlet在运行期间创建一个Cookie存储当前用户相关数据,OneServlet工作完毕后,将Cookie写入到响应头交还给当前浏览器。
浏览器收到响应后,将Cookie存储在浏览器的缓存一段时间后,用户通过同一个浏览器再次向myWeb网站发送请求申请TwoServlet时,浏览器需要无条件的将myWeb网站之前推送过来的Cookie,写入到请求头发送过去。
实现方法
实例:注册会员卡—>刷卡吃饭
注册卡页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<center>
<form action="/myWeb/one">
<table border="2px">
<tr>
<td>用户名</td>
<td><input type="text" name="userName"></td>
</tr>
<tr>
<td>预存金额</td>
<td><input type="text" name="money"></td>
</tr>
<tr>
<td><input type="submit" value="提交信息"></td>
<td><input type="reset" value=重写></td>
</tr>
</table>
</form>
</center>
</body>
</html>
处理注册卡信息的OneServlet
package com;
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 OneServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//调用请求对象读取请求头中的信息
String userName = request.getParameter("userName");
String money = request.getParameter("money");
//开卡
Cookie card1 = new Cookie("userName",userName);
Cookie card2 = new Cookie("money",money);
//发卡,将Cookie写入到响应头交给浏览器
response.addCookie(card1);
response.addCookie(card2);
//通知Tomcat将点餐页面写入到响应体交给浏览器(请求转发)
request.getRequestDispatcher("/index_2.html").forward(request,response);
}
}
选择吃什么的刷卡吃饭界面index_2.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>点餐页面</title>
</head>
<body>
<center>
<font style="color:red;font-size: 40px">点餐页面</font>
<form action="/myWeb/two">
<input type="radio" name="food" value="面条"/>面条100元
<input type="radio" name="food" value="豆浆"/>豆浆2元<br>
<input type="submit" value="划卡点餐"/>
</form>
</center>
</body>
</html>
处理吃饭的TwoServlet
package com;
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;
import java.io.PrintWriter;
import java.io.Writer;
public class TwoServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int noodleMoney = 100;
int waterMoney = 2;
String userName;
double money;
response.setContentType("text/html;charset=utf-8");
Cookie newCard = null;
PrintWriter out = response.getWriter();
//读取请求头信息
String food = request.getParameter("food");
//读取请求中的Cookie
Cookie[] cookieArray = request.getCookies();
//刷卡消费
for(Cookie card:cookieArray){
String key = card.getName();
String value = card.getValue();
if("userName".equals(key)){
userName = value;
}else if("money".equals(key)){
money = Double.parseDouble(value);
if("面条".equals(food)){
if(money >= noodleMoney){
newCard = new Cookie("money",String.valueOf(money-noodleMoney));
out.println("吃面条");
}else {
out.println("没钱滚蛋");
}
}else if("豆浆".equals(food)){
if(money >= waterMoney){
newCard = new Cookie("money",String.valueOf(money-waterMoney));
out.println("喝豆浆");
}else {
out.println("没钱滚蛋");
}
}
}
}
//将用户卡返还给用户
if(newCard != null){
response.addCookie(newCard);
}
}
}
结果:
以上不是重点,重点是Cookie信息!
Cookie信息在浏览器保存了
Cookie的生命周期
在默认情况下,Cookie对象放在浏览器的缓存中,因此只要浏览器关闭v给i,Cookie对象就别销毁掉
在手动设置情况下,可以要求浏览器将接收的Cookie存放在客户端计算机上的硬盘上,同时需要指定Cookie在硬盘上存活时间。在存活时间范围内,关闭浏览器关闭客户端计算机,关闭服务器,都不会导致Cookie别销毁。在存活时间到达时,Cookie自动从硬盘上被删除
cookie.setMaxAge(60);//存活一分钟
cookie.setMaxAge(-1);//关闭浏览器失效
cookie.setMaxAge(0);//不记录cookie