1. 把实现了servlet接口的Java程序叫做servlet
- 编写一个类,实现servlet接口
- 把开发好的Java类部署到web服务器中
2. HelloServlet
- 构建一个普通的maven项目,删掉src目录,就可以建立更多的module;这个空工程就是maven主工程
- pom.xml中添加依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
可以在此路径下找到对应的jar包
- 将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">
</web-app>
- 创建并标注,将maven的结构搭建完整
- 编写一个普通类,实现servlet接口,这里直接继承HttpServlet
public class HelloServlet extends HttpServlet {
//由于get或者post只是请求实现的不同的方法,可以相互调用,业务逻辑都一样
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// ServletOutputStream sos = resp.getOutputStream();
PrintWriter writer = resp.getWriter();//响应流
writer.println("hello,Servlet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
- web.xml中编写servlet的映射
- 为什么需要映射:我们写的Java程序,但是要通过浏览访问,而浏览器需要连接web服务器,所以我们需要在web服务中注册我们写的Servlet,还需要给他一个浏览器能够访问的路径。
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.gy.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
- 配置tomcat local,并测试
- tips
- 以HTML输出
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();//响应流
writer.println("<h1>404</h1>");
3. Servlet原理
Servlet是由web服务器调用,web服务器在收到浏览器请求之后,会调用相关API对请求处理。
- 一个请求(Servlet)可以指定通用映射路径
<!--默认请求路径-->
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
- 指定一些后缀或者前缀等
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
- 优先级问题
指定了固有的映射路径优先级最高,如果找不到就会走默认的处理请求;
4. ServletContext
- web容器在启动的时候,它会为每个web程序都创建一个ServletContext对象,它代表了当前的web应用。
- 共享数据
- 存数据的Servlet
public class setData extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("hello");
ServletContext servletContext = this.getServletContext();
String username = "爱丽丝";//数据
servletContext.setAttribute("username",username);//将一个数据保存在ServletContext中 名为:username 值为 爱丽丝
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
- 读取数据的Servlet
public class GetData extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String username = (String) context.getAttribute("username");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
writer.write(username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
- 获取初始化参数
web.xml
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
public class ServletDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取上下文对象
ServletContext context = this.getServletContext();
//获取初始参数
String url = context.getInitParameter("url");
System.out.println("url: "+url);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
测试结果:页面输出url
- 请求转发,路径不会变
context.getRequestDispatcher("/转发的请求路径").forward(req,resp);
-
读取配置文件
clean掉target,然后启动项目可以发现resource下的配置文件.properties在target的路径/WEB-INF/classes/db.properties
public class ServletDemo05 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream stream = this.getServletContext().getResourceAsStream("/WEB-INF/classes/cn/bloghut/servlet/aa.properties");
// InputStream stream = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties pt = new Properties();
pt.load(stream);
String username = pt.getProperty("username");
String password = pt.getProperty("password");
resp.getWriter().println(username+":"+password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
maven资源导出失败问题
在pom.xml中添加下列代码
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
5. HttpServletResponse
web服务器接收到客户端的http请求,会针对这个请求,分别创建一个代表请求的HttpServletRequest对象,代表响应的一个HttpServletRespoonse;
- 如果要获取客户端请求过来的参数:找HttpServletRequest
- 如果要给客户端响应一些信息:找HttpServletResponse
下载文件
1.要获取文件的路径
2.下载的文件名是啥
3设置想办法让浏览器能够支持(Content-Disposition)下载我们需要的东西
4.获取下载文件的输入流
5.创建缓冲区
6.获取OutputStream对象
7.将FileOutputStream流写入到buff缓冲区
8.使用OutputStream将缓冲区中的数据输出到客户端!
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String realPath = "E:\\workspace_idea1\\javaweb-servlet\\response\\src\\main\\resources\\粒子群.jpg";
//注意这里+1的位置易出错
String fileName = realPath.substring(realPath.lastIndexOf("\\")+1);
resp.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"UTF-8"));
//获取下载文件的输入流
FileInputStream in = new FileInputStream(realPath);
int len = 0;
byte[] buffer = new byte[1024];
ServletOutputStream out = resp.getOutputStream();
while ((len=in.read(buffer))>0){
out.write(buffer,0,len);
}
in.close();
out.close();
}
验证码功能
- 前端实现
- 后端实现,需要用到Java图片类,生成一个图片
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//浏览器三秒刷新一次
resp.setHeader("refresh","3");
//在内存中创建一个图片
BufferedImage image = new BufferedImage(80, 20, BufferedImage.TYPE_INT_RGB);
//得到图片
Graphics2D g = (Graphics2D) image.getGraphics();//笔
//设置图片背景颜色
g.setColor(Color.white);
g.fillRect(0,0,80,20);
//给图片写数据
g.setColor(Color.blue);
g.setFont(new Font(null,Font.BOLD,20));
g.drawString(makeNum(),0,20);
//告诉浏览器,这个请求用图片打开
resp.setContentType("image/jpg");
//网站存在缓存,不让浏览器缓存
resp.setDateHeader("expires",-1);
resp.setHeader("Cache-Control","no-cache");
resp.setHeader("Pragma","no-cache");
//把图片写给浏览器
ImageIO.write(image,"jpg",resp.getOutputStream());
}
//生成随机数
private String makeNum(){
Random random = new Random();
String num = random.nextInt(9999)+"";
StringBuffer sb = new StringBuffer();
for (int i = 0;i <4-num.length();i++){
sb.append("0");
}
num = sb.toString() + num;
return num;
}
实现重定向
B一个web资源收到客户端A请求后,B他会通知客户端去访问另一个web资源,这个过叫重定向。
- 用户登录
public void sendRedirect(String location) throws IOException;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*
resp.setHeader("Location","/r/img");
resp.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);//302重定向,状态码
*/
resp.sendRedirect("/r/img");//此处为验证码Servlet的路径
}
重定向和转发
- 相同点
页面都会实现跳转 - 不同点
请求转发的时候,url不会产生变化 307
重定向的时候,url地址栏会发生变化 302
一个DEMO
index.jsp,其中action后路径与Servlet对应
<html>
<body>
<h2>Hello World!</h2>
<%--当前项目路径--%>
<%@ page contentType="text/html; charset=UTF-8" %>
<form 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>
RequestTest.java
@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);
}
6.HttpServletRequest
HttpServletRequest代表客户端的请求,用户通过Http协议访问服务器,Http请求中的所有信息会被封装到HttpServletRequest,通过这个HttpServletRequest中的方法,获取客户端的所有信息。
- 获取前端传递的参数
req.getParameter
- 请求转发
- 转发时"/“代表的是本应用程序的根目录 重定向时”/"代表的是webapps目录
- getRequestDispatcher分成两种,可以用request调用,也可以用getServletContext()调用 不同的是request.getRequestDispatcher(url)的url可以是相对路径也可以是绝对路径。而this.getServletContext().getRequestDispatcher(url)的url只能是绝对路径。
index.jsp
<%--
Created by IntelliJ IDEA.
User: Ray
Date: 2022-04-24
Time: 20:53
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
</head>
<body>
<h1>登录</h1>
<form action="${pageContext.request.contextPath}/login" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
爱好:
<input type="checkbox" name="hobbies" value="girl">女孩
<input type="checkbox" name="hobbies" value="movie">电影
<input type="checkbox" name="hobbies" value="games">游戏
<input type="submit">
</form>
</body>
</html>
LoginServlet.java
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
String[] hobbies = req.getParameterValues("hobbies");
System.out.println("=============");
System.out.println(username);
System.out.println(password);
System.out.println(Arrays.toString(hobbies));
System.out.println("=============");
//通过请求转发
req.getRequestDispatcher("/success.jsp").forward(req,resp);
标签:HttpServletRequest,简介,resp,req,HttpServletResponse,IOException,Servlet,void
From: https://www.cnblogs.com/gyViolet/p/16934106.html