el表达式和标签库的简单使用!!!

备工作1:tomcat10.1.13版本,可去官网下载,不会下载的童靴看过来:如何正确下载tomcat???_明天更新的博客-CSDN博客

 准备工作2:添加架包(需要三个架包)

jstl架包,下载地址:有道云笔记

el表达式和标签库的简单使用!!!_第1张图片

el表达式和标签库的简单使用!!!_第2张图片

 准备好这三个架包后,创建一个普通的JavaWeb项目。

不太了解创建步骤的看这里:idea如何建立web项目???_明天更新的博客-CSDN博客

 1.搭建servlet环境

详细步骤:servlet初体验之环境搭建!!!_明天更新的博客-CSDN博客

2.if标签库的使用

el表达式和标签库的简单使用!!!_第3张图片

Java代码(Servlet):

/*
 * Copyright (c) 2020, 2023,  All rights reserved.
 *
 */
package cn;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * 

Project: servlet-web-jstl - UserListServlet

*

Powered by scl On 2023-09-01 20:52:50

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ @WebServlet("/userListServlet") public class UserListServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); String age = req.getParameter("age"); req.setAttribute("age",age); req.getRequestDispatcher("user.jsp").forward(req,resp); } }

jsp代码:因为要使用到标签库,一定要加上这个:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    适用tomcat10以上的版本

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


    Title


${age}

  

年龄大于18岁

年龄小于18岁

年龄为18岁,恭喜你以成年!!

结果显示:el表达式和标签库的简单使用!!!_第4张图片

3.foreach标签库的使用

el表达式和标签库的简单使用!!!_第5张图片

Java代码(user类):

/*
 * Copyright (c) 2020, 2023,  All rights reserved.
 *
 */
package cn;

/**
 * 

Project: servlet-web-jstl - UserEntity

*

Powered by scl On 2023-09-02 10:47:58

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ public class UserEntity { /** * 用户名称 */ private String username; /** * 用户年龄 */ private Integer age; /** * 状态 0:冻结状态 1:为冻结 */ private Integer start; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getStart() { return start; } public void setStart(Integer start) { this.start = start; } public UserEntity(String username, Integer age, Integer start) { this.username = username; this.age = age; this.start = start; } public UserEntity() { } }

Java代码(servlet):

/*
 * Copyright (c) 2020, 2023,  All rights reserved.
 *
 */
package cn;

import com.sun.net.httpserver.HttpServer;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 

Project: servlet-web-jstl - UserListServletFor

*

Powered by scl On 2023-09-02 10:45:16

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ @WebServlet("/userListServletFor") public class UserListServletFor extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { List list=new ArrayList<>(); list.add(new UserEntity("小明",14,0)); list.add(new UserEntity("小红",22,1)); list.add(new UserEntity("小绿",44,1)); list.add(new UserEntity("小王",18,0)); req.setAttribute("userList",list); req.getRequestDispatcher("userList.jsp").forward(req,resp); } }

jsp页面:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2023/9/2
  Time: 10:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title



        
序号 名称 年龄 冻结 操作
${m.index+1} ${user.username} ${user.age} 冻结状态未冻结 修改    删除

结果显示:

el表达式和标签库的简单使用!!!_第6张图片

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