jsp技术

 在jsp中既可以写java代码又可以写html代码  这种如果写多了就太乱了 不太好 要专一

jsp技术_第1张图片

 添加jsp依赖


        
            javax.servlet.jsp
            jsp-api
            2.2
            provided
        

 创建一个jsp页面 写一段html代码和java代码

<%--
  Created by IntelliJ IDEA.
  User: lsc1999
  Date: 2022/4/4
  Time: 20:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


hello jsp

<% System.out.println("hello jsp"); %>

 控制台和浏览器输出

 jsp技术_第2张图片

 jsp技术_第3张图片

 jsp技术_第4张图片

 jsp技术_第5张图片

jsp技术_第6张图片

把数据存在request域中 转发到jsp中 

在jsp中使用el表达式来取数据

@WebServlet(value = "/ServletDemo01")
public class ServletDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        ArrayList strings = new ArrayList<>();
        strings.add("路韶聪");
        strings.add("周冬雨");
        strings.add("黄四郎");
        //把数据放到request域中
        request.setAttribute("msg",strings);

        //转发到hello.jsp中
        request.getRequestDispatcher("/hello.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

 el表达式

${msg}

jsp技术_第7张图片

 jsp技术_第8张图片

 jsp技术_第9张图片



jsp技术_第10张图片

 jsp技术_第11张图片

 jsp技术_第12张图片


        
            jstl
            jstl
            1.2
        

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

jsp技术_第13张图片

标签

jsp技术_第14张图片

jsp技术_第15张图片

jsp技术_第16张图片

 

标签jsp技术_第17张图片

 

遍历对象并在网页上显示

创建对象

package com.cong.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 +
                '}';
    }
}

伪造数据 将来是在数据库中查的

       //1. 准备数据
        List brands = new ArrayList();
        brands.add(new Brand(1, "三只松鼠", "三只松鼠", 100, "三只松鼠,好吃不上火", 1));
        brands.add(new Brand(2, "优衣库", "优衣库", 200, "优衣库,服适人生", 0));
        brands.add(new Brand(3, "小米", "小米科技有限公司", 1000, "为发烧而生", 1));

创建servlet

@WebServlet(value = "/ServletBrand")
public class ServletBrand extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 准备数据
        List brands = new ArrayList();
        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("aaa", "测试");
        request.getRequestDispatcher("/jstl-forEach.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

创建jsp 使用foeEach标签循环遍历 使用if标签做判断

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>




    
    Title




序号 品牌名称 企业名称 排序 品牌介绍 状态 操作
${brand.id} ${brand.brandName} ${brand.companyName} ${brand.ordered} ${brand.description} 启用 禁用 修改 删除

页面回显数据

jsp技术_第18张图片

 

你可能感兴趣的:(java,javascript)