1 of 13

Introduction to JSP

JSP- Java Server Pages

2 of 13

Introduction

  • Java Server Pages (JSP) is a server side scripting language, it is used for developing web pages that support dynamic content which helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with<% and end with >% .
  • A Java Server Pages component is a type of Java servlet that is designed to fulfill the role of a user interface for a Java web application.
  • Web developers write JSPs as text files that combine HTML code, XML elements, and embedded JSP actions and commands.
  • Using JSP, you can collect input from users through web page forms, present records from a database or another source, and create web pages dynamically.
  • JSP tags can be used for a variety of purposes, such as retrieving information from a database or registering user preferences, accessing JavaBeans components, passing control between pages and sharing information between requests, pages etc.

3 of 13

  • A JSP page consists of HTML tags and JSP tags. The JSP pages are easier to maintain than Servlet because we can separate designing and development.
  • It is a Web based technology helps us to create dynamic and platform independent web pages.
  • In this, Java code can be inserted in HTML/ XML pages or both.
  • JSP is first converted into servlet by JSP container before processing the client’s request.
  • They are easy to maintain.
  • No recompilation or redeployment is required.
  • JSP has access to entire API of JAVA .
  • JSP are extended version of Servlet.

4 of 13

Advantages of JSP over Servlet�

1)Extension to Servlet: JSP technology is the extension to Servlet technology. We can use all the features of the Servlet in JSP. In addition to, we can use implicit objects, predefined tags, expression language and Custom tags in JSP, that makes JSP development easy.

2) Easy to maintain: JSP can be easily managed because we can easily separate our business logic with presentation logic. In Servlet technology, we mix our business logic with the presentation logic.

3) Fast Development: No need to recompile and redeploy, If JSP page is modified, we don't need to recompile and redeploy the project. The Servlet code needs to be updated and recompiled if we have to change the look and feel of the application.

4) Less code than Servlet: In JSP, we can use many tags such as action tags, JSTL, custom tags, etc. that reduces the code. Moreover, we can use EL, implicit objects, etc.

5 of 13

JSP and PHP both are server side scripting languages. But here is a list of reasons why JSP is better than PHP

  • Anything you can do with PHP, you can do with JSP; the reverse is not true.
  • JSP is much more powerful, since it has access to all the Java libraries. PHP only has access to PHP libraries.
  • JSP is Object-Oriented, so leads to cleaner code that's easier to debug, maintain, and improve. (PHP also allows objects, but the object model is more primitive, and most scripted pages ignore PHP objects and just use normal variables.
  • With JSP, if the code inside a page gets too big, or if you want to use it elsewhere, you can cut it out, make it into a Java class, and invoke it from anywhere in your application (even not from a page). With PHP, you're stuck inside the HTML box.

6 of 13

JSP Architecture

7 of 13

->Web client sends request to Web server.

-> As per the request, the Web server (here after called as JSP container) loads the page.

->JSP container converts JSP file into .java file having servlet code-Translation.

.java file is converted into .class file- compilation.

->The .class file of Servlet is executed and output of execution is sent to the client as response.

8 of 13

Life cycle of JSP

  • A JSP life cycle is defined as the process from its creation till the destruction. This is similar to a servlet life cycle with an additional step which is required to compile a JSP into servlet.

9 of 13

  • Translation of JSP page to Servlet
  • Compilation of JSP page(Compilation of JSP into test.java)
  • Classloading (test.java to test.class)
  • Instantiation(Object of the generated Servlet is created)
  • Initialization(jspInit() method is invoked by the container)
  • Request processing(_jspService()is invoked by the container)
  • JSP Cleanup (jspDestroy() method is invoked by the container)

10 of 13

JSP Initialization

  • When a container loads a JSP it invokes the jspInit() method before servicing any requests. If you need to perform JSP-specific initialization, override the jspInit() method −

public void jspInit()

{

// Initialization code...

}

Typically, initialization is performed only once and as with the servlet init method, you generally initialize database connections, open files, and create lookup tables in the jspInit method.

11 of 13

JSP Execution

  • This phase of the JSP life cycle represents all interactions with requests until the JSP is destroyed.
  • Whenever a browser requests a JSP and the page has been loaded and initialized, the JSP engine invokes the _jspService() method in the JSP.
  • The _jspService() method takes an HttpServletRequest and an HttpServletResponse as its parameters as follows −

void _jspService(HttpServletRequest request, HttpServletResponse response)

{

// Service handling code...

}

The _jspService() method of a JSP is invoked on request basis. This is responsible for generating the response for that request and this method is also responsible for generating responses to all seven of the HTTP methods, i.e, GET, POST, DELETE, etc.

12 of 13

JSP Cleanup

  • The destruction phase of the JSP life cycle represents when a JSP is being removed from use by a container.
  • The jspDestroy() method is the JSP equivalent of the destroy method for servlets. Override jspDestroy when you need to perform any cleanup, such as releasing database connections or closing open files.
  • The jspDestroy() method has the following form −

public void jspDestroy()

{

// Your cleanup code goes here.

}

13 of 13