JavaWeb-----JSP详解

   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 heretitle>
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 heretitle>
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 heretitle>
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 heretitle>
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 heretitle>
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 heretitle>
head>
<body>
    <%
        out.println(pageContext.getAttribute("pageContext")+"
"); out.println(request.getAttribute("request")+"
"); out.println(session.getAttribute("session")+"
"); out.println(application.getAttribute("application")); %> body> html>

   6个动作

     JSP中的六个动作:

  •  动态包含
  •  请求转发
  •  设置请求参数  
  •  创建一个对象
  •  给指定的对象属性赋值
  •  取出指定对象的属性值

   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_Practisedisplay-name>
    <welcome-file-list>
        <welcome-file>index.jspwelcome-file>
    welcome-file-list>
    <servlet>
        <servlet-name>implicitServletservlet-name>
        
        <jsp-file>/implicit.jspjsp-file>

        <init-param>
            <param-name>userNameparam-name>
            <param-value>jtxparam-value>
        init-param>

        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>implicitServletservlet-name>
        <url-pattern>/implicitServlet.dourl-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 heretitle>
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 heretitle>
head>
<body>
    <%
        out.println("out内置对象" + "
"); request.setAttribute("request", "request内置对象"); out.println(request.getAttribute("request") + "
"); response.setCharacterEncoding("UTF-8"); session.setAttribute("session", "session内置对象"); out.println(session.getAttribute("session") + "
"); application.setAttribute("application", "application内置对象"); out.println(application.getAttribute("application") + "
"); pageContext.setAttribute("pageContext", "pageContext内置对象"); out.println(pageContext.getAttribute("pageContext") + "
"); String userName = config.getInitParameter("userName"); %> <h3><%=userName%>h3> <h3><%=request.getParameter("userPwd")%>h3> body> html>

 

转载于:https://www.cnblogs.com/fengfuwanliu/p/10523416.html

你可能感兴趣的:(JavaWeb-----JSP详解)