JSP简介
JSP全名为Java Server Pages,其根本是一个简化的Servlet设计,是一种动态网页技术标准
它实现了Html语法中的java扩展(以 <%, %>形式),JSP与Servlet一样,是在服务器端执行的
3个指令
include指令
在JSP中,可以使用include指令来包含其他jsp文件
include的指令的语法如下:<%@ include file="文件路径"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>这是一个表头</h1> </body> </html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <%@ include file="header.jsp" %> <h1>这是一个主体部分</h1> </body> </html>
page指令 主要用来设置页面的属性,比如导入包、指明输出的页面类型、控制session等
<%@page import="java.util.Date"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% Date date = new Date(); System.out.println(date); %> <form action="${pageContext.request.contextPath}/login" method="post"> <span>用户名:</span><input type="text" name="userName"> <span>密码:</span><input type="text" name="userPwd"> <input type="submit" value="提交"> </form> </body> </html>
taglib指令 用来引用标签库并设置标签库的前缀
<%@page import="java.util.ArrayList"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% List<String> list = new ArrayList<>(); list.add("AA"); list.add("BB"); request.getSession().setAttribute("list", list); %> <c:forEach var="str" items="${list}"> <table> <tr> <td>${str}</td> </tr> </table> </c:forEach> </body> </html>
4大作用域
- pageContext 页面域,页面作用域仅限于当前页面对象
- request 请求域,请求作用域是同一个请求之内,在页面跳转时,如果通过forward方式跳转,则forward目标页面仍然可以拿到request中的属性值
- session 会话域,会话作用域是在一个会话的生命周期内,会话失效,则session中的数据也随之丢失
- application 应用域,应用作用域是最大的,只要服务器不停止,则application对象就一直存在,并且为所有会话所共享
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% pageContext.setAttribute("pageContext", "pageContext作用域"); request.setAttribute("request", "request作用域"); session.setAttribute("session", "session作用域"); application.setAttribute("application", "application作用域"); request.getRequestDispatcher("target.jsp").forward(request, response); %> </body> </html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% out.println(pageContext.getAttribute("pageContext")+"<br>"); out.println(request.getAttribute("request")+"<br>"); out.println(session.getAttribute("session")+"<br>"); out.println(application.getAttribute("application")); %> </body> </html>
6个动作
JSP中的六个动作:
- <jsp:include > 动态包含
- <jsp:forward> 请求转发
- <jsp:param> 设置请求参数
- <jsp:useBean> 创建一个对象
- <jsp:setProperty> 给指定的对象属性赋值
- <jsp:getProperty> 取出指定对象的属性值
9大内置对象
JSP共有以下9大内置对象:
- out 主要用来向客户端输出数据
- request 来自客户端的请求经Servlet容器处理后,由request对象进行封装
- response 它封闭了JSP 的响应,然后被发送到客户端以响应客户的请求
- session 表示一个会话,用来保存用户信息,以便跟踪每个用户的状态
- pageContext 本JSP的页面上下文
- application 从servlet配置对象获得的servlet上下文
- config 取得指定名称的初始化参数内容
- page 指向当前jsp页面本身
- exception 本JSP页面的异常对象。只有配置<%@page isErrorPage="true"%> 后才能使用
配置Web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>JSP_Practise</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>implicitServlet</servlet-name> <!--指定哪一个JSP页面配置成Servlet --> <jsp-file>/implicit.jsp</jsp-file> <init-param> <param-name>userName</param-name> <param-value>jtx</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>implicitServlet</servlet-name> <url-pattern>/implicitServlet.do</url-pattern> </servlet-mapping> </web-app>
JSP页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="${pageContext.request.contextPath}/implicitServlet.do?userPwd=123456">跳转测试九个内置对象</a> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page isErrorPage="true"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% out.println("out内置对象" + "<br>"); request.setAttribute("request", "request内置对象"); out.println(request.getAttribute("request") + "<br>"); response.setCharacterEncoding("UTF-8"); session.setAttribute("session", "session内置对象"); out.println(session.getAttribute("session") + "<br>"); application.setAttribute("application", "application内置对象"); out.println(application.getAttribute("application") + "<br>"); pageContext.setAttribute("pageContext", "pageContext内置对象"); out.println(pageContext.getAttribute("pageContext") + "<br>"); String userName = config.getInitParameter("userName"); %> <h3><%=userName%></h3> <h3><%=request.getParameter("userPwd")%></h3> </body> </html>