Java Server Page (JSP)

Java Server Page (JSP)

Java Server Page (JSP) is a technology for controlling the content or appearance of Web pages through the use of servlets, small programs that are specified in the Web page and run on the Web server to modify the Web page before it is sent to the user who requested it.

Sun Microsystems, the developer of java also refers to the JSP technology as the Servlet application program interface (API). JSP is comparable to Microsoft's Active Server Page (ASP) technology.

Whereas a Java Server Page calls a Java program that is executed by the Web server, an Active Server Page contains a script that is interpreted by a script interpreter (such as vscripts or J script ) before the page is sent to the user.

jsp2.JPG

Why Use JSP?

JavaServer Pages often serve the same purpose as programs implemented using the Common Gateway Interface (CGI). But JSP offers several advantages in comparison with the CGI.

  • Performance is significantly better because JSP allows embedding Dynamic Elements in HTML Pages itself instead of having separate CGI files.

  • JSP are always compiled before they are processed by the server unlike CGI/Perl which requires the server to load an interpreter and the target script each time the page is requested.

  • JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP also has access to all the powerful Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP, etc.

  • JSP pages can be used in combination with servlets that handle the business logic, the model supported by Java servlet template engines.

jsp.jpg

Advantages of using JSP

  • It does not require advanced knowledge of JAVA
  • It is capable of handling exceptions
  • Easy to use and learn
  • It can tags which are easy to use and understand
  • Implicit objects are there which reduces the length of code
  • It is suitable for both JAVA and non JAVA programmer

Life Cycle

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.

Paths Followed By JSP

The following are the paths followed by a JSP :

  • Compilation
  • Initialization
  • Execution
  • Cleanup

jsp1.JPG

Jsp Implicit Objects

These objects are created by JSP Engine during translation phase (while translating JSP to Servlet). They are being created inside service method so we can directly use them within Scriptlet without initializing and declaring them. There are total 9 implicit objects available in JSP.

  • Out: This is used for writing content to the client (browser). It has several methods which can be used for properly formatting output message to the browser and for dealing with the buffer. Read full article here » OUT implicit object with examples.
  • Request: The main purpose of request implicit object is to get the data on a JSP page which has been entered by user on the previous JSP page. While dealing with login and signup forms in JSP we often prompts user to fill in those details, this object is then used to get those entered details on an another JSP page (action page) for validation and other purposes. Read full article here » Request implicit object with examples.
  • Response: It is basically used for modfying or delaing with the response which is being sent to the client(browser) after processing the request. Read full article here » Response implicit object with examples.
  • Session: It is most frequently used implicit object, which is used for storing the user’s data to make it available on other JSP pages till the user session is active. Read full article here » Session implicit object with examples.
  • Application: This is used for getting application-wide initialization parameters and to maintain useful data across whole JSP application. Read full article here » Application implicit object with examples.
  • Exception: Exception implicit object is used in exception handling for displaying the error messages. This object is only available to the JSP pages, which has isErrorPage set to true. Read full article here » Exception implicit object with examples.
  • Page: Page implicit object is a reference to the current Servlet instance (Converted Servlet, generated during translation phase from a JSP page). We can simply use this in place of it. I’m not covering it in detail as it is rarely used and not a useful implicit object while building a JSP application. pageContext: It is used for accessing page, request, application and session attributes. Read full article here » pageContext implicit object with examples.
  • Config: This is a Servlet configuration object and mainly used for accessing getting configuration information such as servlet context, servlet name, configuration parameters etc.

Did you find this article valuable?

Support Kushagra Sharma by becoming a sponsor. Any amount is appreciated!