1. 什么是JSP(Java Server Pages)
1.1概述
🐲简化的Servlet设计,在HTML标签中嵌套Java代码,用以更新开发Web应用的动态网页
🦄JSP文件在容器中会转换成Servlet执行
🍕JSP是对Servlet的一种高级封装。本质还是Servlet
🐷与Servlet相比,JSP可以很方便的编写或者修改HTML网页而不用去面对大量的prinlln语句
👾Tomcat会将xxx.jsp文件转换成Java代码,进而编译成.class文件运行,最终将运行结果通过response响应给客户端
1.2 作用
替换显示页面部分的Servlet(使用*.jsp文件文件替换XxxJSP.java)
2. 创建并访问JSP
web同级目录下右键 --> new --> JSP/JSPX --> 输入文件名 --> 选择JSP file创建
1. 运行Tomcat,在浏览器上添加上.jsp文件的名称如first.jsp
3. 普通脚本、声明脚本、输出脚本、注释
3.1 普通脚本
<% %>
浏览器输出结果为
3.2 声明脚本
<%! %>
3.3输出脚本
<%=Java表达式%>
例题
3.4注释
<%-- 这里是JSP注释,内容不会被发送至浏览器甚至不会被编译 --%>
<!-- 这里是Html注释,通过浏览器查看网页源代码时可以看见注释内容 -->
3. JSP头部的page指令
(1) contentType属性:表示JSP返回的数据类型是什么,即response.setContentType()的参数值
(2) language属性:表示JSP翻译之后是什么语言文件(目前只支持Java)
(3) pageEncoding属性:表示当前JSP文件本身的字符集(可在IDEA右下角看到)
(4) import属性:表示导包(导类),与Java一致
(5) autoFlush属性:设置当out输出流缓冲区满了之后是否自动刷新缓冲区,默认值是true
(6) buffer属性:设置out缓冲区的大小,默认是8kb
注意:out缓冲区满了之后不能自动刷新的话会报错
(7) errorPage属性:设置当JSP页面运行出错时自动跳转到的页面(错误信息页面)的路径,这个 路径一般都是以斜杠打头,表示请求的地址是http://ip:port/工程路径/,对应代码web目录
(8) isErrorPage属性:设置当前JSP页面是否是错误信息页面,默认是false,如果是true可以 获取错误信息
(9) session属性:设置访问当前JSP页面时是否会创建HttpSession对象,默认值是true
(10) extends属性:设置JSP页面翻译出来的Java类默认继承谁
————————————————
版权声明:本文为CSDN博主「BlogGao」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_49343190/article/details/107932765
4. include静态包含(可能会有重名冲突的问题,不建议使用)
语法:
<%@inclued…%>
4.1 例题
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Header页面</title>
</head>
<body>
<h1>header页面</h1>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>footer页面</title>
</head>
<body>
<h1>footer页面</h1>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="header.jsp"%>
<html>
<head>
<title>Main页面</title>
</head>
<body>
<h1>main页面</h1>
</body>
</html>
<%@include file="footer.jsp"%>
访问main.jsp显示结果
静态包含只是把header.jsp和footer.jsp两个页面的输出代码复制到了当前JSP文件的.class中
相当于是复制到了同一文件下
4.2静态包含的重名冲突
当在header.jsp或者footer.jsp下面用普通脚本定义一个String message="helloworld";
变量
在同时引用了header.jsp和footer.jsp的main.jsp文件里就不能再次使用message这个变量名
会报错
5. taglib指令
在学习JSTL时再了解就行
6. 动作标签
语法
<jsp: action_name attribute=“value”/>
6.1 动作标签之include动态包含
属性:page -->包含在页面中的相对URL地址
语法
<jsp:include page=“index.jsp”/>
这里的jsp:include动作与静态包含时不同,是将外部文件的输出结果引入到了当前JSP文件中
6.1.1 例题
6.1.2 运行结果
6.2 动作标签useBean、setProperty、getProperty
属性:id 、 class
6.2.1语法:
<jsp:useBean id=“name” class=“package.className”/>
jsp:useBean动作用来加载一个将在JSP页面中使用的JavaBean
<jsp:setProperty name=“” property=“” value=“” />
<jsp:getProperty name=“” property=“” />
6.2.2 例题
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>useBean</title>
</head>
<body>
<%--id=对象的名字 class=全限定名--%>
<jsp:useBean id="user" class="com.qf.project.entity.Admin"/>
<jsp:setProperty name="user" property="username" value="tom"/>
<jsp:setProperty name="user" property="password" value="123456"/>
<jsp:setProperty name="user" property="phone" value="17654815467"/>
<jsp:setProperty name="user" property="address" value="中原工学院"/>
<jsp:getProperty name="user" property="username"/>
<jsp:getProperty name="user" property="password"/>
<jsp:getProperty name="user" property="phone"/>
<jsp:getProperty name="user" property="address"/>
</body>
</html>
运行结果:
7.1 动作标签之forward、param
语法
<jsp:forward page=" "/>
jsp:forward动作把请求转到另外的页面
7.1.1 forward例题
当在浏览器访问A页面时跳转到B页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>A页面</title>
</head>
<body>
<jsp:forward page="B.jsp"></jsp:forward>
</body>
</html>
7.1.2 param例题:把A.jsp页面跳转到B.jsp页面时传递一个参数给B让它输出
语法
<jsp:param name=“” value=“” />
在转发动作内部使用,做参数传递
A页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>A页面</title>
</head>
<body>
<jsp:forward page="B.jsp">
<jsp:param name="name" value="libai"/>
</jsp:forward>
</body>
</html>
B页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>B页面</title>
</head>
<body>
<h1>B页面</h1>
<%
String message =request.getParameter("name");
%>Re
<%=message%>
</body>
</html>
8. 内置对象
由JSP自动创建的对象,可以直接使用
对象名 | 类型 | 说明 |
---|---|---|
request | javax.servlet.http.HttpServletRequst | |
response | javax.servlet.http.HttpServletResponse | |
session | javax.servlet.http.HttpSession | 由session="true"开关 |
application | javax.servlet.ServletContext | |
config | javax.servlet.ServletConfig | |
exception | java.lang.Throwable | 由isErrorPage="fage"开关 |
out | javax.servlet.jsp.JspWriter | |
pageContext | javax.servlet.jsp.PageContext | |
page | java.lang.Object当前对象this | 当前servlet实例 |
9. 四大域对象
JSP有四大域对象,存储数据和获取数据的方式一样,不同的是取值的范围有差别
pageContext(javax.servlet.jsp.PageContext)当前JSP页面范围
request(javax.servlet.http.HttpServletReques)一次请求有效(在浏览器未关闭下重新访问再次请求则得到null)
session (javax.servlet.http.HttpSession)一次会话有效(关闭浏览器失效)
application (javax.servlet.ServletContext)整个Web应用有效(服务器重启或关闭失效)
9.1 例题
9.1.1 四大域对象之pageContext
pageContext因为是在当前JSP页面有效,一旦跳转则失效
通过setAttribute(“name”,value);存储值
通过getAttribute(“name”);获取值
用于获取其他8个内置对象或者操作其他对象的作用域
所以在Xxx.jsp页面写
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<%
pageContext.setAttribute("pageContextScope","are you pig?");
String Areyoupig= (String)pageContext.getAttribute("pageContextScope");
%>
<h1>pageContextScope值为:<%=Areyoupig%></h1>
</body>
</html>
输出结果为
9.1.2 四大域对象之request
一次请求有效(在浏览器未关闭下重新访问再次请求则得到null)request要通过getRequestDispatcher()传值
在Servlet页面编写
ScodeServlet页面
@WebServlet(name = "ScodeServlet", value = "/scope")
public class ScodeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//HttpServletReques
request.setAttribute("requestScope","bbb");
//request要通过getRequestDispatcher()传值
request.getRequestDispatcher("/innerObj.jsp").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<%
pageContext.setAttribute("pageContextScope","are you pig?");
String Areyoupig= (String)pageContext.getAttribute("pageContextScope");
String bbb = (String)request.getAttribute("requestScope");
%>
<h1>pageContextScope值为:<%=Areyoupig%></h1>
<h1>requestScope值为:<%=bbb%></h1>
</body>
</html>
注意在浏览器要访问的Servlet页面scope,不能直接访问innerObj.jsp运行结果如下
9.1.3 四大域对象之session、application
利用sendRedirect()重定向传值
例题
@WebServlet(name = "ScodeServlet", value = "/scope")
public class ScodeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("requestScope", "bbb");
// request.getRequestDispatcher("/innerObj.jsp").forward(request, response);
request.getSession().setAttribute("sessionScope", "ddd");
request.getServletContext().setAttribute("sevletContextScope", "ccc");
//利用sendRedirect()重定向,所以必须使用session
response.sendRedirect(request.getContextPath() + "/innerObj.jsp");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<%
pageContext.setAttribute("pageContextScope","are you pig?");
String Areyoupig= (String)pageContext.getAttribute("pageContextScope");
String bbb = (String)request.getAttribute("requestScope");
String ccc = (String)session.getAttribute("sessionScope");
String ddd = (String)application.getAttribute("sevletContextScope");
%>
<h1>pageContextScope值为:<%=Areyoupig%></h1>
<h1>requestScope值为:<%=bbb%></h1>
<h1>sessionScope值为:<%=ccc%></h1>
<h1>application值为:<%=ddd%></h1>
</body>
</html>
运行结果
10. pageContext作用域对象
1. 调用8大内置对象 2.操作作用域对象request\session\application
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>pageContext页面</title>
</head>
<body>
<%
<%--调用8大内置对象--%>
pageContext.getRequest();
pageContext.getResponse();
pageContext.getException();
pageContext.getPage();
pageContext.getServletConfig();
pageContext.getSession();
pageContext.getServletContext();
pageContext.getOut();
%>
<%
pageContext.setAttribute("page", "1234");
pageContext.setAttribute("req", "aaa", PageContext.REQUEST_SCOPE);
pageContext.setAttribute("sess","bbb",PageContext.SESSION_SCOPE);
pageContext.setAttribute("app","ccc",PageContext.APPLICATION_SCOPE);
String req = (String)request.getAttribute("req");
String sess = (String)session.getAttribute("sess");
String app = (String)application.getAttribute("app");
%>
<h1>request:<%=req%></h1>
<h1>session:<%=sess%></h1>
<h1>app:<%=app%></h1>
</body>
</html>
运行结果