JavaWeb03: JSP

1. JSP的定义

Java Server Pages: Java服务器端页面也和Servlet一样,用于动态Web技术。

最大的特点: 写JSP就像是写html

区别:html只给用户提供静态数据,但JSP页面中可以嵌入Java代码,为用户提供动态数据。

2. JSP原理

JSP怎么执行?

浏览器向服务器发送请求,不管访问什么资源,其实都是在访问serlet。而JSP最终也会被转换成一个Java类,因为他继承的HttpJspBase最终继承于HttpServlet。(观察index_jsp.java)

因此它本质上是一个servlet。

//初始化
public void _jspInit() {}

//销毁
public void _jspDestroy() {}

//JSPService
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response){}

1. 判断请求

2. 内置了一些对象

    final javax.servlet.jsp.PageContext pageContext;       //页面上下文
    final javax.servlet.ServletContext application;        //appilicationContext
    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;          //out
    HttpServletRequest request
    HttpServletResponse response

3. 输出页面前增加的代码

//设置响应的页面类型
response.setContentType("text/html; charset=UTF-8");     
pageContext = _jspxFactory.getPageContext(this, request, response, null, false, 8192, true);
jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
out = pageContext.getOut();
_jspx_out = out;

4. 以上的这些对象可以在JSP页面中直接使用。

注:在jsp页面中,只要是java代码就会原封不动输出,html会转换成类似out.write("<html>\r\n")格式,输出到前端。

3. JSP语法

支持所有Java语法,也有一些自己扩充的语法。

JSP表达式

<%--JSP表达式
用来把程序的输出,输出到客户端
--%>
<%--JSP表达式
<%= 变量或表达式>
--%>

<%= new java.util.Date()%>

JSP脚本片段

<%--JSP脚本片段--%>
<%
    int sum = 0;
    for (int i = 2;i<30;i++){
        sum+=i;
    }
    out.println("<h1>sum="+sum+"</h1>");
%>

JSP脚本片段的再实现

<%--JSP脚本片段再实现--%>
<%
    int x = 0;
    out.println(x);
%>
<%
    out.println(x);
%>

JSP中嵌入html元素

<%--JSP中嵌入html元素--%>
<%
    for (int i = 0;i<5;i++){
        sum+=i;
%>
<h1>hello</h1>
<%
    }//输出五个hello
%>

JSP声明

<% %>作用域在index_jsp.java文件下的public void _jspService里,在里面转译。而<%!  %>作用域更高一级,称为jsp声明。

注<%-- --%>为注释符号,jsp的注释不会在客户端显示,但是html的<!-- -->会在客户端显示。安全性稍高。

4. JSP指令

<%@ page errorPage="Error/500.jsp" %>
<%@ page import="java.util.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
//1.会将两个页面取出来合二为一,放在.java文件里。如果include的两个文件都有int i,会报500错误。
<%@include file="common/header.jsp"  %>
//或者
//2.拼接页面,本质上是两个页面
<jsp:include page="/common/header.jsp"></jsp:include>

5. 9个内置对象

  • PageContext 存东西
  • Request 存东西
  • Response
  • Session 存东西
  • Application (ServletContext) 存东西
  • config (ServletConfig)
  • out
  • page 不用了解
  • exception
<%@ page isELIgnored="false" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%--内置对象--%>
<%
    pageContext.setAttribute("name1","公孙离1");//保存的数据在一个页面中有效
    request.setAttribute("name2","公孙离2");//保存的数据在一次请求中有效,请求转发会携带这个数据
    session.setAttribute("name3","公孙离3");//保存的数据只在一次会话中有效,从打开浏览器到关闭浏览器
    application.setAttribute("name4","公孙离4");//保存的数据在服务器中有效,打开服务器到关闭服务器
%>

<%--脚本片段中的代码,会被原封不动生到.JSP.java
要求:这里代码必须保证java语法的正确性
--%>

<%
    //pageContext.getAttribute();或通过寻找的方式取出
    String name1 = (String) pageContext.findAttribute("name1");
    String name2 = (String) pageContext.findAttribute("name2");
    String name3 = (String) pageContext.findAttribute("name3");
    String name4 = (String) pageContext.findAttribute("name4");
    String name5 = (String) pageContext.findAttribute("name5");
    System.out.println(name1);
%>

<%--使用el表达式输出 ${} --%>
<h1>取出的值为:</h1>
<h3>${name1}</h3>
<h3>${name2}</h3>
<h3>${name3}</h3>
<h3>${name4}</h3>
<h3>${name5}</h3>

注:EL表达式不起作用的话,加入<%@page isELIgnored="false" %>

参考https://blog.csdn.net/qq_27825395/article/details/84841406

 pageContext.setAttribute("name","hello",pageContext.SESSION_SCOPE);
 //等价于
 session.setAttribute("name","hello");

注意事项:页面跳转的前后端的两种方式

应用场景:

  • request:客户端向服务器发送请求,产生的数据,用户看完没用了,比如:新闻,用户看完没用的。
  • session:客户端向服务器发送请求,产生的数据,用户用完一会儿还有用,比如:购物车;
  • application:客户端向服务器请求,产生的数据,一个用户用完了,其他用户还能使用。比如记录。

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

首先要在pom.xml里配置:

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

6.1 EL表达式作用:${ }

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

6.2 JSP标签

三种常用标签:

<jsp:include page="index.jsp"></jsp:include>

<%--跳转页面并携带信息--%>
<jsp:forward page="/jsptag2.jsp">
    <jsp:param name="name" value="公孙离"/>
    <jsp:param name="age" value="15"/>
</jsp:forward>

在另一个页面(jsp文件)中取信息:

<%--取出参数:也可以用EL表达式--%>
名字:<%=request.getParameter("name")%>
年龄:<%=request.getParameter("age")%>

6.3 JSTL(JSP标准标签库)

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

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

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

JSTL标签库使用步骤:

  1. 引入对应的taglib
  2. 使用其中的方法
  3. ps:在tomcat(lib文件夹下)可能也需要手动引入jstl包,否则会报错:jstl解析错误。

<c:if>

<h4>if测试</h4>
<hr>
<form action="coreif.jsp" method="get">
    <%--用EL表达式获取web开发常用的对象
    ${param.参数名}}
    --%>
    <input type="text" name="username" value="${param.username}">
    <input type="submit" value="登陆">
</form>

<%--判断如果提交的用户是管理员,则登陆成功--%>
<%--
    if(request.getParameter("username").equals("admin")){
        out.println("登陆成功");
--%>

<%--用jstl标签库来写--%>
<c:if test="${param.username=='admin'}" var="isAdmin">
    <c:out value="admin welcomes you!"></c:out>
</c:if>
<c:out value="${isAdmin}"></c:out>

<c:choose>

       

<c:foreach>  

<%
    ArrayList<String> people = new ArrayList<>();
    people.add(0,"张三");
    people.add(0,"李四");
    people.add(0,"王五");
    people.add(0,"赵六");
    request.setAttribute("list",people);
%>

<%--
var: 每一次遍历出来的变量
items: 要遍历的对象
begin: 哪里开始
end: 哪里结束
step: 步长
--%>
<c:forEach var="people" items="${list}">
    <c:out value="${people}"/>
</c:forEach>
<hr>
<c:forEach var="people" items="${list}" begin="1" end="3" step="2">
    <c:out value="${people}"/>
</c:forEach>

7.JavaBean 

是一个实体类

JavaBean有特定写法:

  • 必须要有一个无参构造
  • 属性必须私有化
  • 必须有对应的get/set方法

一般用来和数据库的字段做映射ORM

ORM:对象关系映射

  • 表--->类
  • 字段--->属性
  • 行记录--->对象

猜你喜欢

转载自blog.csdn.net/qq_43378019/article/details/110949530