文章目录
JSP基本介绍
一、基本语法
-
使用方式
-
<%--JSP注释格式 JSP作用:用来将程序输出到浏览器 <%= 变量/表达式%> --最简单的使用方式 --%> <h1 align="center"><%= new java.util.Date()%></h1> <%--JSP脚本片段(不同的片段里的变量名不得重复,并且可以跨片段使用) 脚本片段里的代码会原封不动的转换未java代码 --%> <% int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; } out.print("<h1 align='center'>"+sum+"</h1>"); %> <%--在代码中嵌入html代码--%> <% for (int i = 0; i < 5; i++) { %> <h1 align="center">i am wjr<%= i%></h1> <% } %>
-
二、原理
-
本质
-
本质就是一个Servlet
-
在tomcat启动访问一个jsp文件时,会自动创建一个对应的jspName_jsp.class的文件
-
文件路径
C:\Users\Lazy r\.IntelliJIdea2019.2\system\tomcat\Unnamed_JavaWeb_Study_3\work\Catalina\localhost\study\org\apache\jsp
-
-
编译成的Servlet的核心内容
-
上述jsp代码编译成了index_jsp类中的**_jspService方法里的下述代码(非核心代码已经略去),根据生成的代码发现jsp中的注释不会再客户端显示,而html的注释会(html的未演示)**
public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent, org.apache.jasper.runtime.JspSourceImports { public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException { ...... //九大隐式对象 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.jsp.JspWriter _jspx_out = null;//请求 javax.servlet.jsp.PageContext _jspx_page_context = null;//响应 //该对象仅在jsp中使用了<%@page isErrorPage="true" %>,才会生成 java.lang.Throwable exception = org.apache.jasper.runtime.JspRuntimeLibrary. getThrowable(request); if (exception != null) { response.setStatus(javax.servlet.http.HttpServletResponse. SC_INTERNAL_SERVER_ERROR); } ...... //将jsp中的内容输出到浏览器 out.write("<html>\n"); out.write("<head>\n"); out.write(" <title>jsp</title>\n"); out.write("</head>\n"); out.write("<body>\n"); out.write("\n"); out.write("<h1 align=\"center\">"); out.print( new java.util.Date()); out.write("</h1>\n"); out.write("\n"); out.write('\n'); int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; } out.print("<h1 align='center'>"+sum+"</h1>"); out.write('\n'); out.write('\n'); out.write('\n'); for (int i = 0; i < 5; i++) { out.write("\n"); out.write(" <h1 align=\"center\">i am wjr"); out.print( i); out.write("</h1>\n"); } out.write("\n"); out.write("\n"); out.write("</body>\n"); out.write("\n"); out.write("\n"); out.write("</html>\n"); ...... } }
-
-
九大隐式对象
- PageContext pageContext
- 页面上下文
- HttpSession session
- Session
- ServletContext application
- applicationContext
- ServletConfig config
- config
- JspWriter out
- out
- Object page
- page:默认当前页
- JspWriter _jspx_out
- 请求
- PageContext _jspx_page_context
- 响应的内容
- Throwable exception
- 该对象仅在jsp中使用了<%@page isErrorPage=“true” %>,才会生成
- PageContext pageContext
-
四大作用域(由低到高)
-
pageContext:不用了解;
-
request:客户端想服务器发送数据,产生的数据,用户看完就没用了,如新闻(用户看完就没用了);
-
session:客户端想服务器发送数据,产生的数据,用户用完一会还有,如购物车;
-
application:客户端想服务器发送数据,产生的数据,一个用户用完了,其他用户还可以使用,如聊天数据;
-
<% pageContext.setAttribute("name1","wjr1");//保存的数据只在一个页面有效 request.setAttribute("name2","wjr2");//保存的数据旨在一次请求中有效,请求转发会携带这个数据 session.setAttribute("name3","wjr3");//保存的数据只在一次会话中有效,从打开浏览器到关闭浏览器 application.setAttribute("name4","wjr4");//保存的数据只在服务器中有效,从打开服务器到关闭服务器 %> <% //从pageContext取出,从底层到高层(作用域) //page->request->session->application //JVM双亲委派机制 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"); String name6 = (String)pageContext.getAttribute("name6"); %> <%-- el表达式:${...}} --%> <h1 align="center">${name1}</h1> <h1 align="center">${name2}</h1> <h1 align="center">${name3}</h1> <h1 align="center">${name4}</h1> <h1 align="center">el表达式:${name5}</h1> <h1 align="center">jsp:<%=name6%></h1>
-
三、EL表达式、JSP标签、JSTL标签
-
EL表达式
- 使用
- 语法:$(…)
- 获取数据
- 执行运算
- 获取web开发的常用对象
- 使用
-
JSP标签
-
使用
<%@page arg="..." %> <%--arg表示各种参数--%> <html> <%--引入别的jsp文件--%> <%--两个页面合二为一--%> <%@include file="serverError.jsp"%> <%--两个页面拼接,一般使用这个--%> <jsp:include page="serverError.jsp"/> <%--其他指令--%> <jsp:arg1 .../> <%--请求转发--%> <jsp:forward page="object.jsp"> <%-- 数据放入request --%> <jsp:param name="name" value="wjr"/> </jsp:forward> <%-- 取出放入的数据 --%> <%= request.getParameter("name")%> </html>
-
-
JSTL标签
-
作用
- 弥补html标签的不足,可以供我们使用,标签的功能和Java代码一样
-
学习网站
-
使用方式
-
引入依赖
<dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency>
-
随上述学习网站使用
-
示例
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%--引入JSTL核心标准标签库--%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>JSTL</title> </head> <body> <div align="center"> <form action="JSTL.jsp" method="get"> <%--${param.get("name")} 获取表单中数据--%> username:<input type="text" name="username" value="${param.get("username")}"> <input type="submit"> </form> <%--判断数据是否符合标准--%> <c:if test="${param.username == 'wjr'}" var="isHost"> <c:out value="欢迎主人回来,${isHost}"/> </c:if> <c:if test="${param.username != 'wjr'}" var="isBad"> <c:out value="你是坏蛋,${isBad}"/> </c:if> </div> </body> </html>
-
-