博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JSP !
阅读量:5129 次
发布时间:2019-06-13

本文共 4222 字,大约阅读时间需要 14 分钟。

JSP(Java Server Page) which display the page of server also a servlet in essence. The JSP could write java code in a html page.

JSP = Java + HTML

In the MVC:

  Servlet: controller

  Jsp: viwer

We can see the jsp file in work directory in tomcat's root path. This directory is used to restore the jsp java file an jsp class file. From the source code, we can see from

the jsp's java file that class extends from org.apache.jasper.runtime.HttpJspBase. The HttpJspBase also extends the HttpServlet. So the jsp is a servlet in essence.

How to write java code in jsp?

  <%!    %>  define the member variables  usually, we define method in it. If define the member field in it, it may lead to thread security problems.

  <%     %>  define the local variables    the variables are in local position, at the _jspservice()

  <%=   %>  output variables        call the out.print("content")

The comment of jsp:

  <%-- comment --%>

  We recommend the use of this style in jsp, it will improve the effeciency of jsp.

The instruction of jsp:

  page  taglib  include

The format of instruction:

  <%@ instruction attribute1="value1" attribute2="value2" ... %>

 

Page:

  import: import the jar of class jsp needs.  <%@page import="java.util.Date" %>

  session: has the session object or not, the default value is true.

  buffer: when write to the html page, the out stream is JspWriter, this attribute is to set the capacity of the buffer, the default value is 8kb.

  errorPage: set the page location when there is a error in the jsp, we can define the error page in the web.xml:

        <error-page>

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

          <location>/404.jsp</location>

        </error-page>

  isErrorPage: has the exception inner-object in the jsp, the default value is false.

  contentType: has the same funciont as the response.setContentType("text/html;charset=utf-8");

  pageEncoding: set the encoding of the server when the server translate the jsp code.

 

Taglib: import external tag lib, when we use the JSTL.

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

 

Include: include some resource in the static way.

  static: <%@include file="/8.jsp">

  

   dynamic: <jsp:include page="/8.jsp"></jsp:include>

  We should use the static way as most as possible. 

 

The action of jsp:

  1.  <jsp:include page="/8.jsp"> include the resource of 8.jsp in the dynamic way.

  2. <jsp:forward page="/8.jsp"> forward to the specific resource and the url location will not change in the browser.

  3. <jsp:param value="value" name="name"> forward the parameter.

 

There are 9 inner objects in jsp:

  request/response/config/application/out/exception/session/pageContext/page

 

 The detail of pageContext inner object:

  1. It's a domain object, and it can manipulate the data in other three domain object (ServletRequest, HttpSession, ServletContext):

    void setAttribute(String name, Object value);

    void removeAttribute(String name);

    Object getAttribute(String name);

    pageContext's scope is limited in the page, only works in the page.

    How to handle the data of other three domain object?

      void setAttribute(String name, Object value, int scope);

      void getAttribute(String name, int scope);

      void removeAttribute(String name, int scope);

        the value of scope:

          1. PageContext.PAGE_SCOPE

          2. PageContext.REQUEST_SCOPE

            3. PageContext.SESSION_SCOPE

          4. PageContext.APPLICATION_SCOPE

    the code:

      pageContext.setAttribute("name","value",PageContext.APPLICATION_SCOPE);

      equals:

      application.setAttribute("name","value");

    Object findAttribute(String key): find the object from page,request,session,application. Return the minimum range object.

  2. Get other domain object:

    ServletRequest requst = pageContext.getRequest();

    From this feature, if we want to get other domain object in a method, we can :

      public void method(){

        ServletRequest request = pageContext.getRequest();

        HttpSession session = pageContext.getSession();

        JspWriter writer = pageContext.getOut();

      }

The 4 important domain object:

  PageContext, ServletRequest, HttpSession, ServletContext

 

转载于:https://www.cnblogs.com/ppcoder/p/7242166.html

你可能感兴趣的文章
图论例题1——NOIP2015信息传递
查看>>
CocoaPods的安装和使用那些事(Xcode 7.2,iOS 9.2,Swift)
查看>>
Android 官方新手指导教程
查看>>
幸运转盘v1.0 【附视频】我的Android原创处女作,请支持!
查看>>
UseIIS
查看>>
vi命令提示:Terminal too wide
查看>>
引用 移植Linux到s3c2410上
查看>>
MySQL5.7开多实例指导
查看>>
[51nod] 1199 Money out of Thin Air #线段树+DFS序
查看>>
Red and Black(poj-1979)
查看>>
安装 Express
查看>>
包含列的索引:SQL Server索引的阶梯级别5
查看>>
myeclipse插件安装
查看>>
浙江省第十二届省赛 Beauty of Array(思维题)
查看>>
NOIP2013 提高组 Day1
查看>>
cocos2dx 3.x simpleAudioEngine 长音效被众多短音效打断问题
查看>>
存储(硬件方面的一些基本术语)
查看>>
观察者模式
查看>>
Weka中数据挖掘与机器学习系列之基本概念(三)
查看>>
Win磁盘MBR转换为GUID
查看>>