JSP

简介: JSP

JSP

JSP——Java Server Page:Java服务端页面,在html页面中编写Java代码的页面。

绝大多数时候,我们希望响应的不是简简单单一句话,而是一个页面,我们用PrintWriter对象去写一个页面也是可以的,但缺点太明显。

所以直接返回一个页面,并且能够写Java代码就能大大简化我们的开发,这就是——JSP。

当前不提倡在jsp页面中写java代码,代替使用EL表达式。

JSP是简化Servlet编写的一种技术,它将Java代码和HTML语句混合在同一个文件中编写,只对网页中的要动态产生的内容采用Java代码来编写,而对固定不变的静态内容采用普通静态HTML页面的方式编写。


建立对JSP的直观认识

在JSP页面中编写的Java代码需要嵌套在<%和%>中,嵌套在<%和%>之间的Java代码被称之为脚本片段(Scriptlets),没有嵌套在<%和%>之间的内容被称之为JSP的模版元素

WEB容器(Servlet引擎)接收到以.jsp为扩展名的URL的访问请求时,它将把该访问请求交给JSP引擎去处理。

每个JSP 页面在第一次被访问时,JSP引擎将它翻译成一个Servlet源程序,接着再把这个Servlet源程序编译成Servlet的class类文件,然后再由WEB容器(Servlet引擎)像调用普通Servlet程序一样的方式来装载和解释执行这个由JSP页面翻译成的Servlet程序——JSP本质上就是一个Servlet。

idea中jsp被编译出的servlet被放在哪里?

C:\Users\Administrator.IntelliJIdea2016.1\system\tomcat\Tomcat_8_0_32_web1\work\Catalina\localhost\web\org\apache\jsp

新建一个jsp页面hello.jsp

<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <%
        Date date = new Date();
        System.out.println(date);
    %>
</body>
</html>

运行可以看到控制台上有时间输出。


JSP中9个隐式对象

隐式对象(或隐含变量):在JSP当中我们没有手动声明创建,但实际存在,可以直接使用的对象。

final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
// 还有request和response以及exception对象,一共9个

request:客户端的请求信息被封装在request对象中,通过它才能了解用户的需求,然后做出响应

②response:包含了响应客户请求的有关信息,但在JSP中使用很少

③pageContext:页面的上下文,是PageContext的一个对象,可以从该对象中获取到其他8个隐含对象,也可以获取到当前页面的其他信息

session:指的是客户端与服务器的一次会话,从客户端连到服务器的一个WebApplication开始,知道客户端与服务器断开连接为止

application:代表当前web应用,是ServletContext对象,能实现用户间数据的共享,可存放全局变量,它开始于服务器的启动,直到服务器的关闭,在此期间,此对象一直存在;这样在用户的前后连接或不同用户之间的连接中,可以对此对象的同一属性进行操作;在任何地方对此对象属性的操作,都将影响到其他用户对此的访问。服务器的启动和关闭决定了application对象的生命周期

⑥config:当前JSP对应的Servlet的ServletConfig对象,可获取该Servlet的初始化参数(开发时基本不用),需要通过映射的地址才可以。

out:JspWriter对象,调用out.println()可以直接把字符串打印到浏览器上

⑧page:page对象就是指向当前JSP页面本身,类型为Object,有点类似于类中的this,几乎不使用

⑨exception:该对象是一个例外对象,只有页面是一个错误页面,即isErrorPage设置为true的时候(默认为false)才能使用,否则无法编译。

注意:JSP可以放置在WEB应用程序中的除了WEB-INF及其子目录外的其他任何目录中


JSP模板元素

JSP页面中的静态HTML内容称之为JSP模版元素(比如html,body等等),在静态的HTML内容之中可以嵌套JSP的其他各种元素来产生动态内容和执行业务逻辑。

JSP模版元素定义了网页的基本骨架,即定义了页面的结构和外观。


JSP表达式

JSP表达式(expression)提供了将一个java变量或表达式的计算结果输出到客户端的简化方式,它将要输出的变量或表达式直接封装在<%= 和 %>之中。

在JSP表达式中嵌套的变量或表达式后面不能有分号。

<body>
    <%
        Date date = new Date();
    %>
    <%= date %>
</body>

JSP脚本片断

像片段一样的JSP表达式,嵌套在<%和%>中,必须全部是符合java语法的语句。

<body>
    <%
        String ageStr = request.getParameter("age");
        int age = Integer.parseInt(ageStr);
        if(age > 18){
    %>
        成年....
    <%  }else{ %>
        未成年...
    <%  } %>
</body>

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/3/9
  Time: 21:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="index.jsp" method="post">
    <input type="text" name="age">
    <input type="submit">
</form>
</body>
</html>

页面间跳转的2种方式---请求转发和请求重定向(重点)

一.请求转发

请求转发需要借助于一个接口,RequestDispatcher接口 ,利用这个接口的forward方法实现请求转发。

二.请求重定向

用HttpServletResponse的sendRedirect方法实现请求重定向。


请求转发

<body>
    <a href="forwardServlet">Forward</a>
</body>

@WebServlet("/forwardServlet")
public class ForwardServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("ForwardServlet's doGet");
        // 请求转发
        // 1.调用HttpServletRequest的getRequestDispatcher()方法获得RequestDispatcher对象
        // 调用getRequestDispatcher()需要传入要转发的地址(/代表当前WEB应用的根目录)
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/testServlet"); 
        // 2.调用HttpServletRequest的forward(request,response)进行请求转发
        requestDispatcher.forward(request, response);
    }
}

@WebServlet("/testServlet")
public class TestServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("TestServlet's doGet方法");
    }
}

运行html,点击查看控制台


image.png


请求重定向

<body>
    <a href="forwardServlet">Forward</a>
    <a href="redirectServlet">Redirect</a>
</body>

@WebServlet("/redirectServlet")
public class RedirectServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("RedirectServlet's doGet");
        response.sendRedirect("testServlet");
    }
}

再次运行html,点击Redirect,查看控制台


image.png


image.png

貌似与请求转发没什么区别,但实际区别非常之大。

观察用请求转发和请求重定向时地址栏的变化

请求转发地址栏没变


image.png

image.png

请求重定向地址栏变化了

image.png


image.png

说明一个重要问题(本质区别):


请求转发只发出了一个请求
请求重定向发出了2个请求

请求转发和请求重定向的目标可以是一个servlet也可以是一个jsp

request.getRequestDispatcher("/XXXServlet").forward(request,response)

response.sendRedirect("/XXXServlet")只能在doGet或doPost方法中调用一次

也即,在doGet或doPost方法中,只能转发或重定向到一个页面,不能到多个页面


属性相关方法


①设置属性:void setAttribute(String name,Object o)

②获取指定属性:Object getAttribute(String name)

能够使用这些方法的对象有4个:
pageContext,request,session,application-->这4个对象也称为域对象

域对象(重点)


域:

2个维度:一份数据可以在多少个页面间共享,可以在多长的时间范围内共享。

新建ServletA 书写一下代码运行测试

@WebServlet("/aaa")
public class ServletA extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        javax.servlet.jsp.PageContext pageContext = javax.servlet.jsp.JspFactory.getDefaultFactory().getPageContext(this, request, response,
                null, true, 8192, true);
        pageContext.setAttribute("pageAttr","pageValue");
        String strPageContext = (String)pageContext.getAttribute("pageAttr");
        System.out.println(strPageContext);
        request.setAttribute("requestAttr","requestValue");
        HttpSession session = request.getSession();
        session.setAttribute("sessionAttr","sessionValue");
        request.getServletContext().setAttribute("applicationAttr","applicationValue");
        request.getRequestDispatcher("/ccc").forward(request,response);
    }
}

ServletC

@WebServlet("/ccc")
public class ServletC extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("ServletC");
        javax.servlet.jsp.PageContext pageContext = javax.servlet.jsp.JspFactory.getDefaultFactory().getPageContext(this, request, response,
                null, true, 8192, true);
        String strPageContext = (String)pageContext.getAttribute("pageAttr");
        System.out.println(strPageContext);
        String strRequestContext = (String)request.getAttribute("requestAttr");
        System.out.println(strRequestContext);
        String strSessionContext = (String)request.getSession().getAttribute("sessionAttr");
        System.out.println(strSessionContext);
        String strAppContext = (String)request.getServletContext().getAttribute("applicationAttr");
        System.out.println(strAppContext);
    }
}

ServletB

@WebServlet("/bbb")
public class ServletB extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        javax.servlet.jsp.PageContext pageContext = javax.servlet.jsp.JspFactory.getDefaultFactory().getPageContext(this, request, response,
                null, true, 8192, true);
        pageContext.setAttribute("pageAttr","pageValue");
        String strPageContext = (String)pageContext.getAttribute("pageAttr");
        System.out.println(strPageContext);
        request.setAttribute("requestAttr","requestValue");
        HttpSession session = request.getSession();
        session.setAttribute("sessionAttr","sessionValue");
        request.getServletContext().setAttribute("applicationAttr","applicationValue");
        response.sendRedirect("/ccc");
    }
}

pageContext:属性的作用范围仅限于当前servlet或JSP页面

request:属性的作用范围仅限于同一个请求(考虑页面转发的情况)

session:属性的作用范围限于一次会话(浏览器打开直到关闭,称为一次会话,前提是在此期间会话没有失效),数据是用户独立的。(后面细讲)。

application:属性的作用范围限于当前WEB应用,是范围最大的属性作用范围,只要在一处设置属性,在其他各处的JSP或Servlet中都可以获取,直到服务器关闭。数据所有用户共享。

@WebServlet(name = "CountServlet",urlPatterns = "/count")
public class CountServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Object obj = request.getSession().getAttribute("count");
        if(obj == null)
        {
            request.getSession().setAttribute("count",1);
        }
        else
        {
            int count = Integer.parseInt(obj.toString());
            count++;
            request.getSession().setAttribute("count",count);
        }
        response.sendRedirect("index.jsp");
    }
}

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
<a href="/count">加1</a>
  ${count}
  </body>
</html>

servlet中通过getAttribute(String name),获取指定属性。而jsp中使用EL表达式。

EL表达式


EL能够极大的简化我们的开发

EL 全名为 Expression Language,它原本是 JSTL 1.0 为方便存取数据所自定义的语言。当时 EL只能在JSTL标签中使用。到了JSP2.0 之后,EL已经正式纳入成为标准规范之一。


语法


EL 语法很简单,它最大的特点就是使用上很方便。接下来介绍 EL 主要的语法结构

${requestAttr}
${user.sex}

所有 EL 都是以 ${ 为起始、以} 为结尾的。

User user = (User)session.getAttribute("user"); 
String sex = user.getSex( );

两者相比较之下,可以发现 EL 的语法更为方便、简洁。


EL 隐含对象


如果我们在request和application中设置了同名属性怎么办?

与范围有关的隐含对象

applicationScope

sessionScope

requestScope

pageScope


JSP中如何做条件判断和遍历集合


JSTL


JavaServer Pages Standard Tag Library (1.1 ) ,它的中文名称为 JSP 标准标签函数库。JSTL 是一个标准的已制定好的标签库,可以应用于各种领域,如:基本输入输出、流程控制、循环。


安装


直接把JSTL有关的两个jar包,拷贝进lib目录下

然后在jsp页面上添加指令,就可以使用了

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

流程控制

<c:if>

<c:if>的用途就和我们一般在程序中用的 if 一样


image.png

image.png

<!-- 没有else,但能储存结果 -->
    <c:if test="${ sessionScope.age > 18 }">成年人</c:if>

<c:choose>

<c:choose>本身只当做 <c:when> 和 <c:otherwise> 的父标签。

<c:when> 和 <c:otherwise>也不能脱离<c:choose>。

<c:otherwise>必须在<c:when>之后使用。

<c:choose>
        <c:when test="${param.age > 60 }">
            老年
        </c:when>
        <c:when test="${param.age > 40 }">
            中年
        </c:when>
        <c:when test="${param.age > 18 }">
            青年
        </c:when>
        <c:otherwise>
            未成年
        </c:otherwise>
    </c:choose>

迭代操作

<c:forEach>

<c:forEach> 为循环控制,它可以将集合(Collection)中的成员循序浏览一遍。运作方式为当条件符合时,就会持续重复执行<c:forEach>的本体内容。


image.png

image.png

遍历Collection(数组也一样)

<%
    <c:forEach items="${ requestScope.list }" var="customer">
        ${ customer.id } --${ customer.name } --${ customer.address } --${ customer.phone }<br><br>
    </c:forEach>

遍历Map集合

<c:forEach items="${ requestScope.map }" var="customer">
        ${ customer.key } --- ${ customer.value.id },${ customer.value.name },${ customer.value.address },${ customer.value.phone }<br><br>
    </c:forEach>


JSP


JSP指令


新建一个jsp文件,最上面一行就是jsp指令

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分。

JSP指令的基本语法格式:

<%@ 指令 属性名="值" %>

include指令

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>A Page</h1>
    <!-- 在a.jsp中包含b.jsp,file后的资源写的是相对路径,如果有/代表是wen应用根目录 -->
    <%@include file="b.jsp" %>
</body>
</html>


目录
相关文章
|
2月前
|
JavaScript Java
jsp使用
jsp使用
|
7月前
|
Java 数据库
Jsp应用
Jsp应用
30 0
【JSP】jsp的组成
在上一篇博客【jsp】为什么出现JSP?,讲了jsp出现的原因,接下来这篇博客,将向大家讲述jsp中都有哪些内容。
|
Java
jsp处理
jsp处理
92 0
|
开发框架 Java 数据库连接
为什么使用JSP?
为什么使用JSP?
286 0
|
Java 数据库连接 API
使用jsp
使用jsp
110 0
|
Java
JSP 处理
JSP 处理
76 0
|
安全 Java 应用服务中间件
jsp
jsp
154 0
jsp
|
前端开发 搜索推荐 Java
JSP是什么
JSP是什么
259 0
JSP是什么
|
XML SQL Java
JSP(三)
JSP(三)
136 0
JSP(三)