Home  >  Article  >  类库下载  >  Summary of JSP Basics of JavaWeb

Summary of JSP Basics of JavaWeb

高洛峰
高洛峰Original
2016-10-11 16:36:411464browse

The following code demonstrates an example of the JavaFX process command line parameters. You can refer to it.

1. Overview

 A JSP file is a file formed by combining three major instructions, nine major objects and JSP expressions with HTML statements. Its essence is a Servlet. To put it simply, it is Java code + HTML statement.

2. Operating principle

When each JSP page is accessed, it is translated into a Servlet source program by the JSP engine, and the source program is then compiled into a Servlet class file. The subsequent execution process is consistent with the execution of ordinary Servlet. The Servlet engine loads class files and translates them for execution.

3. Nine objects

  JSP has 9 built-in objects, which can be used without declaration.

  (1) request: actually HTTPServletRequest.

  (2) response: actually HTTPServletResponse.

  (3) pageContext: represents the current JSP page, contains almost all the information of the current JSP page, and eight other objects can also be obtained from this object.

  (4) session: represents a session between the browser and the server, and is an object of HttpSession.

  (5) out: You can directly output text information or HTML code on the page through the method of this object.

  (6) application: represents the current WEB application and is a ServletContext object.

  (7) exception: After declaring , exception information can be output through this object.

  (8) config: The ServletConfig object of the Servlet corresponding to the current JSP. If you want to access the configuration parameters of the current JSP, you need to map the access.

  (9) page: A reference to the Servlet object corresponding to the current Jsp, but of object type.

  The four most commonly used and important objects are as follows according to their scope:

   pageContext: The scope is the current JSP page. Variables in files introduced through can also be accessed on the current page. However, the variables and parameters introduced into the file through pageContext.include("file") cannot be directly used and modified on the current page. The essence of this method is to add the contents of the file to the current page intact, which can be understood as adding HTML The page is spliced ​​directly at this location.

  request: scope is always valid during the request forwarding process.

  Session: The scope is a session between the browser and the server. This is intuitively reflected in the opening and closing of the browser. Application cases include the implementation of shopping carts in shopping websites.

  Application: Valid when the current application is loaded in the server. The application will be uninstalled or reloaded from the server, and the server will be shut down, etc., which will cause the object to become invalid. Application examples include website statistics on the number of visitors.

  Methods related to attributes:

  Object getAttribute (String name) Gets the specified attribute

  Enumerration getAttributeNames () Gets the Enumeration object consisting of all attributes

name, Object o) Set attributes

4. Three major instructions

  (1) page: declare page information or import content

  ① The jar package required to import the file. Externally added jar packages are generally stored under the Web project WEB-INFlib,

<%@ page import="java.util.*"%>

  ② Close the session, and the session is open by default.

<%@ pagesession="false" %>

  ③ If there is an error on the current page, jump to the "file" page. And the jump method is request forwarding.

<%@ page errorPage="file"%>

  ④ The "file" page above can handle or explain page errors and can be used with the exception object.

<%@ page isErrorPage="true"%>

  ⑤ The character encoding of the current JSP page (JSP file encoding)

<%@ page pageEncoding="utf-8"%>

  ⑥ The response type of the current JSP page (the content encoding sent by the server to the browser)

<%@ page contentType="text/html; charset=utf-8"%>

  ⑦ Whether to use EL expression, usually false

<%@ page isELInored="false" %>

  ⑧ To specify the information of the JSP page, you can use the getServletInfo() method to obtain the string

<%@page info="this JSP info" %>

   (2) include instruction: include other files and merge them with the current page. Such files usually include text, JSP or other format files.

  ① Static introduction: Notify the JSP engine to merge other source files into the Servlet source file converted from the current JSP page when translating the JSP page. The two source files are merged into one Servlet source file. "file" is a relative path.

<%@ include page="file"%>

    ②动态引入:该句被执行时程序通过请求转发跳转到"file",执行当中的内容之后再返回执行当前页面的剩余部分,实际是两个Servlet源文件

<jsp:include page="file"></jsp:include>

  (3) taglib: 能够让用户根据需求自定义新的标签,便于功能的实现,但因此也导致了代码易读性不高。

5.其它

  (1) JSP表达式:可以直接将表达式中的内容显示在浏览器中

<%= "Hello World!" %>

  (2) 声明函数

<%!
     pulic void function(){
      out.println("Hello World!");
    }%>

  (3)注释

 <!-- 客户端注释,客户端查看源可以看到 -->
  <%-- 服务端注释,客户端查看不到 --%>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn