目录
【概述】
1、导入坐标
- <dependency>
- <groupId>javax.servlet.jspgroupId>
- <artifactId>jsp-apiartifactId>
- <version>2.2version>
- <scope>providedscope>
- dependency>
2、创建JSP文件

3、编写HTML标签和Java代码
- <body>
- <h1>hello jsph1>
- <%
- System.out.println("hello jsp");
- %>
- body>
【概述】
JSP脚本用于在 JSP页面内定义 Java代码
例:
- <%@ page import="com.itheima.pojo.Brand" %>
- <%@ page import="java.util.List" %>
- <%@ page import="java.util.ArrayList" %>
- <%@ 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));
- %>
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- 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>
- <%
- for (int i = 0; i < brands.size(); i++) {
- Brand brand = brands.get(i);
- %>
- tr>
- <tr align="center">
- <td><%=brand.getId()%>
- td>
- <td><%=brand.getBrandName()%>
- td>
- <td><%=brand.getCompanyName()%>
- td>
- <td><%=brand.getOrdered()%>
- td>
- <td><%=brand.getDescription()%>
- <%
- if (brand.getStatus() == 1) {
- //显示启用
- %>
- <td>
- <%="启用"%>
- td>
- <%
- } else {
- //显示禁用
- %>
- <td>
- <%="禁用"%>
- td>
- <%
- }
- %>
- <td><a href="#">修改a> <a href="#">删除a>td>
- tr>
- <%
- }
- %>
- table>
- body>
- html>
【功能】
获取数据
【语法】
${expression}
例:获取域中存储的key为brands的数据
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <%--页首插入这句--%>
- <%@page isELIgnored="false" %>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
- ${brands}
- body>
- html>
【注意】
el表达式获取数据,会依次从这4个域中寻找,直到找到为止
【概述】
JSP标准标签库(Jsp Standarded Tag Library) ,使用标签取代JSP页面上的Java代码
【种类】

1、导入坐标
-
- <dependency>
- <groupId>jstlgroupId>
- <artifactId>jstlartifactId>
- <version>1.2version>
- dependency>
- <dependency>
- <groupId>taglibsgroupId>
- <artifactId>standardartifactId>
- <version>1.1.2version>
- dependency>
2、在JSP页面上引入JSTL标签库
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3、使用
<c:if>
【属性】
test,用于定义条件表达式
【使用】
例:
- <%@ 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>Titletitle>
- head>
- <body>
- <%--c:if:替换java中的 if-else--%>
- <c:if test="${status==1}">
- 启用
- c:if>
- <c:if test="${status==0}">
- 禁用
- c:if>
- body>
- html>
- //Servlet调用文件
- @WebServlet(urlPatterns = "/demo1")
- public class ServletDemo1 extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //准备数据
- 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域中
- request.setAttribute("brands",brands);
- request.setAttribute("status",1);
- //转发到el-demo.jsp
- //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);
- }
- }
【属性】
【使用】
例:
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <%--页首插入这句--%>
- <%@page isELIgnored="false" %>
-
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- 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>
- <%--相当于foreach--%>
- <c:forEach items="${brands}" var="brand" varStatus="status">
- tr>
- <tr align="center">
- <td>${status.count}td>
- <td>${brand.brandName}td>
- <td>${brand.companyName}td>
- <td>${brand.ordered}td>
- <td>${brand.description}td>
- <c:if test="${brand.status==1}">
- <td>启用td>
- c:if>
- <c:if test="${brand.status==0}">
- <td>禁用td>
- c:if>
- <td><a href="#">修改a> <a href="#">删除a>td>
- tr>
- c:forEach>
- table>
- <hr>
- <%--相当于for循环--%>
- <c:forEach begin="0" end="10" step="1" var="i">
- <a href="#">${i}a>
- c:forEach>
- body>
- html>
- @WebServlet(urlPatterns = "/demo1")
- public class ServletDemo1 extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //准备数据
- 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域中
- request.setAttribute("brands",brands);
- //转发到el-demo.jsp
- request.getRequestDispatcher("/jstl-foreach.jsp").forward(request,response);
- }
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- this.doGet(request, response);
- }
- }