码迷,mamicode.com
首页 > Web开发 > 详细

Day10(--JSP与JSP内置对象)

时间:2015-09-26 00:17:43      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:

一、JSP

1.   java Server Pages,sun提供的一种动态的web资源底层原理是Servlet

2.   HTMLServletJSP的区别

l  HTML:显示数据

l  ServletJava代码,可以向浏览器输出HTMJSP的运行时会编译成Servlet去输出到浏览器

l  JSPJava+HTML

3.   JSP的执行流程:

l  浏览器发送数据到tomcattomcat中有JSP引擎

l  第一次访问jsp时,将jsp翻译成一个servlet

l  修改后第一次访问,重新翻译成一个servlet

l  servlet 响应:Response 响应到浏览器

4.   注释

l  HTML注释:<!---->查看源中可以看到  servlet可以看到

l  Java注释: /* */ //  查看源中不可以看到 servlet可以看到

l  jsp注释:  <%--%>     jsp注释在servlet不可以看到

5.   注意:

l  HTML模板:JSP中包含HTML模板,所有的HTML标签随便写,最后通过Writer输出

l  Java代码:  写在<%%>中,所有的代码都会移植到编译成的servletservice方法中

l  Server中不能直接定义方法可以通过声明 <%! %>可以声明方法和成员变量

l  表达式:<%=%> 表达式,用于输出,相当于out.print();

l  指令:<%@指令名称 属性=’属性=’’> 多个属性可以使用空格分隔

6.   page:页面的配置信息

l  ContendType:告诉浏览器响应的编码和类型

l  import:导包

l  ErrorPage:jsp出现异常后跳转的页面

l  isErrorPage:(true)如果是错误页面,可以得到错误信息

l  404不能去isErrorPage页面,404不是异常,是找不到的,false可以

l  Session:如果为true,则可以获取session的内置对象,默认值为true

l  pageEncoding:翻译成Servlet时,解析JSP时的编码,响应的类型和编码

7.   Taglib:导标签库

l  <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

        <c:if test="<%=2 > 1 %>">

         //执行

  </c:if>

8.   include:包含

l  静态包含:(指令)包含的jsp和被包含的jsp合并成一个servlet  包含时,上边的变量,下面可以使用

<%@ include file=”/”%>  /代表当前应用 

l  动态包含:(标签)包含的jsp和被包含的jsp都会生成不同的servlet,最终合并了输出流

<jsp:include page=”/”></jsp :include> 可以没有子标签,不在同一行,必须存在子标签

9.   异常的处理:就近原则

l  方式一:在page指令中添加errorPage属性,说明出项异常后的跳转页面(只能是异常)不是404错误

配置页面:<%@ page  pageEncoding="UTF-8" errorPage="/error.jsp"%>

错误页面:<%@ page  pageEncoding="UTF-8" isErrorPage="true"%>

显示错误信息:<%=exception.getMessage() %>

l  方式二:在web.xml配置错误的类型和错误编码,可以配置出现404时跳转的页面

错误跳转:不能跳转到isErrorPagetrue的页面,它只接受异常

<error-page>

    <error-code>404</error-code>

    <location>/error.jsp</location>

  </error-page>

异常跳转:

  <error-page>

    <exception-type>java.lang.NullPointerException</exception-type>

    <location>/error.jsp</location>

</error-page>

二、jsp中的九大内置对象:

对象名称

类型

备注

request

javax.servlet.http.HttpServletRequest

请求

response

javax.servlet.http.HttpServletResponse

响应

session

javax.servlet.http.HttpSession

 

application

javax.servlet.ServletContext

上下文

config

java.servlet.ServletConfig

 

out

java.servlet.jsp.JapWriter

èresponse.getWriter()

page

javax.servlet.Servlet

Servlet当前引用

exception

java.lang.Exception

异常

pageContext

java.servlet.jsp.PageContext

获取上8个内置对象

l  pageContext 可以获取其他8个内置对象

l  //有简化的转发和包含功能(请求的调度)dispatcher

l  pageContext.forward("/index.jsp");   转发

l  request.getRequestDispatcher("/index.jsp").forward(request, response);

l  pageContext.include("/myHead.jsp");  包含

l  request.getRequestDispatcher("/myHead.jsp").include(request, response);

l  //查找域中的属性,pageContext,request,session,application 范围为域对象的有效范围才可以查找到

l  out.println(pageContext.findAttribute("username"));

l  ps:响应可以实现重定向 response.sendRedirect("/index.jsp");   /代表tomcat根目录

三、四大域对象(作用范围)

1.   application:整个应用

l  application.setAttribute("username", "lisi");设置

l  <%=application.getAttribute("username")%>    获取

l  pageContext.removeAttribute("username");     设置后需要删除,不然一直存在

2.   session:同一会话,jsessionID一样

l  session.setAttribute("username", "zhangsan");设置

l  <%=session.getAttribute("username") %>       获取

3.   request:当前页面,共享请求时,比如转发和包含

l  request.setAttribute("username", "zhangsan");设置

l  <%=request.getAttribute("username") %>       获取

l  设置转发和包含时,可以作用到转发到的页面和包含当前页面的页面!

l  <%@ include file="10pageContext.jsp" %>      包含

l  request.getRequestDispatcher("/testpageContext.jsp").forward(request, response);转发

4.   pageContext:当前页面

l  pageContext.setAttribute("username", "zhangsan");设置

l  <%=pageContext.getAttribute("username") %>       获取

Day10(--JSP与JSP内置对象)

标签:

原文地址:http://www.cnblogs.com/Probably/p/4839744.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!