0% found this document useful (0 votes)
4 views3 pages

Understanding JavaServer Pages (JSP)

Java Server pages notes

Uploaded by

Pankaj Kumawat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Understanding JavaServer Pages (JSP)

Java Server pages notes

Uploaded by

Pankaj Kumawat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

JSP (JavaServer Pages)

-----------------------
Introduction
------------
JSP is a technology used for creating dynamic web content. It simplifies the
creation of dynamic web pages by embedding Java code directly into HTML using
special tags.

JSP is nothing but a servlet in tag form.

We write the code in the form of tags.

JSP also translates into a servlet that extends the built-in class HttpJspBase.

HttpJspBase class extends HttpServlet class.

------------------------------------------------------
public class JspCalculator extends HttpJspBase {
}
---------------------------------------------------------

It has three lifecycle methods:

public void jspInit() {}: This method is used to initialize the JSP page. It is
typically used for one-time initializations like variable initialization and
setting up resources. We can override this method.

public void _jspService(HttpServletRequest request, HttpServletResponse response)


{}: This is the main method where the actual processing of the JSP page takes
place. Its implementation is automatically generated by the JSP container, so we
cannot override it. It is managed by the JSP container. Whenever there is a client
request, the _jspService method is executed.

public void jspDestroy() {}: This method is used to execute cleanup code or release
external resources before the JSP is destroyed.

Implicit Objects / Predefined Objects / Built-in Objects / Pre-initialized Objects


-----------------------------------------------------------------------------------
--
request - HttpServletRequest

response - HttpServletResponse

session - HttpSession

page - Object

pageContext - PageContext

application - ServletContext

out - JspWriter

config - ServletConfig

exception - Throwable
JSP Scopes
---------------
page: If data is stored in page scope, then that data is accessible only within the
same JSP page. When control goes to the next JSP page, that data will not be
available.

request: If data is kept in request scope, then it is accessible to all the


servlets and JSPs involved in handling the client request.

session: If data is kept in session scope, then it is accessible to all the


servlets and JSPs involved in handling the client request during that session.
Session scope is specific to the client.

application: Data kept in application scope is accessible to all the servlets and
JSPs of the application until the application is shut down.

Difference Between Servlet and JSP


------------------------------------

Purpose:

Servlet: Used to implement controller functionality.

JSP: Used for creating dynamic web pages.

Translation Phase:

Servlet: Does not have a translation phase.

JSP: Has a translation phase where the JSP is converted into a servlet.

Method Overriding:

Servlet: service() method can be overridden.

JSP: service() method cannot be overridden.

Implicit Objects:

Servlet: Does not have implicit objects.

JSP: Has implicit objects.

Registration:

Servlet: Registered in [Link].

JSP: Not registered in [Link].

JSP Scripting Elements


-------------------------
Declarations: Used to declare variables and methods. Syntax: <%! %>

Scriptlets: Used to include Java code that gets executed every time the page is
requested. Syntax: <% %>
Expressions: Used to output values to the client. Syntax: <%= %>

JSP Scripting Elements


-----------------------------
Declarations:
--------------
Used to define methods in a JSP page.

Used to declare instance variables.

Does not have access to implicit objects.

Can be used to override jspInit() and jspDestroy() methods.

Uses a semicolon as a statement terminator.

Scriptlets:
-------------
Used to declare local variables in a JSP page.

Has access to implicit objects.

Can access methods and variables declared in declaration tags.

Uses a semicolon as a statement terminator.

Its code is added to _jspService() during the JSP translation phase.

Expressions:
------------
Evaluates and displays the result of an expression.

Has access to implicit objects.

Can access variables and methods defined in declaration and scriptlet tags.

Does not use a semicolon.

Its code is added to _jspService() during the JSP translation phase.

JSP Standard Action tag


-------------------------

<jsp:forward>: This tag forwards the request to another resource (e.g., a JSP file,
servlet, or HTML file). It's like a page redirection that happens on the server
side.

<jsp:include>: This tag includes the content of another resource into the current
JSP page. It's useful for reusing code, like headers or footers.

<jsp:param>: This tag is used within <jsp:forward> and <jsp:include> to pass


parameters to the target resource.

You might also like