首页 > 编程语言 > 详细

JavaWeb(JSP)-----JSP详解

时间:2019-03-15 16:14:36      阅读:143      评论:0      收藏:0      [点我收藏+]

   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等

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
  • response
  • session
  • pageContext
  • application
  • config
  • page
  • exception

JavaWeb(JSP)-----JSP详解

原文:https://www.cnblogs.com/fengfuwanliu/p/10523416.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!