狂神Java Web (五)Servlet详解

Servlet

1. Servlet简介

  • Sun公司用于开发动态web的一门技术。
  • Sun在API中提供一个接口:Servlet。如果想开发一个Servlet程序,只需要完成两个步骤:
    • 编写一个类实现Servlet接口
    • 将开发好的Java类部署到web服务器中

把实现了Servlet接口的Java程序叫做,Servlet

2. Hello Servlet

Maven父子项目:
构建一个普通的Maven项目,删除掉src目录,之后在其中建立Module即可,该空项目即Maven主工程,父项目
在父项目的pom.xml中会有:

    <modules>
        <module>servlet-01</module>
    </modules>

在子项目的pom.xml中会有:

	<parent>
		<artifactId>javaweb-02-servlet</artifactId>
		<groupId>com.maple</groupId>
		<version>1.0-SNAPSHOT</version>
	</parent>
  • 可能是IDEA版本的问题,有时候新建Module会没有设置成功父项目,设置成功时会显示子项目的pom文件为Ignored,变成灰色的
  • 不建议使用3.6.x版本,还是3.5.4比较稳定
  • 有时候IDEA会在一开始时出现parent标签,如果使用webapps模板的话会自动删掉,不知道原因,可以手动添加。

父项目中的jar子项目可以直接使用

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
    </dependencies>

Maven环境优化:

  1. 修改web.xml为最新的
  2. 将maven环境搭建完整

编写servlet程序:

  1. 编写一个普通类
  2. 实现servlet接口,直接继承 HttpServlet即可。

    在Servlet接口中,Sun公司有两个默认的实现类

package com.maple.servlet;

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

/**
 * Module中子项目的servlet学习
 *
 * @author maple_w
 * Created on 21/07/27 027 19:52
 */
public class HelloServlet extends HttpServlet {

    // get 或者 post只是请求实现的不同方式,可以互相调用,业务逻辑都一样。

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入doGet方法...");
        // ServletInputStream inputStream = req.getInputStream(); // 输入流
        // ServletOutputStream outputStream = resp.getOutputStream(); // 输出流
        PrintWriter out = resp.getWriter(); // 响应流
        out.print("Hello,Servlet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

  1. 编写Servlet的映射
    因为写的是Java程序,但需要通过浏览器访问,浏览器就需要连接web服务器,所以需要在web服务中注册写的Servlet,还要给它一个浏览器能够访问的路径。
    web.xml中注册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">
    
    	<!-- 注册servlet -->
    	<servlet>
    		<servlet-name>hello</servlet-name>
    		<servlet-class>com.maple.servlet.HelloServlet</servlet-class>
    	</servlet>
    	<!-- servlet的请求路径 -->
    	<servlet-mapping>
    		<servlet-name>hello</servlet-name>
    		<url-pattern>/hello</url-pattern>
    	</servlet-mapping>
    
    </web-app>
    
    1. 配置tomcat服务器即可。
      在配置tomcat时,经常因为编码问题出现乱码,一致统一为UTF-8比较好。设置了IDEA里面的编码后,如果控制台中一开始显示正常,但在servlet中sout打印的中文出现乱码,可以在设置Tomcat服务器时,在选项 VM options 中设置: -Dfile.encoding=UTF-8

3. Servlet原理

image

4. Mapping问题

1.一个Servlet指定一个映射路径

    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

2.一个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>

3.一个Servlet指定通用映射路径

    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello/*</url-pattern>
    </servlet-mapping>

如果直接指定 /*,会变成默认请求路径,覆盖掉index.jsp

4.指定一些后缀或前缀等

    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

自定义后缀实现请求映射,注意星号之前不能加映射的路径: /*.do 会报错

5.优先级问题
指定了固有的映射路径,优先级最高;如果找不到,就会走默认的请求处理。

    <!-- 注册servlet -->
    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>com.maple.servlet.HelloServlet</servlet-class>
    </servlet>
    <!-- servlet的请求路径 -->
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>error</servlet-name>
        <servlet-class>com.maple.servlet.ErrorServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>error</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

5. servlet相关方法

	// this.getInitParameter(); 初始化参数
	// this.getServletConfig(); Servlet配置
	// this.getServletContext(); Servlet上下文
	// 这些方法一般写在servlet的doGet方法中
  • getInitParameter() 可以获取初始化参数,该参数可以分为servlet中的初始化参数,和全局上下文参数,在web.xml中进行配置,很少使用。
    	<servlet>
    		<servlet-name>hello</servlet-name>
    		<servlet-class>com.maple.servlet.HelloServlet</servlet-class>
    		<init-param>
    			<param-name>...</param-name>
    			<param-value>...</param-value>
    		</init-param>
    	</servlet>
    
    	<context-param>
    		<param-name>...</param-name>
    		<param-value>...</param-value>
    	</context-param>
    
  • getServletConfig() 可以获取Servlet的配置
  • getServletContext() 获取Servlet的上下文

6. ServletContext对象

web容器在启动的时候,会为每个web程序都创建一个对应的ServletContext对象,代表了当前的web应用;
作用:

共享数据

在当前servlet中保存的数据,可以在另一个servlet中拿到

HelloServlet.java:

package com.maple.servlet;

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;

/**
 * @author maple_w
 * Created on 21/07/27 027 22:27
 */
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	
        ServletContext context = this.getServletContext();

        String username = "用户Maple";
        // 将一个数据保存在ServletContext中
        context.setAttribute("username", username);

        System.out.println("Hello");
    }
}

GetServlet.java:

package com.maple.servlet;

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;

/**
 * 从ServletContext中获取信息
 *
 * @author maple_w
 * Created on 21/07/27 027 22:45
 */
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");
        System.out.println("获取到的用户名为: " + username);

        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().println("用户:"+username);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

然后不要忘了在web.xml中添加对应的servlet。
进行测试,如果直接访问get对应的url,会显示获取到的为null,先访问hello再访问get则可以正常获取到用户名。

获取初始化参数

web.xml中写入初始化参数,然后在servlet中可以利用getServletContext()context.getInitParameter("url")获取对应的参数内容。

    <!-- 配置web应用初始化参数 -->
    <context-param>
        <param-name>url</param-name>
        <param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
    </context-param>
package com.maple.servlet;

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;

/**
 * @author maple_w
 * Created on 21/07/27 027 23:00
 */
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");
        resp.getWriter().println(url);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

请求转发

请求转发的url不会发生变化,但重定向的url会发生变化。

package com.maple.servlet;

import javax.servlet.RequestDispatcher;
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;

/**
 * @author maple_w
 * Created on 21/07/27 027 23:06
 */
public class ServletDemo04 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        // 转发到的请求路径
        RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");
        // 调用forward实现请求转发
        requestDispatcher.forward(req, resp);
        // context.getRequestDispatcher("/gp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

输入该servlet对应的地址即可访问到/gp对应的 servlet,即请求转发。

Properties 读取资源文件

在java目录下新建properties,在resources目录下新建properties,都被打包到了同一个路径下,classes。称该路径为classpath。通过文件流可以读取对应的资源文件。

package com.maple.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.InputStream;
import java.util.Properties;

/**
 * @author maple_w
 * Created on 21/07/27 027 23:27
 */
public class ServletDemo05 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        InputStream stream = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
        InputStream stream1 = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/maple/servlet/aa.properties");
        Properties properties = new Properties();
        properties.load(stream);
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");

        resp.getWriter().println("user: " + username + " ; password: " + password);

        Properties properties1 = new Properties();
        properties1.load(stream1);
        String username1 = properties1.getProperty("username");
        String password1 = properties1.getProperty("password");

        resp.getWriter().println("user: " + username1 + " ; password: " + password1);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

在resources目录下写db.properties,在java目录下写 aa.properties,如果想要java目录下的打包到对应的位置,需要手动在pom.xml中配置build、resources,防止资源导出失败:

  <!-- 在build中配置resources,防止资源导出失败 -->
  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
      </resource>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
      </resource>
    </resources>
  </build>

image

最后通过context的getResourceAsStream将对应位置的资源读取为流,通过Properties类读取流,并获取其中的内容。

7. HttpServletResponse

Web服务器接收到客户端的http请求,针对该请求,分别创建一个代表请求的HttpServletRequest对象、代表相应的HttpServletResponse对象;

  • 如果要获取客户端请求过来的参数:找 HttpServletRequest;
  • 如果要给客户端响应一些信息,找 HttpServletResponse

7.1 简单分类

负责向浏览器发送数据的方法

public ServletOutputStream getOutputStream() throws IOException;
public PrintWriter getWriter() throws IOException; // 一般写中文用

负责向浏览器发送响应头的方法

	public void setCharacterEncoding(String charset);
	
	public void setContentLength(int len);
	
	public void setContentLengthLong(long len);
	
	public void setContentType(String type);
	
	public void setDateHeader(String name, long date);

    public void addDateHeader(String name, long date);
    
    public void setHeader(String name, String value);
	
	public void addHeader(String name, String value);
	
	public void setIntHeader(String name, int value);
	
	public void addIntHeader(String name, int value);

7.2 常见应用

1.向浏览器输出消息

2.下载文件

  1. 获取下载文件的路径
  2. 下载的文件名
  3. 想办法让浏览器支持下载所需的东西
  4. 获取下载文件的输入流
  5. 创建缓冲区
  6. 获取OutputStream对象
  7. 将FileOutputStream流写入到buffer缓冲区
  8. 使用OutputStream将缓冲区数据输出到客户端
package com.maple.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.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * 下载文件
 * @author maple_w
 * Created on 21/07/28 028 8:50
 */
public class FileServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 获取下载文件的路径
        // String realPath = this.getServletContext().getRealPath("/logo.jpg");
        String realPath = "E:\\Code\\KuangStudyJavaWeb\\javaweb-02-servlet\\response-01\\target\\response-01\\WEB-INF" +
                "\\classes\\中文.jpg";
        System.out.println("realPath: " + realPath);
        // 2. 下载的文件名
        String fileName = realPath.substring(realPath.lastIndexOf("\\") + 1);
        System.out.println("fileName: " + fileName);
        // 3. 想办法让浏览器支持下载所需的东西 (Content-Disposition),中文文件名用URLEncoder.encode编码防止乱码
        // resp.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        // 4. 获取下载文件的输入流
        FileInputStream in = new FileInputStream(realPath);
        // 5. 创建缓冲区
        int len = 0;
        byte[] buffer = new byte[1024];
        // 6. 获取OutputStream对象
        ServletOutputStream out = resp.getOutputStream();
        // 7. 将FileOutputStream流写入到buffer缓冲区,使用OutputStream将缓冲区数据输出到客户端
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }

        in.close();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

3.验证码实现

验证码的实现:

  • 前端实现:js
  • 后端实现:java图片类,生成一个图片
package com.maple.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;

/**
 * 验证码图片
 *
 * @author maple_w
 * Created on 21/07/28 028 9:34
 */
public class ImageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 让浏览器3s自动刷新一次
        resp.setHeader("refresh", "3");

        // 在内存中创建图片
        BufferedImage bufferedImage = new BufferedImage(80, 20, BufferedImage.TYPE_INT_RGB);
        // 得到图片
        Graphics2D graphics = (Graphics2D) bufferedImage.getGraphics(); // 笔
        // 设置图片的背景颜色
        graphics.setColor(Color.white);
        graphics.fillRect(0, 0, 80, 20);
        // 给图片写数据
        graphics.setColor(Color.BLUE);
        graphics.setFont(new Font(null, Font.BOLD, 20));
        graphics.drawString(makeNum(), 0, 20);

        // 告诉浏览器,这个请求用图片方式打开
        resp.setContentType("image/jpeg");
        // 不让浏览器缓存
        resp.setDateHeader("expires", -1);
        resp.setHeader("Cache-Control", "no-cache");
        resp.setHeader("Pragma", "no-cache");

        // 把图片写给浏览器
        ImageIO.write(bufferedImage, "jpg", resp.getOutputStream());

    }

    /**
     * 生成随机数
     * @return
     */
    private String makeNum() {
        Random random = new Random();
        String num = random.nextInt(999999) + "";
        StringBuffer sb = new StringBuffer();
        // 补零,确保是6位数
        for (int i = 0; i < 6 - 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);
    }
}

4.重定向

一个web资源B收到客户端A请求后,通知客户端A访问另一个web资源C,该过程叫重定向。
常见场景:用户登录;

	public void sendRedirect(String location) throws IOException;

重定向:

public class RedirectServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 原理
        // resp.setHeader("Location", "/response_01_war/image");
        // resp.setStatus(HttpServletResponse.SC_FOUND);
        resp.sendRedirect("/response_01_war/image");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

重定向与转发的异同点:

  • 相同:
    • 页面都会跳转
  • 不同:
    • 请求转发的时候,URL不会产生变化 307
    • 重定向的时候,URL地址栏会发生变化 302

登录重定向的小demo:
创建处理登录重定向的Servlet:

public class RequestTestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入这个请求了");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println("username: " + username + " ,password: " + password);

        // 重定向的时候一定要注意路径问题,否则会404
        resp.sendRedirect("/response_01_war/success.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

在index.jsp中添加登录提交:

<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8" %>
<html>
<body>
<h2>Hello World!</h2>

    <%-- 这里提交的路径,需要寻找到项目的路径 --%>
    <%-- ${pageContext.request.contextPath}代表当前项目 --%>
    <form action="${pageContext.request.contextPath}/request" method="get">
        用户名: <input type="text" name="username" /> <br/>
        密  码: <input type="password" name="password" /> <br/>
        <input type="submit">
    </form>

</body>
</html>

在jsp中指定编码格式可以防止中文乱码。

8. HttpServletRequest

HttpServletRequest代表客户端的请求,用户通过HTTP协议访问服务器,HTTP请求中的所有信息会被封装到HttpServletRequest中,通过其方法,可以获取请求的信息。

  • getContextPath() 获取当前路径
  • getHeader() 获取头信息
  • getHttpServletMapping() 获取请求的路径
  • getRequestURL() 获取请求的URL

获取前端传递的参数及请求转发:

	request.getParameter(String s)
	request.getParameterValues(String s)

接受登录信息的servlet文件:

package com.maple.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.util.Arrays;

/**
 * @author maple_w
 * Created on 21/07/28 028 10:44
 */
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 解决后台接受中文乱码问题
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String[] hobbys = req.getParameterValues("hobbys");

        System.out.println(username);
        System.out.println(password);
        System.out.println(Arrays.toString(hobbys));

        // 重定向
        // resp.sendRedirect("/resuest-01/success.jsp");
        // 通过请求转发, 此处的 / 就是当前web应用,不需要在多写了。
        req.getRequestDispatcher("/success.jsp").forward(req, resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

登录的jsp页面:

<%--
  User: Yuhang
  Date: 21/07/28 028
  Time: 10:43
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Login</title>
</head>
<body>
    <h1>登录</h1>
    <%-- 以post方式提交表单,提交到login请求 --%>
    <div style="text-align: center">
        <form action="${pageContext.request.contextPath}/login" method="post">
            用户名:<input type="text" name="username"> <br/>
            密码:<input type="password" name="password"> <br/>
            爱好:
            <input type="checkbox" name="hobbys" value="女孩">女孩
            <input type="checkbox" name="hobbys" value="代码">代码
            <input type="checkbox" name="hobbys" value="sing">唱歌
            <input type="checkbox" name="hobbys" value="movie">电影

            <br/>
            <input type="submit">
        </form>
    </div>
</body>
</html>

posted @ 2021-07-27 23:42  Maple_w  阅读(369)  评论(0编辑  收藏  举报