Open In App

What is JSP Page?

Last Updated : 16 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A JSP page is a normal web page with JSP elements for generating the parts of the web page that differ for each request. A simple JSP web page that contains the JSP elements and template text. Everything on the page that is not a JSP element is called Template text. Template text can be any text, i.e., HTML, XML, WML or even plain text.JSP has no dependency on HTML, it can be used with any markup language. Template text is always passed straight to the browser.

JSP Elements

There are three types of JSP elements present:    

  1. Directive
  2. Action
  3. Scripting

New Components were added in JSP 2.0 as an Expression Language(EL) and let’s call this a fourth element type, even though it’s a bit different than the other three.

1. Directive Elements

page:   <%@ page … %>

  • Defines page-dependent attributes, such as session tracking, error page, and buffering requirements.
  • is used for importing a package.
  • is used for Handling an Exception.

Attributes that we can use are:  

  • language=”Scripting language”   : is used to set which type of language it is.
  • import=”import List”  : is used to import the packages,and so on…
  • session=”true/false”
  • contentType=”ctinfo”
  • errorPage=”error-url”
  • isErrorPage=”true/false”
  • info=”information”
  • isELIgnored=”true/false”
  • isThreadSafe=”true/false”

In all the above “import” attribute can be used many times.

Example:

<%@ page language="java" import="java.util.*,java.sql.*" info="Contact page"
           extends="com.rajkumar.User" contextType="text/html" isELIgnored="false"
       isThreadSafe="false" session="false" %>

include: <%@ include … %>

  • Includes a file during the translation phase
  • for using other JSP pages in the current JSP page

Example:

<%@ include file="filename" %>
<%@ include file="header.jsp" %>

taglib:  <%@ taglib … %>

Declares a tag library, containing custom actions, that is used on the page for using Spring Frameworks

Template Text

Template data refers to the static HTML or XML content of the JSP.  Aside from the usual substitutions, such as those based on quoting and escape sequences, the template data is written verbatim as part of the JSP response.

2. Action Tag/Element

JSP Action tags or Elements are used to perform some specific tasks. The action tags are used to control the flow between pages and to use Java Beans. There are many JSP action tags.

  • jsp:forward: forwards the request and response to another resource.
  • jsp:include: includes another resource.
  • jsp:useBean: creates or locates bean object.
  • jsp:setProperty: sets the value of property in bean object.
  • jsp:getProperty: prints the value of the property of the bean.
  • jsp:plugin: embeds another component such as applet.
  • jsp:param: sets the parameter value. It is used in forward and includes mostly.
  • jsp:fallback: can be used to print the message if the plugin is working. It is used in jsp:plugin.

3. Scripting Element

Scriptlet elements must be written within the <% … %> tags.The Scriptlet tag allows writing Java code statements within the JSP page.The tag is responsible for  implementing the functionality of “_jspService()” by scripting the java code.The JSP engine will process any code written within the pair of <%  and %> tags and any other code is treated as plain text while translating the JSP page. There are three main subdivisions of Scripting Elements in Java Server Pages.

  • Expression Tag
  • Scriptlet Tag 
  • Declaration Tag

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads