• 【JAVA】JSP


    目录

    【JSP】—— Java Server Pages

    【JSP使用】

    【JSP原理】

    【JSP脚本】

    【分类】

    【EL表达式】—— Expression Language

    【JavaWeb中的四大域对象】

    【JSTL标签】

    【JSTL使用】

    【JSTL标签if】

    【JSTL标签forEach】


    【JSP】—— Java Server Pages

    【概述】

    • Java服务端页面
    • 一种动态的网页技术,其中既可以定义 HTML、JS、CSS等静态内容,还可以定义 Java代码的动态内容
    • JSP = HTML + Java
    • JSP的作用:简化开发,避免了在Servlet中直接输出HTML

    【JSP使用】

    1、导入坐标

    1. <dependency>
    2. <groupId>javax.servlet.jspgroupId>
    3. <artifactId>jsp-apiartifactId>
    4. <version>2.2version>
    5. <scope>providedscope>
    6. dependency>

    2、创建JSP文件

     3、编写HTML标签和Java代码

    1. <body>
    2. <h1>hello jsph1>
    3. <%
    4. System.out.println("hello jsp");
    5. %>
    6. body>

    【JSP原理】

    【概述】

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

    【JSP脚本】

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

    【分类】

    • <%...%>:内容会直接放到_jspService()方法之中
    • <%=…%>:内容会放到out.print()中,作为out.print()的参数
    • <%!…%>:内容会放到_jspService()方法之外,被类直接包含

    例:

    1. <%@ page import="com.itheima.pojo.Brand" %>
    2. <%@ page import="java.util.List" %>
    3. <%@ page import="java.util.ArrayList" %>
    4. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    5. <%
    6. //查询数据库
    7. List<Brand> brands = new ArrayList<Brand>();
    8. brands.add(new Brand(1, "三只松鼠", "三只松鼠", 100, "三只松鼠,好吃不上火", 1));
    9. brands.add(new Brand(2, "优衣库", "优衣库", 200, "优衣库,服适人生", 0));
    10. brands.add(new Brand(3, "小米", "小米科技有限公司", 1000, "为发烧而生", 1));
    11. %>
    12. html>
    13. <html lang="en">
    14. <head>
    15. <meta charset="UTF-8">
    16. <title>Titletitle>
    17. head>
    18. <body>
    19. <input type="button" value="新增"><br>
    20. <hr>
    21. <table border="1" cellspacing="0" width="800">
    22. <tr>
    23. <th>序号th>
    24. <th>品牌名称th>
    25. <th>企业名称th>
    26. <th>排序th>
    27. <th>品牌介绍th>
    28. <th>状态th>
    29. <th>操作th>
    30. <%
    31. for (int i = 0; i < brands.size(); i++) {
    32. Brand brand = brands.get(i);
    33. %>
    34. tr>
    35. <tr align="center">
    36. <td><%=brand.getId()%>
    37. td>
    38. <td><%=brand.getBrandName()%>
    39. td>
    40. <td><%=brand.getCompanyName()%>
    41. td>
    42. <td><%=brand.getOrdered()%>
    43. td>
    44. <td><%=brand.getDescription()%>
    45. <%
    46. if (brand.getStatus() == 1) {
    47. //显示启用
    48. %>
    49. <td>
    50. <%="启用"%>
    51. td>
    52. <%
    53. } else {
    54. //显示禁用
    55. %>
    56. <td>
    57. <%="禁用"%>
    58. td>
    59. <%
    60. }
    61. %>
    62. <td><a href="#">修改a> <a href="#">删除a>td>
    63. tr>
    64. <%
    65. }
    66. %>
    67. table>
    68. body>
    69. html>

    【EL表达式】—— Expression Language

    【功能】

    获取数据

    【语法】

    ${expression}

    例:获取域中存储的key为brands的数据

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%--页首插入这句--%>
    3. <%@page isELIgnored="false" %>
    4. <html>
    5. <head>
    6. <title>Titletitle>
    7. head>
    8. <body>
    9. ${brands}
    10. body>
    11. html>

    【JavaWeb中的四大域对象】

    1. page:当前页面有效
    2. request:当前请求有效
    3. session:当前会话有效
    4. application:当前应用有效

    【注意】

    el表达式获取数据,会依次从这4个域中寻找,直到找到为止

    【JSTL标签】

    【概述】

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

    【种类】

    【JSTL使用】

    1、导入坐标

    1. <dependency>
    2. <groupId>jstlgroupId>
    3. <artifactId>jstlartifactId>
    4. <version>1.2version>
    5. dependency>
    6. <dependency>
    7. <groupId>taglibsgroupId>
    8. <artifactId>standardartifactId>
    9. <version>1.1.2version>
    10. dependency>

    2、在JSP页面上引入JSTL标签库

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

    3、使用

    <c:if>

    【JSTL标签if

    相当于 if 判断

    【属性】

    test,用于定义条件表达式

    【使用】

    例:

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    3. <%--页首插入这句--%>
    4. <%@page isELIgnored="false" %>
    5. <html>
    6. <head>
    7. <title>Titletitle>
    8. head>
    9. <body>
    10. <%--c:if:替换java中的 if-else--%>
    11. <c:if test="${status==1}">
    12. 启用
    13. c:if>
    14. <c:if test="${status==0}">
    15. 禁用
    16. c:if>
    17. body>
    18. html>
    1. //Servlet调用文件
    2. @WebServlet(urlPatterns = "/demo1")
    3. public class ServletDemo1 extends HttpServlet {
    4. @Override
    5. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    6. //准备数据
    7. List brands = new ArrayList();
    8. brands.add(new Brand(1, "三只松鼠", "三只松鼠", 100, "三只松鼠,好吃不上火", 1));
    9. brands.add(new Brand(2, "优衣库", "优衣库", 200, "优衣库,服适人生", 0));
    10. brands.add(new Brand(3, "小米", "小米科技有限公司", 1000, "为发烧而生", 1));
    11. //存储到request域中
    12. request.setAttribute("brands",brands);
    13. request.setAttribute("status",1);
    14. //转发到el-demo.jsp
    15. //request.getRequestDispatcher("/el-demo.jsp").forward(request,response);
    16. request.getRequestDispatcher("/jstl-if.jsp").forward(request,response);
    17. }
    18. @Override
    19. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    20. this.doGet(request, response);
    21. }
    22. }

    【JSTL标签forEach

    相当于 for 循环

    【属性】

    • items:被遍历的容器
    • var:遍历产生的临时变量
    • varStatus:遍历状态对象
    • begin:开始数
    • end:结束数
    • step:步长

    【使用】

    例:

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    3. <%--页首插入这句--%>
    4. <%@page isELIgnored="false" %>
    5. html>
    6. <html lang="en">
    7. <head>
    8. <meta charset="UTF-8">
    9. <title>Titletitle>
    10. head>
    11. <body>
    12. <input type="button" value="新增"><br>
    13. <hr>
    14. <table border="1" cellspacing="0" width="800">
    15. <tr>
    16. <th>序号th>
    17. <th>品牌名称th>
    18. <th>企业名称th>
    19. <th>排序th>
    20. <th>品牌介绍th>
    21. <th>状态th>
    22. <th>操作th>
    23. <%--相当于foreach--%>
    24. <c:forEach items="${brands}" var="brand" varStatus="status">
    25. tr>
    26. <tr align="center">
    27. <td>${status.count}td>
    28. <td>${brand.brandName}td>
    29. <td>${brand.companyName}td>
    30. <td>${brand.ordered}td>
    31. <td>${brand.description}td>
    32. <c:if test="${brand.status==1}">
    33. <td>启用td>
    34. c:if>
    35. <c:if test="${brand.status==0}">
    36. <td>禁用td>
    37. c:if>
    38. <td><a href="#">修改a> <a href="#">删除a>td>
    39. tr>
    40. c:forEach>
    41. table>
    42. <hr>
    43. <%--相当于for循环--%>
    44. <c:forEach begin="0" end="10" step="1" var="i">
    45. <a href="#">${i}a>
    46. c:forEach>
    47. body>
    48. html>
    1. @WebServlet(urlPatterns = "/demo1")
    2. public class ServletDemo1 extends HttpServlet {
    3. @Override
    4. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    5. //准备数据
    6. List brands = new ArrayList();
    7. brands.add(new Brand(1, "三只松鼠", "三只松鼠", 100, "三只松鼠,好吃不上火", 1));
    8. brands.add(new Brand(2, "优衣库", "优衣库", 200, "优衣库,服适人生", 0));
    9. brands.add(new Brand(3, "小米", "小米科技有限公司", 1000, "为发烧而生", 1));
    10. //存储到request域中
    11. request.setAttribute("brands",brands);
    12. //转发到el-demo.jsp
    13. request.getRequestDispatcher("/jstl-foreach.jsp").forward(request,response);
    14. }
    15. @Override
    16. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    17. this.doGet(request, response);
    18. }
    19. }
  • 相关阅读:
    Single-cell 10x Cell Ranger analysis
    express创建服务器,以及前后端联调
    We’re sorry but XXX doesn’t work properly without JavaScript enabled(解决方案汇总)
    UE4 C++设计模式:装饰模式
    ChatGPT 插件 “Consensus“ 实现论文搜索功能;数据工程在语言建模中的重要性
    操作系统是干什么的?
    如何开发一款基于 Vite+Vue3 的在线表格系统(上)
    基金客户服务
    数控铣床的可分度回转工作台设计
    HONEYWELL 0574-A-012 0574-A-0131 工控DCS系统模块
  • 原文地址:https://blog.csdn.net/huihu__/article/details/126248094