javaweb

这篇博客深入探讨了JavaWeb开发中的关键概念,包括Servlet的简介、映射、原理及其重要接口如ServletContext、HttpServletResponse、HttpServletRequest。详细讲解了Cookie和Session的使用,以及JSP的原理、基本语法和内置对象。同时,涵盖了JavaBean、MVC架构、Filter和监听器的应用,以及JDBC和事务管理的基础知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

javaweb

狂神超厉害!!!
狂神说Java

1、 Serlvet

1.1、 servlet 简介

  • servlet 就是sun公司开发动态web的一门技术
  • sun在这些API中提供了一个接口叫做: Servlet
  • 开发一个servlet程序 只需要:
    • 编写一个类 实现接口
    • 把开发好的java类 部署到web服务器

把实现servlet接口的java程序 叫做 servlet

1.2、 HelloServlet

servlet接口在sun公司有两个默认的实现类

  1. 构建一个maven,删掉里边的东西 src目录 在这里面 建立moudle 空的工程就是maven 的主工程

  2. 关于Maven 父子工程的理解:

    父项目:

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

子项目中

<parent>
<!-- G
     A
     V
-->
</parent>

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

3.maven 环境优化

  • 更新web.xml 设置
  • 将maven的结构搭建完整

4.编写一个Servlet程序

  1. 编写一个普通类

  2. 实现Servlet接口,这里直接HttpServlet

    public class HelloServlet extends HttpServlet {
    
        // Get() Post()只是请求实现的不同方式, 可以相互调用  业务逻辑一致
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // 响应流
            PrintWriter writer = response.getWriter();
            writer.println("Hello,Servlet");
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request,response);
        }
    }
    
    

5.编写servlet的映射

​ 为什么需要 映射: 我们写的是java程序,但是想要通过浏览器访问,而浏览器访问要连接到web服务器,所以我们需要在服务器中注册servlet,而且需要给浏览器一个能够访问的路径

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

6.配置tomcat

7.启动测试

1.3、 Servlet原理

1、 Servlet是由web服务器调用,web服务器在收到浏览器的请求之后,会:
在这里插入图片描述

1.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>/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-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello4</url-pattern>
      </servlet-mapping>  <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello5</url-pattern>
      </servlet-mapping>
    
  3. 一个servlet 可以指定通用路径

    默认路径 会干掉index.jsp(默认的主页)

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

    应用:自定义设置出错界面

    public class ErrorServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            // 一些初始的设置
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("Text/html");
            // 输出流
            PrintWriter writer = resp.getWriter();
            writer.println("<h1>404</h1>");
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    
    }
    
    <servlet>
        <servlet-name>error</servlet-name>
        <servlet-class>servlet.ErrorServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>error</servlet-name>
        <url-pattern>/*</url-pattern>
      </servlet-mapping>
    
  4. 一个servlet 可以指定一些前缀或者后缀等等……

    注:通配符前不可有当前项目的路径:

    /*.do

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

    优先级问题: 固有的映射路径优先级最高 所以 映射的路径为 默认路径时 并不会覆盖其他固有的映射路径

1.5、 ServletContext

web容器在启动的时候,它会为每一个we程序创建一个对应的Servlet对象,它代表了当前的web应用

在这里插入图片描述

1、 数据共享
// 获得数据
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       // 获取ServletContext对象 以存储数据
        ServletContext context = this.getServletContext();
        //使用setAttribute(String,Object)方法 以键值对形式存储
        context.setAttribute("userName",new String("秦疆"));
    }
}

// 拿到数据

public class GetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 首先拿到ServletContext对象 以拿数据
        ServletContext context = this.getServletContext();
        
        //设置 编码格式 防止 乱码
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        
        // 拿到数据 并且 输出到浏览器中
        resp.getWriter().println((String) context.getAttribute("userName"));
    }
}

<!-- 配置文件 -->
<servlet>
  <servlet-name>hello</servlet-name>
  <servlet-class>servlet.HelloServlet</servlet-class>
</servlet>
  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>


  <servlet>
    <servlet-name>get</servlet-name>
    <servlet-class>servlet.GetServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>get</servlet-name>
    <url-pattern>/get</url-pattern>
  </servlet-mapping>

访问时,先访问存放数据的地址 再访问拿数据的地址 才会有数据

2、获取初始化参数

在web.xml中设置初始化参数

<context-param>
  <param-name>url</param-name>
  <param-value>root</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");
        resp.getWriter().print(url);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
  <servlet>
    <servlet-name>sd3</servlet-name>
    <servlet-class>servlet.ServletDemo03</servlet-class>  
  </servlet>
  <servlet-mapping>
    <servlet-name>sd3</servlet-name>
    <url-pattern>/sd3</url-pattern>
  </servlet-mapping>
3、请求转发

重定向和请求转发

在这里插入图片描述

public class ServletDemo04 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        //参数为路径  mapping中的路径 / : 表示当前web项目
        context.getRequestDispatcher("/sd3").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
  <servlet>
    <servlet-name>sd4</servlet-name>
    <servlet-class>servlet.ServletDemo04</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>sd4</servlet-name>
    <url-pattern>/sd4</url-pattern>
  </servlet-mapping>

路径依旧是sd4,但现实内容是sd3

4、读取资源文件
  • java中新建properties

    会存在资源无法导出问题,解决方案–> 在pro.xml中写一个build

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>**/*.properties</exclude>
                    <exclude>**/*.xml</exclude>
                </excludes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
    
  • 在resources中新建properties

发现: 所有的资源文件都会被加载到classes 目录下 我们俗称这个为 classpath

public class ServletDemo05 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获得 输入流  参数为相对路径
        // 第一个 "/" : 代表当前web项目
        InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
        Properties prop = new Properties();
        prop.load(is);

        resp.getWriter().println(prop.getProperty("username"));
        resp.getWriter().println(prop.getProperty("password"));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
username = root
password = 123456
<!--ServletDemo05-->
  <servlet>
    <servlet-name>sd5</servlet-name>
    <servlet-class>servlet.ServletDemo05</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>sd5</servlet-name>
    <url-pattern>/sd5</url-pattern>
  </servlet-mapping>

1.6、 HttpServletResponse

web服务器接收到客户端的http请求,针对这个请求,会分别创建一个代表请求的HttpServletRequest对象,代表响应的一个HttpServletResponse对象

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);

相应的状态码

/*  响应状态码
    - 200 响应成功
    - 3xx:请求重定向:你重新到我给你的新位置去
    - 4xx: 找不到资源
    - 5xx: 服务器代码错误 500  网关错误: 502
*/

int SC_CONTINUE = 100;
int SC_SWITCHING_PROTOCOLS = 101;
int SC_OK = 200;
int SC_CREATED = 201;
int SC_ACCEPTED = 202;
int SC_NON_AUTHORITATIVE_INFORMATION = 203;
int SC_NO_CONTENT = 204;
int SC_RESET_CONTENT = 205;
int SC_PARTIAL_CONTENT = 206;
int SC_MULTIPLE_CHOICES = 300;
int SC_MOVED_PERMANENTLY = 301;
int SC_MOVED_TEMPORARILY = 302;
int SC_FOUND = 302;
int SC_SEE_OTHER = 303;
int SC_NOT_MODIFIED = 304;
int SC_USE_PROXY = 305;
int SC_TEMPORARY_REDIRECT = 307;
int SC_BAD_REQUEST = 400;
int SC_UNAUTHORIZED = 401;
int SC_PAYMENT_REQUIRED = 402;
int SC_FORBIDDEN = 403;
int SC_NOT_FOUND = 404;
int SC_METHOD_NOT_ALLOWED = 405;
int SC_NOT_ACCEPTABLE = 406;
int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
int SC_REQUEST_TIMEOUT = 408;
int SC_CONFLICT = 409;
int SC_GONE = 410;
int SC_LENGTH_REQUIRED = 411;
int SC_PRECONDITION_FAILED = 412;
int SC_REQUEST_ENTITY_TOO_LARGE = 413;
int SC_REQUEST_URI_TOO_LONG = 414;
int SC_UNSUPPORTED_MEDIA_TYPE = 415;
int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
int SC_EXPECTATION_FAILED = 417;
int SC_INTERNAL_SERVER_ERROR = 500;
int SC_NOT_IMPLEMENTED = 501;
int SC_BAD_GATEWAY = 502;
int SC_SERVICE_UNAVAILABLE = 503;
int SC_GATEWAY_TIMEOUT = 504;
int SC_HTTP_VERSION_NOT_SUPPORTED = 505;

2、下载文件
  1. 向浏览器输出消息

    getWriter().println();

  2. 下载文件

    1. 要获取下载文件的路径
    2. 下载的文件名
    3. 设置想办法让浏览器能够支持我们下载的东西
    4. 获取下载文件的输入流
    5. 创建缓冲区
    6. 获取OutputStream对象
    7. 将FileOutputStream流写入buffer缓冲区
    8. 使用OutputStream将缓冲区数据输出到客户端
public class DownServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 要获取下载文件的路径 (两种方式)
        /*
                  String realPath = this.getServletContext().getRealPath("/WEB-INF/classes/林俊杰.png");
     --->         D:\server\apache-tomcat-9.0.45\webapps\resp\WEB-INF\classes\林俊杰.png
                  String realPath = ”D:\Projects\Java\javaweb-servlet\response\src\main\resources\林俊杰.png“
     --->         D:\Projects\Java\javaweb-servlet\response\src\main\resources\林俊杰.png
                  错误的:
                  String realPath = this.getServletContext().getRealPath("/林俊杰.png");
     --->         D:\server\apache-tomcat-9.0.45\webapps\resp\林俊杰.png
        */
        String realPath = this.getServletContext().getRealPath("/WEB-INF/classes/林俊杰.png");
        System.out.println(realPath);
        // 2. 下载的文件名
        // 巧: 最后一个/ 后必定是文件名  直接截取  注意:  需要转义
        String fileName = realPath.substring(realPath.lastIndexOf("//")+1);
        // 3. 设置想办法让浏览器能够支持我们下载的东西
        // Content-Disposition 支持下载 这个信息头会告诉浏览器这个文件的名字和类型
        // 解决中文问题 URLEncoder.encode(fileName)
        resp.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName));
        // 4. 获取下载文件的输入流
        FileInputStream in = new FileInputStream(realPath);
        // 5. 创建缓冲区
        int len = 0;
        byte[] buffer = new byte[1024];
        // 6. 获取OutputStream对象
        ServletOutputStream os = resp.getOutputStream();
        // 7. 将FileOutputStream流写入buffer缓冲区  使用OutputStream将缓冲区数据输出到客户端
        while( (len = in.read(buffer)) != -1){
            os.write(buffer,0,len);
        }
        // 关闭资源
        os.close();
        in.close();
    }
}
3、 验证码功能

验证怎么来的?

  • 前端实现
  • 后端实现 Java的 图片类 image
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(100,25,BufferedImage.TYPE_INT_RGB);
        // 得到图片
        // 1. 拿到画笔
        Graphics2D graphics = (Graphics2D) image.getGraphics(); // 画笔
        // 设置图片背景颜色
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0,0,100,25);
        // 给图片写数据
        graphics.setColor(Color.BLACK);
        graphics.setFont(new Font(null,Font.BOLD,20));
        graphics.drawString(makeNum(),0,25);
        // 图片 已经准备就绪

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

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


    }
    // 生成随机数
    private String makeNum(){
        Random random = new Random();
        String num = random.nextInt(99999999)+"";
        // 保证数字一定是八位
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 8-num.length(); i++) {
            sb.append("0");
        }
        num = sb.toString()+num;
        return num;
    }

4、 实现重定向

一个web资源收到客户端的请求后,它会通知客户端去访问另外一个web资源,这个过程叫重定向

常见场景:

  • 用户登录
public class RedirectServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        /*
        resp.setHeader("Location","/resp/img");
        resp.setStatus(302);
        */
        // 重定向时 注意路径 否则404
        resp.sendRedirect("/resp/img");
    }

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

重定向和请求转发有什么区别

相同:

  • 都可以实现跳转页面

不同:

  • 请求转发url地址不会改变
  • 重定向时url地址会改变
  • 请求转发: 我给你去拿
  • 重定向: 别找我 找它
public class RequestTest extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        System.out.println(username);
        resp.sendRedirect("/resp/success.jsp");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

<%@ page contentType="text/html;charset=gb2312" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
<%--这里提交的路径 , 需要寻找到项目的路径--%>
<%--${pageContext.request.contextPath} 代表当前项目--%>
<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>

1.7、 HttpServletRequest

获取资源,请求转发

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<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="女孩">女孩
    <input type="checkbox" name="hobbies" value="篮球">篮球
    <input type="checkbox" name="hobbies" value="代码">代码
    <input type="checkbox" name="hobbies" value="唱歌">唱歌
    <input type="submit">
</form>
</body>
</html>

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[] hobbies = req.getParameterValues("hobbies");
        System.out.println(username);
        System.out.println(password);
        // 数组不会直接显示出来 需要处理一下
        System.out.println(Arrays.toString(hobbies));
        // 请求转发  通过request请求转发
        /* 注意 请求转发和重定向不同
           这里的 / 代表当前项目!!!
        */
        req.getRequestDispatcher("success.jsp").forward(req,resp);
    }

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
登陆成功
</body>
</html>

2、 Cookie(饼干)、Session(会话)

2.1、 会话

**会话:**用户打开一个浏览器,点击了很多超链接,访问多个web资源,关闭浏览器,这个过程可以称之为会话

**有状态会话:**一个同学来过教室,下次再来教室,我们会知道这个同学曾经来过,称之为有状态会话;

eg:

怎么证明你是西开学生

  1. 发票 西开(服务器)给学生(客户端)一张发票(cookie)
  2. 学籍 西开标记你来过了,有记录(session)

一个网站怎么证明你来过

  1. 服务端给客户端一个信件,客户端下次访问服务端带上信件就可以了;cookie
  2. 服务器登记你来过了,下次你来的时候我来匹配你;session

2.2、保存会话的两种技术

cookie

  • 客户端技术(响应,请求)

session

  • 服务端技术,利用这个技术,可以保存用户的会话信息。我们可以把信息或者数据放在session中

常见场景:网站登陆后,下次会自动登录

2.3、 Cookie

在这里插入图片描述

1、 从请求中拿到cookie信息

2、 服务器响应给客户端cookie

Cookie[] cookies = req.getCookies(); //获得cookie,cookie中是以键值对存储的
cookie.getName();                    //获得cookie中的key信息
cookie.getValue();                   //获得cookie中的value信息
new cookie(string,string);			 //新建一个cookie
cookie.setMaxAge(int);               //设置cookie的生命周期 单位秒
resp.addCookie(cookie); 			 //给客户端一个cookie

cookie一般保存在本地的用户目录下的appdata

一个网站cookie是否存在上限!

  • 一个cookie只能保存一个信息
  • 一个web站点可以给浏览器发送多个cookie,最多存放20个cookie
  • cookie的大小有限制—>4kb
  • 300个cookie浏览器上限

删除cookie

  • 不设置有效期,关闭浏览器即可删除cookie

  • 设置cookie有效期为0

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //解决中文乱码问题
            resp.setCharacterEncoding("utf-16");
            req.setCharacterEncoding("utf-16");
            PrintWriter out = resp.getWriter();
    
            // 从请求中得到cookie
            Cookie[] cookies = req.getCookies();
            if(cookies!=null){
                for (Cookie cookie : cookies) {
                    if(cookie.getName().equals("lastLoginTime")){
                        long lastLoginTime = Long.parseLong(cookie.getValue()); //解析为long型
                        Date date = new Date(lastLoginTime);
                        out.write("上次访问时间为:"+date.toLocaleString());
                    }
                }
            }else {
                resp.getWriter().println("这是第一次来访问");
            }
    
            Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis() + "");
            cookie.setMaxAge(24*60*60); // 设置 cookie的生命周期 一天~
            resp.addCookie(cookie);
    
        }
    
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
            Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis() + "");
            // 设置cookie的生命周期为0 即立刻被删除 将其key的值与想要删除的名字一致  就会替换cookie 然后被删除
            cookie.setMaxAge(0);
            resp.addCookie(cookie);
    }
    

中文乱码解决问题!!!最效率的

        /*
        另: 解决中文乱码问题  转码---> 解码
        String username = "秦疆";
        URLEncoder.encode(username,"utf-8"); // 存储的时候---> 转码
        URLDecoder.decode(username,"utf-8"); // 使用时--->解码
         */

2.4、 Session(重点)

在这里插入图片描述

什么是Session:

  • 服务器会给每一个用户(浏览器)创建一个Session对象;
  • 一个Session独占一个浏览器,只要浏览器没有关闭,这个Session就存在
  • 用户登录之后,整个网站它都可以访问!—>保存用户的信息;保存购物车的信息……

Session、Cookie的区别:

  • Cookie是把用户的数据写给浏览器,浏览器保存(可以保存多个)
  • Session把用户的数据写到用户独有的Session中服务器端保存(保存重要的资源,减少服务器资源的浪费)
  • Session对象由服务器创建

使用场景:

  • 保存一个登录用户的信息
  • 购物车信息
  • 在整个网站中经常会使用的数据,我们将它保存在session中

Session应用:

public class SessionDemo01 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 解决乱码问题
        req.setCharacterEncoding("utf-16");
        resp.setCharacterEncoding("utf-16");
        resp.setContentType("text/html");
        //得到session对象
        HttpSession session = req.getSession();
        String sessionId = session.getId();
        //存放数据
        session.setAttribute("name","秦疆");

        if(session.isNew()){
            resp.getWriter().write("这是新创建的session,ID:"+sessionId);
        }else{
            resp.getWriter().write("session已经存在,ID:"+sessionId);
        }
    }


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

public class SessionDemo02 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 解决乱码问题
        req.setCharacterEncoding("utf-16");
        resp.setCharacterEncoding("utf-16");
        resp.setContentType("text/html");
        //得到session对象
        HttpSession session = req.getSession();
        // 获得session 中的数据
        String name = (String) session.getAttribute("name");
        System.out.println(name);
    }


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

public class SessionDemo03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 手动注销session
        req.getSession().invalidate();
    }

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

自动注销session----->web.xml

<!--设置session 自动失效时间-->
<session-config>
    <!--以分钟为单位-->
    <session-timeout>15</session-timeout>
</session-config>

3、 JSP

3.1、 什么是JSP

java server pages:java服务端页面,也和Servlet一样,用户动态web技术

最大的特点:

  • 写JSP就像在写html
  • 区别:
    • HTML只给用户提供静态的数据
    • JSP页面中可以嵌套Java代码,为用户童工动态数据

3.2、 JSP原理

思路:JSP是怎么执行的!

  • 代码层面没有任何问题

    在这里插入图片描述

  • 服务器内部工作

    tomcat中有一个work目录

    IDEA中使用tomcat的话,会在IDEA的tomcat中产生一个work目录

    C:\Users\51526\AppData\Local\JetBrains\IntelliJIdea2021.1\tomcat\c495a84f-6846-4de0-bed8-e3a036379a1f\work\Catalina\localhost\cs\org\apache\jsp

    发现页面转变成了Java文件

    在这里插入图片描述

浏览器像服务器发送请求,不管访问什么资源,其实都是在访问servlet

JSP最终也会被转换为一个Java类

源码剖析

public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent,
                 org.apache.jasper.runtime.JspSourceImports {
                     ……
                 }

在这里插入图片描述

本质上还是继承了HttpServlet,所以JSP本质还是Servlet!!!

//初始化
public void _jspInit() {
}
//销毁
public void _jspDestroy() {
}
//jspService()
public void _jspService(HttpServletRequest request, HttpServletResponse response){}

jspService()做的事情:

  1. 判断请求

  2. 内置一些对象

    final javax.servlet.jsp.PageContext pageContext;        // 页面上下文
    javax.servlet.http.HttpSession session = null;          //session
    final javax.servlet.ServletContext application;         //applicationContext 实际上就是ServletContext 作用域高
    final javax.servlet.ServletConfig config;               //config 配置文件
    javax.servlet.jsp.JspWriter out = null;                 //out
    final java.lang.Object page = this;                     //当前页面
    HttpServletRequest request;                             //请求
    HttpServletResponse response;							//响应
    
  3. 输出页面前增加的代码

    response.setContentType("text/html"); //设置响应的页面类型
    //给以上对象赋值
    pageContext = _jspxFactory.getPageContext(this, request, response,
                                              null, true, 8192, true);
    _jspx_page_context = pageContext;
    application = pageContext.getServletContext();
    config = pageContext.getServletConfig();
    session = pageContext.getSession();
    out = pageContext.getOut();
    _jspx_out = out;
    
  4. 以上的对象在JSP中可以直接使用

每次重启tomcat时,IDEA中的work文件会被截断,等待启动完毕后work就出来了

没有访问的.JSP 就不会出现

在这里插入图片描述

在JSP页面中:

  • 只要是Java代码就会原封不动的输出

  • 如果是Html代码,就会被转换为:

    out.write("<html>\n");
    

    这样的格式输出到前端

3.3、 JSP基本语法及指令

jsp表达式
<%--jsp 表达式--%>
<%--
<%= 变量或者表达式%>
将其输出到客户端
--%>
<%= new Date()%>
jsp脚本片段
<%--脚本片段--%>
<%
    int sum = 0;
    for (int i = 0; i <= 100; i++) {
        sum += i;
    }
    out.println(sum);
%>
<p>这是一个脚本片段</p>
<%
    out.println(sum);
%>


<%--脚本再实现 实现原理:这些均写在_jspService()中 因此可以嵌套 玩儿~--%>
<%
    for (int i = 0; i < 5; i++) {
%>
<h1>Hello,World</h1>
<%
    }
%>

jsp声明

会被编译到jsp生成的Java类中!

其他的会被编译到_jspService()方法中

<%!
    static {
        System.out.println("Loading Servlet");
    }
    
    private int globalVar = 0;
    
    public void f(){
        System.out.println("进入自定义方法f()");
    }
%>

在这里插入图片描述

3.4、 JSP指令

<%--两种拼接页面的方式--%>
<%@include file=""%>

<jsp:include page=""></jsp:include>
<jsp:include page=""/>

建议使用第二种,第二种本质上页面是分开的!

3.5、 9大内置对象

  • pageContext 存东西
  • request 存东西
  • response
  • session 存东西
  • application 存东西 == servletContext
  • config == servletConfig
  • out == write
  • page 不用
  • exception 异常
    //从底层到高层:作用域:page--->request--->session--->application
	//java JVM 双亲委派
    // 存放数据
    pageContext.setAttribute("name1","长江一号");//保存的数据只在一个页面中有效
    request.setAttribute("name2","长江二号");//保存的数据只在一次请求中有效,请求转发会携带数据
    session.setAttribute("name3","长江三号");//保存的数据只在一次会话中有效,从打开浏览器到关闭浏览器
    application.setAttribute("name4","长江四号");//保存的数据只在服务器中有效,从打开服务器到关闭服务器
    pageContext.forward("pageContextDemo02.jsp");
//  application.getRequestDispatcher("/pageContextDemo02.jsp").forward(request,response);

request:客户端向服务器发送请求,产生的数据,用户看完了就没有用了,比如:新闻,用户看完没有用了

session:客户端向服务器发送请求,产生的数据,客户看完了还有用,比如:购物车;

application:客户端向服务器发送请求,产生的数据,一个用户看完了,另一个用户还要使用,比如:聊天记录

3.6、 JSP标签、JSTL标签、EL表达式

EL表达式:${}

  • 获取数据
  • 执行运算
  • 获取web开发的常用对象
  • 调用Java方法

使用JSTL标签之前导入库,有时候会出现错误,是因为服务器问题,需要手动将包导入tomcat中

<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl-api -->
<dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>jstl-api</artifactId>
    <version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>

jsp 标签

<html>
    <head>
        <title></title>
    </head>
    <body>

        <%--<jsp:include page=""></jsp:include>  拼接页面--%>
        <%--转发: <jsp:include page="index.jsp"/>--%>

        <jsp:include page="index.jsp"/>


    </body>
</html>

jstl表达式

jstl标签库的使用是为了弥补html标签的不足,他自定义许多标签,可以供我们使用,标签的功能和Java代码一样!

核心标签

在这里插入图片描述

使用方法:

  1. 导入库

  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

  3. 如果出错,可能是tomcat中未导入包(服务器问题)

c:if

<html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <%--
    value="${param.username}"
    <c:if test=判断条件 var=变量名>
        --%>
        <form action="jstl01.jsp" method="get">
            <input type="text" name="username" value="${param.username}"><br/>
            <input type="submit" value="登录"><br/>
        </form>

        <c:if test="${param.username=='admin'}" var="isAdmin">
            <c:out value="管理员欢迎您~"></c:out>
        </c:if>
        <c:out value="${isAdmin}"/>

    </body>
</html>

c:foreach

<html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <%
        ArrayList<String> users = new ArrayList<>();
        users.add("张三");
        users.add("李四");
        users.add("王五");
        users.add("田六");
        users.add("赵七");
        request.setAttribute("users",users);
        %>
        <%--
        var 变量名
        items ${}要遍历的对象
        begin 开始,默认0
        end   尾,默认最后
        step  步长
    	--%>
        <c:forEach var="user" items="${users}">
            <c:out value="${user}"/>
        </c:forEach>
    </body>
</html>

c:choose

<html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <c:set var="score" value="85"/>

        <c:choose>
            <c:when test="${score>90}">
                <c:out value="您的成绩优秀!"/>
            </c:when>
            <c:when test="${score>80}">
                <c:out value="您的成绩良好!"/>
            </c:when>
            <c:when test="${score>70}">
                <c:out value="您的成绩一般!"/>
            </c:when>
            <c:when test="${score>60}">
                <c:out value="您的成绩及格!"/>
            </c:when>
            <c:when test="${score<60}">
                <c:out value="您的成绩不及格!"/>
            </c:when>
        </c:choose>
    </body>
</html>

4、 JavaBean

实体类

JavaBean有特定的写法:

  • 必须要有一个无参构造
  • 属性必须私有化
  • 必须要有对应的get/set方法

一般来和数据库的字段做映射

在这里插入图片描述

package com.poppy.pojo;

public class People {
    private int id;
    private String name;
    private int age;
    private String address;

    public People() {
    }

    public People(int id, String name, int age, String address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

5、 MVC三层架构

什么是MVC:modei view controller 模型 视图 控制器

1、 早些年

在这里插入图片描述

用户直接访问控制层,控制层就可以直接操作到数据库

  • servlet—>CRUD(增删改查)—>数据库
  • 弊端:程序十分臃肿,不利于维护
  • servlet:处理请求、响应、视图、跳转、处理jdbc、处理业务代码、处理逻辑代码

架构思想:没有什么是加一层解决不了的,如果有,就再加一层!!!

2、 MVC三层架构

在这里插入图片描述

Model:

  • 业务处理:业务逻辑(services)
  • 数据持久层:CRUD(Dao)

View:

  • 展示数据
  • 提供链接发起servlet请求

Controller:

  • 接受用户请求:req:请求参数、session……
  • 交给业务层处理对应代码
  • 控制视图的跳转

例如:

登录—>接受用户的登录请求—>处理用户的请求(获取用户的参数,username,password)—>交给业务层处理登陆业务(判断用户名密码是否正确;事务)—>Dao层查询用户名和密码是否正确—>数据库

6、 Filter

filter:过滤器、用来过滤网站的数据;

  • 处理中文乱码问题
  • 登陆验证

在这里插入图片描述

Filter开发步骤

  1. 导包

  2. 编写filter

    导Filter包不要错误!!!servlet下的

    public class CharacterEncodingFilter implements Filter {
        // 初始化 服务器启动时就运行了
        public void init(FilterConfig filterConfig) throws ServletException {
            System.out.println("CharacterEncodingFilter初始化");
        }
    	/*
    	1. 过滤中的代码,在过滤任何请求都会执行
    	2. 必须要让过滤器继续通行
    		chain.doFilter(request,response);
    	*/
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=UTF-8");
            // 进入过滤器后 进入资源前
            System.out.println("CharacterEncodingFilter运行前");
            //让我们的请求继续走,如果不写,程序到这里就会被拦截停止
            chain.doFilter(request,response);
            System.out.println("CharacterEncodingFilter运行后");
    
        }
        // 销毁 服务器结束时运行
        public void destroy() {
            System.out.println("CharacterEncodingFilter销毁");
        }
    }
    
  3. 配置web.xml

    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>com.poppy.filter.CharacterEncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
    <!--只要是/servlet下的任何请求,都会经过这个过滤器-->
        <url-pattern>/servlet/*</url-pattern>
    </filter-mapping>
    

7、 监听器

实现一个监听器的接口即可;

demo:统计在线人数

public class OnlineCountListener implements HttpSessionListener {
    //session 创建的时候做什么
    /*
    1. 通过拿到session拿到servletContext
    2. 给servletContext中存入一个用户信息
    3. 计数器
     */
    public void sessionCreated(HttpSessionEvent se) {
        ServletContext context = se.getSession().getServletContext();
        Integer count = (Integer) context.getAttribute("onlineCount");
        //如果count为空,即没有人,则给其加一,并存值
        if(count==null){
            count = new Integer(1);
        }else{
            int i = count.intValue();
            count = new Integer(count + 1);
        }
        context.setAttribute("onlineCount",count);

    }

    public void sessionDestroyed(HttpSessionEvent se) {

    }
}

web.xml配置

<listener>
    <listener-class>com.poppy.listener.OnlineCountListener</listener-class>
</listener>

8、 过滤器、监听器常见应用

监听器

GUI编程中经常使用

过滤器

  1. 解决乱码问题

    第六条

  2. 用户登录之后才可以进入主页;用户没有登陆不能进入主页

    1. 用户登陆之后,向session中放入用户的数据
    2. 进入主页的时候要判断用户是否已经登陆,要求:过滤器中实现

    login.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <form action="/servlet/login" method="post">
        <input type="text" name="username"><br>
        <input type="submit">
    </form>
    </body>
    </html>
    
    

    LoginServlet

    public class LoginServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String username = req.getParameter("username");
    
            if(username.equals("admin")){
                req.getSession().setAttribute(Constant.USER_SESSION,req.getSession().getId());
                resp.sendRedirect("/sys/success.jsp");
            }else{
                resp.sendRedirect("/error.jsp");
            }
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    

    success.jsp

    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h1>登陆成功</h1>
    <a href="/servlet/logout">注销</a>
    </body>
    </html>
    
    

    LogouServlet

    public class LogoutServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            Object userSession = req.getSession().getAttribute(Constant.USER_SESSION);
            if(userSession!=null){
                req.getSession().removeAttribute(Constant.USER_SESSION);
                resp.sendRedirect("/login.jsp");
            }
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    

    error.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h1>登录失败</h1>
    <h3>账户错误/权限不够</h3>
    </body>
    </html>
    
    

    SysFilter

    public class SysFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            //ServletRequest    HttpServletRequest  强转拿到req,resp
            // 之后在session中拿所需要的属性,判断是否由用户登录
            HttpServletResponse resp = (HttpServletResponse) response;
            HttpServletRequest req = (HttpServletRequest) request;
            Object user = req.getSession().getAttribute(Constant.USER_SESSION);
            if(user == null){// 如果没有用户,则跳转到错误页面
                resp.sendRedirect("/error.jsp");
            }
            chain.doFilter(request, response);
        }
    
        @Override
        public void destroy() {
    
        }
    }
    
    
    

    web.xml

    <servlet>
            <servlet-name>LoginServlet</servlet-name>
            <servlet-class>com.poppy.servlet.LoginServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>LoginServlet</servlet-name>
            <url-pattern>/servlet/login</url-pattern>
        </servlet-mapping>
    
        <servlet>
            <servlet-name>LogoutServlet</servlet-name>
            <servlet-class>com.poppy.servlet.LogoutServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>LogoutServlet</servlet-name>
            <url-pattern>/servlet/logout</url-pattern>
        </servlet-mapping>
    
    
    
        <filter>
            <filter-name>SysFilter</filter-name>
            <filter-class>com.poppy.filter.SysFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>SysFilter</filter-name>
            <url-pattern>/sys/*</url-pattern>
        </filter-mapping>
    
    
    

9、 JDBC复习

数据库和程序员之间的一个中间件,程序员直接连接jdbc即可

在这里插入图片描述

10、 事务

ACID

偷懒了 没有记完~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值