zoukankan      html  css  js  c++  java
  • JavaWeb--JSP

    JSP


    什么是JSP?

    • Java Server Page: java服务器端页面,和Servlet一样,用于开发动态 web
    • 最大的特点,就是写JSP,就像是在写HTML
    • 区别:
      • HTML只能给用户提供静态数据
      • JSP页面中,可以嵌入Java代码,为用户提供动态数据

    JSP 原理

    思路: JSP到底如何执行的?

    • 代码层面: target里面和项目里面的JSP类似
    • 服务器层面:
      Tomcat服务器的work目录
          
      image.png

      IDEA工作空间
       
      image.png

      页面转变为了Java程序:

      image.png

      浏览器向服务器发送请求,不管访问什么资源,其实都是在访问Servlet!

      JSP本质上就是一个Servlet:
    // 初始化
    public void _jspInit() {
    }
    // 销毁
    public void _jspDestroy() {  
    }
    //JSPService
    public void _jspService(HttpServletRequest request, HttpServletResponse response) 
    
    1. 判断请求
    2. 内置一些对象
    final javax.servlet.jsp.PageContext pageContext;   // 页面上下文
    javax.servlet.http.HttpSession session = null;	   //session
    final javax.servlet.ServletContext application;	   //applocationContext
    final javax.servlet.ServletConfig config;          //config
    javax.servlet.jsp.JspWriter out = null;            // out
    final java.lang.Object page = this;                // page: 当前
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;
    HttpServletRequest request		// 请求
    HttpServletResponse response	// 响应
    
    1. 输出页面前设置的代码
    response.setContentType("text/html");		//设置响应的页面类型
    
    pageContext = _jspxFactory.getPageContext(this, request, response,null, true, 8192, true);
    _jspx_page_context = pageContext;
          
    application = pageContext.getServletContext();
    
    config = pageContext.getServletConfig();
    
    session = pageContext.getSession();
    
    out = pageContext.getOut();
    _jspx_out = out;
    
    out.write("
    ");
    out.write("<html>
    ");
    out.write("<head>
    ");
      ......   
    out.write("</html>
    ");
    // 在JSP中,只要是Java代码,就会原封不动的输出out.print(name);
    // 如果是HTML代码就会被转换为out.write("<html>
    "); 这样的格式输出到前端
    

    以上的这些对象我们可以在JSP的页面中直接使用。
    image.png

    JSP 基础语法

    任何语言都有自己的语法,Java中有,JSP作为java技术的一种应用,它拥有一些自己扩充的语法,Java所有语法都支持。
    JSP表达式

    
    <%--JSP 表达式
    作用: 用来将程序的输出,输出到客户端
    <%= 变量或者表达式%>
    --%>
    
    <%=new java.util.Date()%>
    

    JSP 脚本片段

    <%
        int sum = 0;
        for (int i = 0; i <= 100; i++) {
            sum += i;
        }
        out.println("<h2>Sum=" + sum + "</h2>");
    %>
    

    JSP 脚本片段的再实现

    <%
        int x = 10;
        out.println(x);
    %>
    <p>这是一个JSP文档</p>
    <%
        int y = 2;
        out.println(x);
        out.println(y);
    %>
    

    在JSP代码中嵌入HTML

    <%
        for (int i = 0; i < 5 ; i++) {
    %>
        <h1>Hello World! <%=i%> </h1>
    <%
        }
    %>
    

    JSP声明

    <%!  
        static {
            System.out.println("Loading Servlet!");
        }
        private int globalVar = 0;
        public void study(){
            System.out.println("进入了方法。。。");
        }
    %>
    

    JSP声明:会被编译到JSP生成的Java类中,其他的就会被生成到_jspService方法中

    EL表达式${}**

    <%for (int i = 0; i < 5 ; i++) {%>
        <h1>Hello World! ${i} </h1>
    <%}%>
    

    此外:JSP的注释不会在HTML的源代码中显示。

    JSP 指令

    <%@ page errorPage="/error/500.jsp" %>
        
    <%@ include file="common/header.jsp"%>
    <h1>我是body1</h1>
    <%@ include file="common/footer.jsp"%>
    
    <hr>
    <jsp:include page="/common/header.jsp"/>
    <h1>我是body2</h1>
    <jsp:include page="/common/footer.jsp"/>
    

    9 大内置对象

    • PageContext        存数据
    • Request               存数据
    • Response
    • Session                                    存数据
    • Application(ServletContext) 存数据
    • Config(ServletConfig)
    • out
    • page
    • Exception

    image.png

    <%--内置对象--%>
    <%
    	//保存的数据只在一个页面中有效
        pageContext.setAttribute("name1","shilin.z-1");     
    	//保存的数据只在一次请求中有效,请求转发携带数据
        request.setAttribute("name2","shilin.z-2");         
    	//保存的数据只在一次会话中有效,从打开浏览器到关闭浏览器
        session.setAttribute("name3","shilin.z-3");         
    	//保存的数据只要服务器中有效,从打开服务器到关闭服务器
        application.setAttribute("name4","shilin.z-4");     
    
    %>
    <%--通过pageContext 取出保存的值--%>
    <%
        //
        String name1 = (String) pageContext.findAttribute("name1");
        String name2 = (String) pageContext.findAttribute("name2");
        String name3 = (String) pageContext.findAttribute("name3");
        String name4 = (String) pageContext.findAttribute("name4");
    %>
    <%--使用EL表达式输出${}--%>
    <h1>取出的值为:</h1>
    <h3>${name1}</h3>
    <h3>${name2}</h3>
    <h3>${name3}</h3>
    <h3>${name4}</h3>
    

    request: 客户端向服务器发送请求,产生的数据,用户看完了就没用了,比如:新闻

    session:客户端向服务器发送请求,产生的数据,用户看完了一会还有用,比如:购物车

    application: 客户端向服务器发送请求,产生的数据,一个用户用完了,其它用户还能用,比如:聊天数据

    JSP 标签、JSTL标签、EL表达

      		<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl-api -->
            <!--  JSTL表达式 依赖 -->
            <dependency>
                <groupId>javax.servlet.jsp.jstl</groupId>
                <artifactId>jstl-api</artifactId>
                <version>1.2</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/taglibs/standard -->
            <!-- Standard标签库 依赖 -->
            <dependency>
                <groupId>taglibs</groupId>
                <artifactId>standard</artifactId>
                <version>1.1.2</version>
            </dependency>
    

    EL表达式: ${}

    • 获取数据
    • 执行运算
    • 获取web开发的常用对象


    JSP标签

    <jsp:include page="/common/header.jsp"/>
    <jsp:forward page="/jsptag02.jsp">
        <jsp:param name="name" value="shilin"/>
        <jsp:param name="age" value="20"/>
    </jsp:forward>
    

    JSTL 标签

    • JSTL标签库的使用就是为了弥补HTML标签的不足;它自定义许多标签,可以供我们使用,标签的功能和java代码一样。

    https://www.runoob.com/jsp/jsp-jstl.html

    • 核心标签:核心标签是最常用的 JSTL标签。引用核心标签库的语法如下:

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

    • JSTL标签使用步骤:
      • 引入对应的taglib
      • 使用其中的方法
      • 在Tomcat中也需要引入jstl的包, 否则会报错误:JSTL解析错误
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <h4>if测试</h4>
        <form action="coreif.jsp" method="get">
            <%--EL表达式获取表单中的数据 ${param.参数名}--%>
            名字:<input type="text" name="username" value="${param.username}">
            <input type="submit" value="登录">
        </form>
    
        <c:if test="${param.username == 'admin'}" var="isAdmin" >
            <c:out value="管理员欢迎您。。"/>
        </c:if>
        <c:out value="${isAdmin}"/>
        
        <c:set var="score" value="85"></c:set>
        <c:choose>
            <c:when test="${socre>90}">
                你的成绩为优秀
            </c:when>
            <c:when test="${socre>80}">
                你的成绩为一般
            </c:when>
            <c:when test="${socre>60}">
                你的成绩为及格
            </c:when>
            <c:when test="${socre<60}">
                你的成绩为不及格
            </c:when>
        </c:choose>
          
       <%
            ArrayList<String> people = new ArrayList<>();
            people.add(0,"张三");
            people.add(1,"李四");
            people.add(2,"王五");
            people.add(3,"赵六");
            people.add(4,"田七");
            request.setAttribute("list",people);
        %>
        <%--
            var: 每次遍历处理的变量
            items: 要遍历的对象
        --%>
        <c:forEach var="people" items="${list}">
            <c:out value="${people}"/><br>
        </c:forEach>
        <c:forEach var="people" items="${list}" begin="1" end="4" step="2">
            <c:out value="${people}"/><br>
        </c:forEach>         
    </body>
    </html>
    

    JavaBean

    • 实体类
    • JavaBean 有特定的写法
      • 必须要有一个无参构造
      • 属性必须私有化
      • 必须有对应的get/set方法
      • 一般用来和数据库字段做映射ORM。
      • ORM(对象关系映射)
        • 表 -- 类
        • 字段 -- 属性
          • 行记录 -- 对象
    id name age address
    1 张三 2 重庆
    2 李四 32 北京
    3 王五 45 上海
    class People{
    	private int id;
        private String name;
        private int age;
        private String address;
    }
    
    class A{
    	new People(1,"张三",2,"重庆");
        new People(2,"李四",32,"北京");
        new People(3,"王五",45,"上海");
    }
    

  • 相关阅读:
    poj1904 King's Quest
    ACM竞赛须掌握的知识 以及 2个版本的POJ推荐 @ NKOJ discuss 转载的
    poj1466
    C++23中设计模式的factory模式
    poj3667 hotel
    poj1505 Copying Books
    在linux系统中安装VSCode(Visual Studio Code)
    Spring_的jar详细说明
    java开发问题总结4Maven使用问题汇总
    线程同步之信号量(sem_init,sem_post,sem_wait)
  • 原文地址:https://www.cnblogs.com/sinlearn/p/13558659.html
Copyright © 2011-2022 走看看