What Is JSP?

JSP (full name Java Server Ages) is a dynamic web technology standard created by Sun Microsystems. JSP is deployed on a web server and can respond to requests sent by clients, dynamically generate Web pages of HTML, XML, or other format documents based on the content of the request, and then return to the requester. JSP technology uses the Java language as a scripting language to provide services for users' HTTP requests, and can work with other Java programs on the server to handle complex business requirements together.

  • A simple and efficient way to add dynamic web content in a templated manner.
  • available
    Version 1.2
    JSP 1.2 does not support EL, but you can use external JSTL tags to use EL. [1]
    For quick and easy
    Built-in objects
    request
    The request object is an object of type javax.servlet.httpServletRequest. This object represents the request information of the client and is mainly used to accept data transmitted to the server through the HTTP protocol. (Including header information, system information, request method, and request parameters, etc.). The scope of the request object is a request.
    response
    response represents the response to the client, mainly to return the object processed by the JSP container back to the client. The response object also has a scope, which is only valid within a JSP page.
    session
    1. What is a session: It starts when a client opens a browser and connects to the server, and ends when the client closes the browser and leaves the server. This is called a session. When a client accesses a server, it may repeatedly connect between several pages of this server and repeatedly refresh a page. The server should know in some way that this is the same client, which requires a session object.
    2. Session object ID: When a client accesses a JSP page on the server for the first time, the JSP engine generates a session object and assigns a String ID number at the same time. The JSP engine also sends this ID number to the client and stores it in a cookie. In this way, a one-to-one correspondence is established between the session object and the client. When the client visits other pages connected to the server, the client will not be assigned a new session object. Until the client closes the browser, the client's session object on the server side is cancelled and the corresponding relationship with the client disappears. When the client reopens the browser and connects to the server, the server creates a new session object for the client.
    3.The session object has a certain period of time expiration, so the name-value pairs in the session will be lost after a certain period of time. You can avoid this situation by changing the session effective time. At the same time, try to avoid storing a lot of valid information in the session when programming, request is a good substitute object.
    application
    1. What is application:
    This application object is generated after the server is started. When the client browses between the pages of the visited website, the application object is the same until the server is closed. But unlike the session, all clients' application objects are the same, that is, all clients share this built-in application object.
    2. Application object common methods:
    (1) public void setAttribute (String key, Object obj): Add the object obj specified by the parameter Object to the application object, and specify an index key for the added object.
    (2) public Object getAttribute (String key): Gets the object containing the keyword in the application object.
    out
    The out object is used to output information in the web browser and manage the output buffer on the application server. When using the out object to output data, you can operate the data buffer to clear the residual data in the buffer in time to make buffer space for other outputs. After the data is output, close the output stream in time.
    page
    The page object represents the JSP itself, and is only valid within a JSP page. The page implicit object essentially contains the variables referenced by the current Servlet interface, similar to the this pointer in Java programming.
    config
    The main role of the config object is to get the configuration information of the server. You can get a config object through the getServletConfig () method of the pageContext object. When a servlet is initialized, the container passes certain information to the servlet through the config object. Developers can provide initialization parameters in the web.xml file for servlet programs and JSP pages in the application environment.
    exception
    An instance of java.lang.Throwable that represents exceptions and errors in other pages. This object can only be used when the page is an error handling page, that is, the isErrorPage property of the pragma page is true. Common methods are getMessage () and printStackTrace ().
    pageContext
    The role of the pageContext object is to get any range of parameters, through which you can get out, request, response, session, application and other objects of the JSP page. The creation and initialization of the pageContext object is done by the container. You can use the pageContext object directly in the JSP page.
    Difference from servlets
    Architecturally, JSP can be regarded as an advanced abstraction from Servlets and applied as an extension of JAVA Servlet 2.1 API. Servlets and JSPs were first developed by Sun Microsystems. Since the JSP 1.2 version, JSP has been under the Java Community Process (Some people translated: JAVA community organization) development mode. JSR-53 specifies the specifications of JSP 1.2 and Servlet 2.4, and JSR-152 specifies the specifications of JSP 2.0. In May 2006, the JSP 2.1 specification was released in JSR-245 as part of Java EE5.
    Comparison with ASP
    Both JSP (Java Server Page) and ASP (Active Server Page) are commonly used dynamic webpage technologies, and both are programs that can be embedded in HTML. However, the two are essentially different. Compare:
    1. Web server support : Most common web servers such as Apache, Netscape, and Microsoft IIS support JSP pages. Only Microsoft's own Microsoft IIS and Personal Web Server can support ASP.
    2. Platform support : JSP has platform independence. As long as it is a platform that ordinary Java programs can run, JSP programs are supported. Windows platform can well support ASP, but ASP's dependence on the Win32-based progressive model makes it difficult to port to other platforms.
    3. Component model : JSP is built on reusable, cross-platform components (such as: JavaBeans, Enterprises JavaBeans, and user-defined tag libraries and other components), while ASP uses a Win32-based COM component model.
    4. Scripting language : JSP can use Java programming language or JavaScript as the scripting language, while ASP uses VBScript or Jscript as the scripting language.
    5. Security : JSP uses the Java security model, while ASP uses the Windows NT security structure.
    6. Connection with Access database : JSP uses JDBC to establish a connection with Access database, while ASP uses Data Active Objects for Access database.
    7. User-defined tags : JSP can be extended with user-defined tag libraries. ASP does not have user-defined tag libraries. ASP cannot be extended.
    According to the functions provided by JSTL tags, they can be divided into 5 categories.

    JSTL JSP JSTL library installation

    The steps to install the JSTL library in Apache Tomcat are as follows:
    • A binary package (jakarta-taglibs-standard-current.zip) downloaded from Apache's standard tag library. Download address: http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/
    • Download the jakarta-taglibs-standard-1.1.1.zip package and decompress it. Copy the two jar files under jakarta-taglibs-standard-1.1.1 / lib /: standard.jar and jstl.jar files to / WEB-INF / lib /.
    To use any library, you must include the <taglib> tag in the header of each JSP file.

    JSP core tags

    Core tags are the most commonly used JSTL tags. The syntax for referencing the core tag library is as follows:
    <% @ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"%>
    label description
    <c: out> Used to display data in JSP, like <% = ...>
    <c: set> Used to save data
    <c: remove> Used to delete data
    <c: catch> Used to handle abnormal conditions that generate errors, and store error information
    <c: if> Same as if we use in normal programs
    <c: choose> Itself only as a parent tag of <c: when> and <c: otherwise>
    <c: when> <c: choose> subtag, used to determine whether the condition is true
    <c: otherwise> The child tag of <c: choose> is executed after the <c: when> tag and is executed when the <c: when> tag is judged to be false
    <c: import> Retrieve an absolute or relative URL and then expose its content to the page
    <c: forEach> Basic iteration tags, accepting multiple collection types
    <c: forTokens> Separate content based on specified delimiter and iterate output
    <c: param> Used to pass parameters to included or redirected pages
    <c: redirect> Redirect to a new URL.
    <c: url> Create URL with optional query parameters

    JSP formatting tags

    JSTL formatting tags are used to format and output text, dates, times, and numbers. The syntax for referencing a formatted tag library is as follows:
    <% @ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt"%>
    label description
    <fmt: formatNumber> Format numbers using the specified format or precision
    <fmt: parseNumber> Parse a string representing a number, currency or percentage
    <fmt: formatDate> Format the date and time using the specified style or pattern
    <fmt: parseDate> Parse a string representing a date or time
    <fmt: bundle> Bind resources
    <fmt: setLocale> Designated area
    <fmt: setBundle> Bind resources
    <fmt: timeZone> Specify time zone
    <fmt: setTimeZone> Specify time zone
    <fmt: message> Display resource profile information
    <fmt: requestEncoding> Set request character encoding

    SQL JSP SQL tags

    The JSTL SQL Tag Library provides tags that interact with relational databases (Oracle, MySQL, SQL Server, etc.). The syntax for referencing the SQL tag library is as follows:
    <% @ taglib prefix = "sql" uri = "http://java.sun.com/jsp/jstl/sql"%>
    label description
    <sql: setDataSource> Specifying the data source
    <sql: query> Run SQL query
    <sql: update> Run SQL update statement
    <sql: param> Set the parameters in the SQL statement to the specified value
    <sql: dateParam> Set the date parameter in the SQL statement to the specified java.util.Date object value
    <sql: transaction> Provide nested database behavior elements in shared database connections, and run all statements as a transaction

    XML JSP XML tags

    The JSTL XML tag library provides tags for creating and manipulating XML documents. The syntax for referencing XML tag libraries is as follows:
    <% @ taglib prefix = "x" uri = "http://java.sun.com/jsp/jstl/xml"%>
    Before using the xml tag, you must copy the XML and XPath related packages to your <Tomcat installation directory> \ lib:
    • XercesImpl.jar:
    • xalan.jar:
    label description
    <x: out> Similar to <% = ...>, but only for XPath expressions
    <x: parse> Parsing XML data
    <x: set> Set XPath expression
    <x: if> Judge the XPath expression, if it is true, execute the content in the ontology, otherwise skip the ontology
    <x: forEach> Iterating over nodes in an XML document
    <x: choose> Parent tags of <x: when> and <x: otherwise>
    <x: when> <x: choose> subtag for conditional judgment
    <x: otherwise> <x: choose> child tag, executed when <x: when> is judged to be false
    <x: transform> Applying XSL transformations to XML documents
    <x: param> Used with <x: transform> to set XSL style sheets

      JSTL JSP JSTL functions

      JSTL contains a series of standard functions, most of which are general-purpose string processing functions. The syntax for referencing the JSTL function library is as follows:
      <% @ taglib prefix = "fn" uri = "http://java.sun.com/jsp/jstl/functions"%>
      function description
      fn: contains () Tests if the input string contains the specified substring
      fn: containsIgnoreCase () Tests whether the input string contains the specified substring and is not case sensitive
      fn: endsWith () Tests if the input string ends with the specified suffix
      fn: escapeXml () Skip characters that can be used as XML tags
      fn: indexOf () Returns the position of the specified string in the input string
      fn: join () Combine the elements of the array into a string and output
      fn: length () Returns the length of the string
      fn: replace () Replaces the specified position in the input string with the specified string and returns
      fn: split () Separate the strings with the specified delimiter and form an array of substrings and return
      fn: startsWith () Tests if the input string starts with the specified prefix
      fn: substring () Returns a subset of a string
      fn: substringAfter () Returns a subset of the string after the specified substring
      fn: substringBefore () Returns a subset of the string before the specified substring
      fn: toLowerCase () Convert characters in a string to lower case
      fn: toUpperCase () Convert characters in a string to uppercase
      fn: trim () Remove leading whitespace

      IN OTHER LANGUAGES

    Was this article helpful? Thanks for the feedback Thanks for the feedback

    How can we help? How can we help?