• Java---Java Web---JSP


    JSP

    • 概念:Java Server Pages,Java服务端页面

    • 一种动态的网页技术,其中既可以定义HTML、JS、CSS等静态内容,还可以定义Java代码的动态内容

    • JSP=HTML +Java

    • JSP的作用:简化开发,避免了在Servlet中直接输出HTML标签

    JSP快速入门

    在这里插入图片描述

    <%@ page import="com.itheima.pojo.Brand" %>
    <%@ page import="java.util.ArrayList" %>
    <%@ page import="java.util.List" %>
    
    <%@ page contentType="text/html;charset=UTF-8" language= "java" %>
    <%
        List<Brand> brands = new ArrayList<Brand>();
        brands.add(new Brand(1,"三只松鼠","三只松鼠",100,"三只松鼠,好吃不上火",1));
        brands.add(new Brand(2,"优衣库","优衣库",200,"优衣库,服适人生",0));
        brands.add(new Brand(3,"小米","小米科技有限公司",1000,"为发烧而生",1));
    %>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <input type="button" value="新增"><br>
    <hr>
    <table border="1" cellspacing="0" width="800">
        <tr>
            <th>序号</th>
            <th>品牌名称</th>
            <th>企业名称</th>
            <th>排序</th>
            <th>品牌介绍</th>
            <th>状态</th>
            <th>操作</th>
    
        </tr>
    
    
        <%
            for (int i = 0;i<brands.size();i++){
                Brand brand = brands.get(i);
        %>
    
        <tr align="center">
            <td><%=brand.getId()%></td>
            <td><%=brand.getBrandName()%></td>
            <td><%=brand.getCompanyName()%></td>
            <td><%=brand.getOrdered()%></td>
            <td><%=brand.getDescription()%></td>
            <td><%=brand.getStatus()==1?"启用":"禁用"%></td>
            <td><a href="#">修改</a> <a href="#">删除</a></td>
        </tr>
    
    
        <%
            }
        %>
    
    </table>
    
    </body>
    </html>
    
    
    package com.itheima.pojo;
    
    /**
     * 品牌实体类
     */
    
    public class Brand {
        // id 主键
        private Integer id;
        // 品牌名称
        private String brandName;
        // 企业名称
        private String companyName;
        // 排序字段
        private Integer ordered;
        // 描述信息
        private String description;
        // 状态:0:禁用  1:启用
        private Integer status;
    
    
        public Brand() {
        }
    
        public Brand(Integer id, String brandName, String companyName, String description) {
            this.id = id;
            this.brandName = brandName;
            this.companyName = companyName;
            this.description = description;
        }
    
        public Brand(Integer id, String brandName, String companyName, Integer ordered, String description, Integer status) {
            this.id = id;
            this.brandName = brandName;
            this.companyName = companyName;
            this.ordered = ordered;
            this.description = description;
            this.status = status;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getBrandName() {
            return brandName;
        }
    
        public void setBrandName(String brandName) {
            this.brandName = brandName;
        }
    
        public String getCompanyName() {
            return companyName;
        }
    
        public void setCompanyName(String companyName) {
            this.companyName = companyName;
        }
    
        public Integer getOrdered() {
            return ordered;
        }
    
        public void setOrdered(Integer ordered) {
            this.ordered = ordered;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    
        public Integer getStatus() {
            return status;
        }
    
        public void setStatus(Integer status) {
            this.status = status;
        }
    
        @Override
        public String toString() {
            return "Brand{" +
                    "id=" + id +
                    ", brandName='" + brandName + '\'' +
                    ", companyName='" + companyName + '\'' +
                    ", ordered=" + ordered +
                    ", description='" + description + '\'' +
                    ", status=" + status +
                    '}';
        }
    }
    
    

    JSP原理

    • JSP本质上就是一个Servlet

    • JSP 在被访问时,由JSP容器(Tomcat)将其转换为Java文件(Servlet),在由JSP容器(Tomcat)将其编译,最终对外提供服务的其实就是这个字节码文件

    在这里插入图片描述

    JSP脚本

    • JSP脚本用于在JSP页面内定义Java代码

    • JSP脚本分类:

      1.<%…%>:内容会直接放到jspService()方法之中

      2.<%=…%>:内容会放到out.print()中,作为out.print()的参数

      3.<%!..%>:内容会放到jspService()方法之外,被类直接包含

    • JSP缺点

      由于JSP页面内,既可以定义HTML 标签,又可以定义Java代码,造成了以下问题:

      1.书写麻烦:特别是复杂的页面

      2.阅读麻烦

      3.复杂度高:运行需要依赖于各种环境,JRE, JSP容器,JavaEE…

      4.占内存和磁盘:JSP会自动生成java和.class文件占磁盘,运行的是.class文件占内存

      5.调试困难:出错后,需要找到自动生成的java文件进行调试

      6.不利于团队协作:前端人员不会Java,后端人员不精HTML

      7.。。。

    EL表达式

    在这里插入图片描述

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@page isELIgnored="false" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    ${brands}
    </body>
    </html>
    
    @WebServlet("/demo1")
    public class ServletDemo1 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            List<Brand> brands = new ArrayList<Brand>();
            brands.add(new Brand(1, "三只松鼠", "三只松鼠", 100, "三只松鼠,好吃不上火", 1));
            brands.add(new Brand(2, "优衣库", "优衣库", 200, "优衣库,服适人生", 0));
            brands.add(new Brand(3, "小米", "小米科技有限公司", 1000, "为发烧而生", 1));
    
            request.setAttribute("brands", brands);
    
            request.getRequestDispatcher("/el-demo.jsp").forward(request, response);
    
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doGet(request, response);
        }
    }
    

    JSTL标签

    • JSP标准标签库(Jsp Standarded Tag Library),使用标签取代JSP页面上的Java代码

    在这里插入图片描述

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@page isELIgnored="false" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <c:if test="${status==1}">
        <h1>启用</h1>
    </c:if>
    <c:if test="${status==0}">
        <h1>禁用</h1>
    </c:if>
    </body>
    </html>
    
    @WebServlet("/demo1")
    public class ServletDemo1 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            List<Brand> brands = new ArrayList<Brand>();
            brands.add(new Brand(1, "三只松鼠", "三只松鼠", 100, "三只松鼠,好吃不上火", 1));
            brands.add(new Brand(2, "优衣库", "优衣库", 200, "优衣库,服适人生", 0));
            brands.add(new Brand(3, "小米", "小米科技有限公司", 1000, "为发烧而生", 1));
    
            request.setAttribute("brands", brands);
            request.setAttribute("status", 1);
    
    //        request.getRequestDispatcher("/el-demo.jsp").forward(request, response);
            request.getRequestDispatcher("/jstl-if.jsp").forward(request, response);
    
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doGet(request, response);
        }
    }
    

    在这里插入图片描述

    MVC模式和三层架构

    MVC是一种分层开发的模式,其中:

    • M: Model,业务模型,处理业务
    • V: View,视图,界面展示
    • C: Controller,控制器,处理请求,调用模型和视图

    在这里插入图片描述

    MVC好处:

    • 职责单一,互不影响
    • 有利于分工协作
    • 有利于组件重用

    在这里插入图片描述

    在这里插入图片描述
    如有不足,请多指教,
    未完待续,持续更新!
    大家一起进步!

  • 相关阅读:
    [羊城杯2020]easyphp .htaccess的利用
    Linux设置用户的密码有效期
    产品经理基础--04流程图与结构图
    myssql基于Spring Boot的宠物猫店管理系统的设计与实现毕业设计源码140909
    施耐德PLC之间无线Modbus通讯说明
    在MySQL客户端使用Tab健进行命令补全
    net二手手帐
    【每日一题Day47】LC1687从仓库到码头运输箱子
    HCIE-storage-lab 考试注意事项,实验考试细节及问题等
    云计算:掌控未来,一触即发!
  • 原文地址:https://blog.csdn.net/qq_40440961/article/details/127040963