• 动态页面技术之JSP


    1.什么是JSP技术

    JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它是由Sun Microsystems公司倡导、许多公司参与一起建立的一种动态网页技术标准。
    JSP技术有点类似ASP技术,它是在传统的网页HTML(标准通用标记语言的子集)文件(*.htm,*.html)中插入Java程序段(Scriptlet)和JSP标记(tag),从而形成JSP文件,后缀名为(*.jsp)。
    用JSP开发的Web应用是跨平台的,既能在Linux下运行,也能在其他操作系统上运行。

    2.JSP常用脚本

    1>.<%java代码%> ----- 内部的java代码翻译到service方法的内部

    2>.<%=java变量或表达式> ----- 会被翻译成service方法内部out.print()

    3>.<%!java代码%> ---- 会被翻译成servlet的成员的内容

     

    3.JSP注释及其注释作用范围

    1>.Html注释:<!--注释内容--> ---可见范围 jsp源码、翻译后的servlet、页面显示html源码

    2>.java注释://单行注释  /*多行注释*/ --可见范围 jsp源码 翻译后的servlet

    3>.jsp注释:<%--注释内容--%> ----- 可见范围 jsp源码可见

    注意:java注释必须在jsp内部才能使用

     

    4.jsp的运行原理

    jsp在第一次被访问时会被Web容器翻译成servlet,在执行

    过程:

    第一次访问---->helloServlet.jsp---->helloServlet_jsp.java---->编译运行

    PS:被翻译后的servlet在Tomcat的work目录中可以找到

    5.jsp常用指令

    1>page指令:

    1.属性最多的指令(实际开发中page指令默认)属性最多的一个指令,根据不同的属性,指导整个页面特性

    格式:<%@ page 属性名1= "属性值1" 属性名2= "属性值2" ...%>

    <%@ page language="java" contentType="text/html; charset=UTF-8"  errorPage="/errorPage.jsp" pageEncoding="UTF-8"%>

    2>language:jsp脚本中可以嵌入的语言种类

    3>pageEncoding:当前jsp文件的本身编码---内部可以包含contentType   contentType:response.setContentType(text/html;charset=UTF-8)

    4>session:是否jsp在翻译时自动创建session

    5>import:导入java的包

    6>errorPage:当前页面出错后跳转到哪个页面

    7>isErrorPage:当前页面是一个处理错误的页面

     

    重点实例

    1>import

    在jsp中需要new一个fiel  File file=new File("");则需要导入io.file 方法如下

    格式:<%@page import="java.io.File"%>

    2>errorPage

    如果当前页面发生错误时跳转到指定的页面 

    格式:<%@ page language="java" contentType="text/html; charset=UTF-8"  errorPage="/errorPage.jsp" pageEncoding="UTF-8"%>

     

     8>include指令

    页面包含(静态包含)指令:在当前jsp页面插入另一个jsp页面

    格式:<%@ include file="被包含的文件地址"%>

     <%@ include file="/Demo.jsp"%>

     9.JSP的9个内置对象

     什么是内置对象:

    jsp被翻译成servlet之后,service方法中有9个对象定义并初始化完毕,我们在jsp脚本中可以直接使用这9个对象

    名称

    类型

    描述

    out

    javax.servlet.jsp.JspWriter

    用于页面输出

    request

    javax.servlet.http.HttpServletRequest

    得到用户请求信息,

    response

    javax.servlet.http.HttpServletResponse

    服务器向客户端的回应信息

    config

    javax.servlet.ServletConfig

    服务器配置,可以取得初始化参数

    session

    javax.servlet.http.HttpSession

    用来保存用户的信息

    application

    javax.servlet.ServletContext

    所有用户的共享信息

    page

    java.lang.Object

    指当前页面转换后的Servlet类的实例

    pageContext

    javax.servlet.jsp.PageContext

    JSP的页面容器

    exception

    java.lang.Throwable

    表示JSP页面所发生的异常,在错误页中才起作用

    10.JSP中常用的内置对象 out和pageContext

    1>out对象

    out的类型:JspWriter

    out作用就是想客户端输出内容----out.write()

    out缓冲区默认8kb 可以设置成0 代表关闭out缓冲区 内容直接写到respons缓冲器

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" buffer="0kb"%>

    2>pageContext对象(jsp页面的上下文对象)

    (1)pageContext对象是一个域对象必有得方法有:

    setAttribute(String name,Object obj)

    getAttribute(String name)

    removeAttrbute(String name)

    (2)pageContext还可以向指定的其他域中存取数据

    setAttribute(String name,Object obj,int scope)

    getAttribute(String name,int scope)

    removeAttrbute(String name,int scope)

      示例:

            request.setAttribute("name", "1");
            pageContext.setAttribute("name", "2");
            //使用pageContext向request域中存储数据
            pageContext.setAttribute("name", "3", PageContext.REQUEST_SCOPE);
            //使用pageContext向 PageContext域中存储数据
            pageContext.setAttribute("name", "4", PageContext.SESSION_SCOPE);
          //使用pageContext向PageContext域中存储数据
            pageContext.setAttribute("name", "5", PageContext.APPLICATION_SCOPE);

    完整代码:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8" buffer="8kb"%>
    <!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>
        <%
            //使用pageContext向request域中存储数据
            request.setAttribute("name", "1");
            pageContext.setAttribute("name", "2");
            pageContext.setAttribute("name", "3", PageContext.REQUEST_SCOPE);
            pageContext.setAttribute("name", "4", PageContext.SESSION_SCOPE);
            pageContext.setAttribute("name", "5", PageContext.APPLICATION_SCOPE);
        %>
        <%=request.getAttribute("name")%>
        <%=pageContext.getAttribute("name",PageContext.REQUEST_SCOPE)%>
        <!--findAttribute会按照域的作用范围  的从小到大找  -->
        <!-- 域的作用范围   pageContext<request<session<ServletContext也叫application/-->
        <%=pageContext.findAttribute("name")%>
    
    </body>
    </html>

    11.findAttribute(String name)查找名字为"name"的域

    查找范围:所有的域 

    查找域顺序:从小到大

                        pageContext<session<request<application(ServletContext)

    四个域的作用范围:

                       page域:当前jsp页面范围

                       request域:一次请求

                    session域:一次会话

                    application域:整个web应用

    12.JSP页面包含的动态和静态标签

    1>页面包含(静态包含):<!@ include file="被包含的页面"/>

    2>页面包含(动态包含):<jsp:include page="被包含的页面"/>

    相同点:在一个页面中包含另一个页面

    不同点:静态包含:会直接合并include_1.jsp和include_2.jsp的页面代码并凑到一起在翻译成include_005f1_jsp.java后编译成include_005f1_jsp.class

                只会生成1个.java和1个.class

                动态包含:会分别生成include1_jsp.java include1_jsp.class 和include2_jsp.java include2_jsp.class 在需要include1_jsp.java编译运行时找到include2_jsp.java

    完整代码:

     include1.jsp

    <%@ 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>this is include_1 page</h1>
    <%@ include file="include_2.jsp" %>
    
    </body>
    </html>

     include2.jsp

    <%@ 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>this is include_2 page</h1>
    </body>
    </html>

    include_1

    <%@ 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>this is include1 page</h1>
        <!-- 在当前页面种包含另一个页面 -->
        <jsp:include page="/include2.jsp"></jsp:include>
    </html>

    include_2

    <%@ 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>this is include2 page</h1>
    </body>
    </html>

     

     

  • 相关阅读:
    [译]Chapter 3 Understanding Controllers
    Effiective C# Item1 : 使用属性代替成员变量
    终于出版了
    《Thinking in UML》读书笔记之一
    【译】ClickOnce部署概述
    [译]Chapter 2 Building a Simple ASP.NET MVC Application
    开始翻译《Application Architecture Guide 2.0》
    [译]Chapter 1 An Introduction to ASP.NET MVC(1)
    [译]Chapter 1 An Introduction to ASP.NET MVC(3)
    Effective C# Item4:使用Conditional特性代替#if条件编译
  • 原文地址:https://www.cnblogs.com/asndxj/p/9893041.html
Copyright © 2020-2023  润新知