1.Servlet简介
1.1概述
-
Servlet就是sun公司开发动态web的一门技术
-
sun在这些API中提供了一个接口叫做:Servlet,如果你想要开发一个Servlet程序,只需要两步:
- 编写一个类,实现Servlet接口
- 把开发好的java类部署到web服务器中
-
把实现了Servlet接口的java程序叫做Servlet
-
servlet接口在sun公司有两个实现类: HttpServlet,GenericServlet
1.2HelloServlet
-
构建一个普通的Maven项目,删掉里面的src目录,以后我们学习就在这个项目里面建立Moudel;这个空的工程就是Maven的主公程。
-
建立一个Moudel:servlet-01
关于maven父子工程的理解
-
父项目里会有:
<modules> <module>servlet-01</module> </modules>
-
子项目里会有:
<parent> <artifactId>javaweb-02-maven</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent>
-
-
maven环境优化
- 将子项目里的webapp.WEB-INF.web.xml换成最新版本
- 将maven结构搭建完整
-
编写一个Servlet程序(导入依赖的servlet的jar包)
-
编写一个普通类
-
实现Servlet接口,这里我们直接继承HttpServlet
package moli.Servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; public class helloServlet extends HttpServlet { //由于get和post只是请求实现的不同方式,可以互相调用,业务实现逻辑都一样 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //ServletOutputStream outputStream = resp.getOutputStream(); PrintWriter writer = resp.getWriter(); //响应流 writer.print("hello servlet"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } }
-
-
编写Servlet的映射
为什么需要映射:我们写的是JAVA程序,但是要通过浏览器访问,而浏览器需要连接web服务器,所以我们需要在web服务器中注册我们写的Servlet,还需要给他一个浏览器能够访问的路径。
<?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" metadata-complete="true"> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>hello</servlet-name> <servlet-class>moli.Servlet.helloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
-
配置tomcat
-
启动测试
1.3遇到的问题
idea中通过maven已经导入了包,idea中也能定位到包的位置,本地maven仓库也有对应的jar包存在,但是在本地编译的时候一直报程序包javax.servlet.xxx不存在。
解决方案:
- 还是jar包下载失败,或者没有自动下载缺少的jar包
- 推测原因是idea的版本与maven构建的build不相容
如果你的项目是多模块的,还需要勾选另一个选项
借鉴:https://blog.csdn.net/qq_43377237/article/details/108010414?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163506613416780269847839%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=163506613416780269847839&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v1~rank_v31_ecpm-5-108010414.pc_search_result_hbase_insert&utm_term=servlet%E7%BC%96%E8%AF%91%E6%97%B6%E4%B8%8D%E6%8A%A5%E9%94%99%EF%BC%8C%E8%BF%90%E8%A1%8C%E6%97%B6%E6%8A%A5%E9%94%99%E7%A8%8B%E5%BA%8F%E5%8C%85%E4%B8%8D%E5%AD%98%E5%9C%A8&spm=1018.2226.3001.4187
1.4Servlet原理
servlet由web服务器调用,在接收到浏览器的请求后会:
1.5Mapping问题
-
一个Servlet可以指定一个映射路径
<servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping>
-
一个Servlet可以指定多个映射路径
<servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello1</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello2</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello3</url-pattern> </servlet-mapping>
-
一个Servlet可以指定通用映射路径
<servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello/*</url-pattern> </servlet-mapping> //*代表任意输入什么都可以跳转到页面
-
默认请求路径(会把首页干掉)
<servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
-
指定一些后缀或者前缀等等……
<servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>*.moli</url-pattern> </servlet-mapping> //可以自定义后缀实现请求映射。注意:*前面不要加路径也就是/hello这种
2.ServletContext
因为每次做项目的时候都会有一些重复粘贴的内容,所以我们可以在idea里建一个文件note.md
2.1概述
this.getInitParameter() 初始化参数,可以在web.xml里配置
this.getServletConfig() servlet配置
this.getServletContext() servlet上下文,类似中间商
web容器在启动的时候,它会为每一个web程序创建一个对应的ServletContext对象,它代表了当前的web应用。
2.2共享数据
我在这个Servlet中保存的数据,可以在另外一个Servlet中拿到。
public class helloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String username = "小明";
context.setAttribute("username",username);//将一个数据保存在ServletContext中
}
}
public class getServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String username = (String) context.getAttribute("username");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().print("名字:"+username);
}
}
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>moli.servlet.helloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>getc</servlet-name>
<servlet-class>moli.servlet.getServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getc</servlet-name>
<url-pattern>/getc</url-pattern>
</servlet-mapping>
2.3获取初始化参数
public class servlet03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url");
resp.getWriter().print(url);
}
}
<!--配置web应用的初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
<servlet>
<servlet-name>gp</servlet-name>
<servlet-class>moli.servlet.servlet03</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>gp</servlet-name>
<url-pattern>/gp</url-pattern>
</servlet-mapping>
2.4请求转发
转发和重定向:
public class servlet04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
System.out.println("hhhh");
RequestDispatcher dispatcher = context.getRequestDispatcher("/gp");//转发的请求路径
dispatcher.forward(req,resp);//调用forword实现请求转发
}
}
2.5读取资源文件
Properties
-
在java目录下新建properties
-
在resource目录下新建properties
发现:都被打包到同一个路径下:classes,我们俗称这个路径为classpath
思路:需要一个文件流
username = root
password = 1234
public class propertiesServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(is);
String username = prop.getProperty("username");
String password = prop.getProperty("password");
resp.getWriter().print(username+":"+password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<servlet>
<servlet-name>ps</servlet-name>
<servlet-class>moli.servlet.propertiesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ps</servlet-name>
<url-pattern>/ps</url-pattern>
</servlet-mapping>
3.HttpServletResponse
web服务器接收到客户端的http请求,针对这个请求,分别创建一个代表请求的HttpServletRequest,代表响应的一个HttpServletResponse。
3.1简单分类
负责向浏览器发送数据的方法
ServletOutputStream getOutputStream() throws IOException;
PrintWriter getWriter() throws IOException;
负责向浏览器发送响应头的方法
void setCharacterEncoding(String var1);
void setContentLength(int var1);
void setContentLengthLong(long var1);
void setContentType(String var1);
void setDateHeader(String var1, long var2);
void addDateHeader(String var1, long var2);
void setHeader(String var1, String var2);
void addHeader(String var1, String var2);
void setIntHeader(String var1, int var2);
void addIntHeader(String var1, int var2);
状态码常量
int SC_OK = 200;
int SC_FOUND = 302;
int SC_SEE_OTHER = 303;
int SC_NOT_FOUND = 404;
int SC_BAD_GATEWAY = 502;
3.2常见应用
- 向浏览器输出消息
- 下载文件
- 验证码
3.2.1下载文件
public class FileServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1. 要获取下载文件的路径(在target里面,写死)
String realPath = "D:\\c\\javaweb-02-maven\\response\\target\\response\\WEB-INF\\classes\\1.jpg";
//2. 下载的文件名是啥
String filename = realPath.substring(realPath.lastIndexOf("//") + 1);
//3. 设置想办法让浏览器能够支持下载我们需要的东西(百度搜索:web下载文件的头信息)
//中文文件名可能乱码,因此用URLEncoder.encode()
resp.setHeader("Content-disposition","attachment;filename = "+ URLEncoder.encode(filename,"UTF-8"));
//4. 获取下载文件的的输入流
FileInputStream fis = new FileInputStream(realPath);
//5. 创建缓冲区
byte[] buffer = new byte[1024];
int len = 0;
//6. 获取OutputStream对象
ServletOutputStream outputStream = resp.getOutputStream();
//7. 将FileOutputStream流写入到buffer缓冲区,使用OutputStream将缓冲区的数据输出到客户端
while ((len = fis.read(buffer))>0){
outputStream.write(buffer,0,len);
}
//8. 关闭流
outputStream.close();
fis.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<servlet>
<servlet-name>fileDown</servlet-name>
<servlet-class>moli.Servlet.FileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>fileDown</servlet-name>
<url-pattern>/down</url-pattern>
</servlet-mapping>
3.2.2设置图片验证码
public class imageServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//让浏览器3秒自动刷新一次
resp.setHeader("refresh","3");
//在内存中创建一个图片
BufferedImage image = new BufferedImage(80, 80, BufferedImage.TYPE_3BYTE_BGR);
//得到图片,创建一个笔
Graphics2D g = (Graphics2D) image.getGraphics();
//设置图片的背景颜色
g.setColor(Color.white);
g.fillRect(0,0,80,80);
//给图片写数据
g.setColor(Color.blue);
g.setFont(new Font(null,Font.BOLD,20));
g.drawString(makeNum(),0,20);
//告诉浏览器这个请求用图片形式打开
resp.setContentType("image/jpeg");
//网站存在缓存,不让浏览器缓存
resp.setDateHeader("exprises",-1);
resp.setHeader("Cache-Control","no-Cache");
resp.setHeader("pragma","no-Cache");
//把图片写给浏览器
ImageIO.write(image,"jpeg",resp.getOutputStream());
}
//生成随机数
private String makeNum(){
Random random = new Random();
String num = random.nextInt(9999999)+"";
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 7 - num.length(); i++) {
sb.append(0);
}
num= sb.toString()+num;
return num;
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3.2.3重定向
3.2.3.1概述
一个web资源收到客户端请求后,他会通知客户端去访问另一个web资源,这个过程叫重定向。
常见场景:用户登录
重定向的时候要注意路径问题,否则就会404:页面找不到。
void sendRedirect(String var1) throws IOException;
public class redirectServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*
resp.setHeader("location","/r/image");
resp.setStatus(302);
*/
resp.sendRedirect("/r/image");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<servlet>
<servlet-name>redirect</servlet-name>
<servlet-class>moli.Servlet.redirectServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>redirect</servlet-name>
<url-pattern>/red</url-pattern>
</servlet-mapping>
3.2.3.2面试题
重定向和转发的区别:
相同点:页面都会跳转
不同点:
- 请求转发的时候,url不会产生变化 307
- 重定向的时候,url地址栏会发生变化 302
3.2.3.3练习
<html>
<body>
<h2>Hello World!</h2>
<%--这里提交的路径,需要寻找到项目路径--%>
<%--pageContext.request.contextPath代表当前项目--%>
<br action="${pageContext.request.contextPath}/login" method="get">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit">
</form>
</body>
</html>
public class requestTest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println(username+":"+password);
resp.sendRedirect("/r/success.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>success</h1>
</body>
</html>
4.HttpServletRequest
HttpServletRequest代表客户端的请求,用户通过Http协议访问服务器,Http请求中的所有信息会被封装到HttpServletRequest,通过这个HttpServletRequest的方法,获得客户端的所有信息。
获取前端的参数和请求转发:
public class loginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String username = req.getParameter("username");
String pwd = req.getParameter("pwd");
String[] hobbys = req.getParameterValues("hobbys");
System.out.println("=================");
System.out.println(username);
System.out.println(pwd);
System.out.println(Arrays.toString(hobbys));
System.out.println("=================");
//通过请求转发
req.getRequestDispatcher("/success.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
<body>
<div style="text-align: center">
<form action="${pageContext.request.contextPath}/login" method="post">
姓名:<input type="text" name="username"><br>
密码:<input type="password" name="pwd"><br>
爱好:<input type="checkbox" value="篮球" name="hobbys">篮球
<input type="checkbox" value="唱歌" name="hobbys">唱歌
<input type="checkbox" value="跳舞" name="hobbys">跳舞
<input type="checkbox" value="代码" name="hobbys">代码<br>
<input type="submit">
</form>
</div>
</body>
<body>
<h1>success</h1>
</body>
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class> moli.Servlet.loginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
5.cookie,seesion
5.1会话
会话:用户打开一个浏览器,点击很多超链接,访问多个web资源,关闭浏览器,这个过程可以称之为会话。
有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学曾经来过,这样的就称之为有状态会话。
你---------------学校
- 入学申请书:学校给的入学申请书
- 学校登记:学校标记你来过了
客户端----------服务端
- 服务端给客户端一个信件,下次客户端访问服务端的时候带上信件就可以了(cookie)
- 服务器登记你来过了,下次你来的时候服务器直接匹配你(seesion)
保存会话的两种技术:
cookie
- 客户端技术(响应,请求)
seesion
- 服务器技术,利用这技术可以保存用户的会话信息,我们可以把信息或者数据放在seesion中
常见场景:网站登录后,你下次就不用再登陆了,第二次访问就直接上去了。
5.2Cookie
5.2.1上限,删除,乱码
cookie:一般会保存在本地的用户目录下appdata。
一个网站cookie的上限:
- 一个cookie只能存一个信息
- 一个web站点可以给浏览器发送多个cookie,最多存放20个cookie
- cookie的大小有限制为4kb
- 300个cookie是浏览器的上限
删除cookie:
-
不设置有限期,关闭浏览器,cookie自动失效
-
设置有效期时间为0
//创建一个cookie,名字必须和要删除的cookie名字一致 Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+""); //设置cookie有效期为0 cookie.setMaxAge(0); resp.addCookie(cookie);
解决中文乱码:
5.2.2cookie获取上次登录时间
//保存用户上一次访问的时间
public class cookieDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//服务器告诉你,你来的时间,把这个时间封装成为一个信件,下次来的时候带上,服务器就知道了。
//解决中文乱码
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
//cookie服务器端从客户端获取
Cookie[] cookies = req.getCookies();//这里返回数组,说明cookie存在多个
//判断cookie是否存在
if (cookies!=null){
out.write("你上一次访问的时间是:");
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
//获取cookie的名字
if (cookie.getName().equals("lastLoginTime")){
//获取cookie中的值。把字符串解析为长整型的数字
long lastLoginTime = Long.parseLong(cookie.getValue());
Date date = new Date(lastLoginTime);
out.write(date.toString());
}
}
}else {
out.write("这是你第一次访问本站");
}
//服务器给客户端响应一个cookie
Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+"");
//设置cookie有效期为一天.有效期:浏览器关闭掉后再次打开,上次的cookie还是存在的
cookie.setMaxAge(24*60*60);
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
<servlet>
<servlet-name>cookieDemo1</servlet-name>
<servlet-class>moli.cookie.cookieDemo1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cookieDemo1</servlet-name>
<url-pattern>/c1</url-pattern>
</servlet-mapping>
5.3Session
5.3.1概述
- 服务器会给每个用户(浏览器),创建一个Session对象
- 一个Session独占一个浏览器,只要浏览器没有关闭,这个Session就存在
- 用户登录之后,整个网站他都可以访问—>保存登录用户的信息,保存购物车信息,整个网站经常会使用的数据会被保存在Session中
- Session的方法:
Session和Cookie的区别:
- Cookie是把用户的数据写给浏览器,浏览器保存(可以保存多个)
- Session把用户的数据写到用户独占的Session中,服务器端保存(保存重要的信息,减少服务器资源的浪费)
- Session对象由服务器创建
5.3.2使用Session
public class sessionDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决乱码
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//获取session
HttpSession session = req.getSession();
//给session中存东西
session.setAttribute("name",new Person("小明",22));
String id = session.getId();
//判断session是否是新创建的
if (session.isNew()){
resp.getWriter().write("session创建成功"+id);
}else {
resp.getWriter().write("session已经存在"+id);
}
/*
session在创建的时候做了什么事情
Cookie cookie = new Cookie("JSESSIONID", "id");
resp.addCookie(cookie);
*/
}
}
//创建一个Person类
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class sessionDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决乱码
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//获取session
HttpSession session = req.getSession();
Person person= (Person) session.getAttribute("name");
System.out.println(person.toString());
}
}
public class sessionDemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.removeAttribute("name");
//手动注销session
session.invalidate();
}
}
<servlet>
<servlet-name>sessionDemo1</servlet-name>
<servlet-class>moli.cookie.sessionDemo1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sessionDemo1</servlet-name>
<url-pattern>/s1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>sessionDemo2</servlet-name>
<servlet-class>moli.cookie.sessionDemo2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sessionDemo2</servlet-name>
<url-pattern>/s2</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>sessionDemo3</servlet-name>
<servlet-class>moli.cookie.sessionDemo3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sessionDemo3</servlet-name>
<url-pattern>/s3</url-pattern>
</servlet-mapping>
<!--设置session的默认失效时间-->
<session-config>
<!--1分钟后session自动失效-->
<session-timeout>1</session-timeout>
</session-config>