编程语言
首页 > 编程语言> > 狂神Java Web (七)JSP详解

狂神Java Web (七)JSP详解

作者:互联网

JSP

1. 什么是JSP

JSP:Java Server Pages,Java服务器端页面,和Servlet一样,应用于动态web技术。
特点:

2. JSP原理

思路:JSP是怎么执行的

  1. 判断请求
  2. 内置一些对象
    	final javax.servlet.jsp.PageContext pageContext;	// 页面上下文
    	javax.servlet.http.HttpSession session = null;		// session
    	final javax.servlet.ServletContext application;		// applicationContext
    	final javax.servlet.ServletConfig config;			// config
    	javax.servlet.jsp.JspWriter out = null;				// out
    	final java.lang.Object page = this;					// page: 当前页面
    	javax.servlet.http.HttpServletRequest request		// request,请求
    	javax.servlet.http.HttpServletResponse response		// response,响应
    
  3. 输出页面之前增加的代码:
    	  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;
    
  4. 以上的对象都可以在JSP页面中直接使用
    image

在JSP页面中,只要是Java代码,就会原封不动的输出;
如果是HTML代码,就会被转化为out.write("<html>\r\n") 这种格式输出到前端。

3. JSP基础语法

创建普通Maven项目后如何转换为web项目:
在IDEA中项目上右键,然后 Add Framework Support... ,选择web即可。

JSP不仅支持Java的所有语法,还有自己的一些扩充语法。
JSP表达式:

    <%-- JSP表达式
     作用:将程序的输出,输出到客户端
     Sat Jul 31 16:55:35 CST 2021
     格式:<%= 变量或表达式%>
     --%>
    <%= new java.util.Date()%>

**JSP脚本片段 **

    <%-- JSP脚本片段
    sum=4950
     格式: <% 可以有多行内容 %>
     --%>
    <%
        int sum = 0;
        for (int i = 0; i < 100; i++) {
            sum += i;
        }
        out.println("<h1>sum="+sum+"</h1>");
    %>

JSP脚本片段的再实现

<%--
    因为JSP页面最后都要编译到一个Java文件中,所以在脚本片段中的变量是共享的,也不能重复定义。
    10
    这是一个JSP文档
    12
    --%>
    <%
        int x = 10;
        out.println(x);
    %>
    <p>这是一个JSP文档</p>
    <%
        int y = 2;
        out.println(y + x);
    %>
	
	<%-- 在代码中嵌入HTML元素
    Hello World 0
    Hello World 1
    Hello World 2
    --%>
    <%
        for (int i = 0; i < 3; i++) {
    %>
    <h2>Hello World  <%=i%></h2>
    <%
        }
    %>

JSP声明

    <%-- JSP声明
     格式 <%! %>
     --%>
    <%!
        static {
            System.out.println("Loading Servlet...");
        }

        private int golbalVar = 0;

        public void kuang(){
            System.out.println("进入了方法Kuang.");
        }
    %>

JSP声明会被编译到JSP生成的Java类的类中,其他的会被生成到 _jspService 方法中。
在JSP,嵌入Java代码即可:

<%%>
<%=%>
<%!%>
<%--注释--%>

JSP的注释不会再客户端显示,HTML的会。

4. JSP指令

	<%@ page args... %>
	<%--可以设置当前页面对应的错误页面是什么--%>
	<%@page errorPage="error/500.jsp" %>  
	<%--显式声明是一个错误页面--%>
	<%@page isErrorPage="true" %>
	<!-- 也可以在web.xml中设置全局的各种错误对应的错误页面 -->
	<error-page>
        <error-code>500</error-code>
        <location>/error/500.jsp</location>
    </error-page>

修改了web.xml就必须重启Tomcat
设置img时如果读取不到资源,可以将img文件夹右键设置为resource root
如果img路径有问题,可以添加 src="${pageContext.request.contextPath}/img/img1.jpg"

    <%-- @include 将两个页面合二为一,提取公共页面
    如果此时jsp多个页面的代码块中有语法错误,会进入500
    --%>
    <%@include file="common/header.jsp" %>
    <h1>网页主体</h1>
    <%@include file="common/footer.jsp" %>

    <hr/>

    <%-- JSP标签
     jsp:include 通过代码方式引用,拼接页面,本质还是3个
     --%>
    <jsp:include page="/common/header.jsp" />
    <h1>网页主体</h1>
    <jsp:include page="/common/footer.jsp" />

5. JSP 9大内置对象

	// 保存的数据只在一个页面有效
	pageContext.setAttribute("name1", "狂神1号");
	// 作用域设置为sessiong
	// pageContext.setAttribute("name1", "狂神1号",PageContext.SESSION_SCOPE);
	// 保存的数据只在一次请求中有效,请求转发会携带这个数据
	request.setAttribute("name2", "狂神2号");
	// 保存的数据只在一次会话中有效,从打开浏览器到关闭浏览器
	session.setAttribute("name3", "狂神3号");
	// 保存的数据在服务器中有效,从打开服务器到关闭服务器
	application.setAttribute("name4", "狂神4号");
    <%-- 内置对象 --%>
    <%-- 存储属性 --%>
    <%
        // 保存的数据只在一个页面有效
        pageContext.setAttribute("name1", "狂神1号");
        // 作用域设置为sessiong
        // pageContext.setAttribute("name1", "狂神1号",PageContext.SESSION_SCOPE);
        // 保存的数据只在一次请求中有效,请求转发会携带这个数据
        request.setAttribute("name2", "狂神2号");
        // 保存的数据只在一次会话中有效,从打开浏览器到关闭浏览器
        session.setAttribute("name3", "狂神3号");
        // 保存的数据在服务器中有效,从打开服务器到关闭服务器
        application.setAttribute("name4", "狂神4号");
    %>
	
    <%-- 通过pageContext取出保存的值,通过寻找的方式来
        从底层到高层
     --%>
    <%
        // request.getAttribute("");
        String name1 = (String) pageContext.getAttribute("name1");
        String name2 = (String) pageContext.getAttribute("name2");
        String name3 = (String) pageContext.getAttribute("name3");
        String name4 = (String) pageContext.getAttribute("name4");
        String name5 = (String) pageContext.getAttribute("name5");  // 不存在

        // 直接转发过来,request中的数据也会被转发过来,可以取到2号
        pageContext.forward("pageContextDemo02.jsp");
    %>
	
    <%-- 使用EL表达式输出 ${} --%>
    <h1>取出的值为: </h1>
    <h3>${name1}</h3>
    <h3>${name2}</h3>
    <h3>${name3}</h3>
    <h3>${name4}</h3>
    <h3>${name5}</h3>
    <%-- 会显示null --%>
    <h3><%= name5%>
    </h3>
	
    <%--
    scope 设置作用域
        public void setAttribute(String name, Object attribute, int scope) {
            switch(scope) {
            case 1:
                this.mPage.put(name, attribute);
                break;
            case 2:
                this.mRequest.put(name, attribute);
                break;
            case 3:
                this.mSession.put(name, attribute);
                break;
            case 4:
                this.mApp.put(name, attribute);
                break;
            default:
                throw new IllegalArgumentException("Bad scope " + scope);
            }

        }
    --%>

6. JSP标签、JSTL标签、EL表达式

EL表达式:${}

JSP标签:<jsp:xxx>

JSTL:JSP标准标签库

为了弥补HTML标签的不足,自定义很多标签,标签功能和Java代码一样。

使用:引入对应的taglib,然后即可使用对应标签。 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

注意引入时要把对应的jstl和standard的jar包复制到tomcat的lib中,不然会报错。

7. JavaBean

实体类,JavaBean有特定的写法:

一般用来和数据库的字段做映射, ORM 对象关系映射;

标签:Web,Java,标签,request,JSP,狂神,pageContext,页面
来源: https://www.cnblogs.com/maple-w/p/15150363.html