Web 基础15 JSP入门 指令及其部分属性

2018-03-31  本文已影响9人  小熊先生很不开心

1.1 指令的概述

<%@ 指令名称 属性="值" %>分类
<%@ page contentType="text/html;charset=UTF-8" %>

  如果一个指令有多个属性,可以把多个属性写在同一个指令中,也可以把多个属性分开来写

<%@ page contentType="text/html;charset=UTF-8" import="java.util.Date" %>
                
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.util.Date" %> 

1.2 page指令的属性

1.3 page指令的其他属性

  一个软件的默认值的设置,考经验得来的
刚开始,靠项目经理的经验设置
之后,通过压力测试,试运行,通过真实的用户数据 分析之后再做出优化 才去调整
贸然去改 会对系统造成很大的影响

1.4 JSP的异常处理机制

<error-page>
    <exception-type>java.lang.ArithmeticException</exception-type>
    <location>/error.jsp</location>
</error-page>
<error-page>
    <error-code>500</error-code>
    <location>/error.jsp</location>
</error-page>

XML设置

<error-page>
    <error-code>500</error-code>
    <location>/500.jsp</location>
  </error-page>
  
   <error-page>
    <error-code>400</error-code>
    <location>/400.jsp</location>
  </error-page>

错误代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>服务器内部出现异常,请联系管理员!</h1>
    <%
        //String message = exception.getMessage();
        //out.println(message);
    %>
</body> 
</html>

跳转页面


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>服务器忙,请稍后再试!</h1>
</body>
</html>

1.5 include指令&taglib指令

主页

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%@ include file="top.jsp" %>
    <hr />
    <%@ include file="body.jsp" %>
    <hr />
    <%@ include file="bottom.jsp" %>
</body>
</html>

分页

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<h1>传智播客</h1>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<h1>注册表单</h1>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<h1>友情链接</h1>

  这样使用include 被包含的网页是不需要body ,head等标签的,为什么呢。大家看完翻译后的文件可以知道了,如过被包含的文件依旧有body等都会被并接进翻译的文件中,实际上我们只需要一个body就可以了。

1.6 JSP的九大内置对象

这些内置对象都可以直接使用提高开发效率

1.7 PageContext的概述和测试

<%
        //获取其他八个内置对象
        //pageContext.getRequest();
        //pageContext.getResponse();
        //pageContext.getSession();
        
        //保存数据
        
        //设置数据
        //pageContext.setAttribute("username", "zhangsan");
        //删除数据
        //pageContext.removeAttribute("username");
        //获取数据
        //Object obj = pageContext.getAttribute("username");
        //out.println(obj);
        
        //转发
        //pageContext.forward("/error.jsp");
        //包含
        //pageContext.forward("/error.jsp");
        
        //操作其他域对象中的数据
        pageContext.setAttribute("username", "lisi", PageContext.SESSION_SCOPE);
        //Object sessionObj = pageContext.getAttribute("username", PageContext.SESSION_SCOPE);
        //分别在4个域对象当中查找数据,顺序pageContext,request,session,application
        Object sessionObj = pageContext.findAttribute("username");
        out.println(sessionObj);
    %>

1.8 JSP的四大域对象

域对象 作用域
pageContext 当前页面
request 当前请求
session 整个会话
application 整个应用
<%
        //pageContext   当前页面
        //pageContext.setAttribute("username", "zhangsan");
        //out.println(pageContext.getAttribute("username"));
        
        //request       当前请求
        //request.setAttribute("username", "zhangsan");
        //out.println(request.getAttribute("username"));
        //pageContext.forward("/scope/scope02.jsp");
        
        //session       整个会话
        //session.setAttribute("username", "zhangsan");
        //out.println(session.getAttribute("username"));
        
        //application   整个应用
        application.setAttribute("username", "zhangsan");
        out.println(application.getAttribute("username"));
    %>

1.8 的动作标签

静态包含和动态包含.png
上一篇下一篇

猜你喜欢

热点阅读