The basics of Java Server Pages(JSP) and its APIs


Do you want to know the basic concept of Java Server Page(JSP)? In this article I will explain about JSP and why it is useful? various tags, JSP life-cycle and advantages. I will also cover implicit objects and various JSP APIs

Introduction

Java 2 Enterprise Edition(J2EE) is an environment for developing and deploying enterprise applications. J2EE module is a software unit that consists of one or more J2EE components of the same container type. Most of the web application supports Model-View-Controller(MVC) architecture. For simple understanding, Servlet can be used as a Controller, JSP can be used as a View, and EJB can be used as a Model.
Here, we will focus only on Java Server Pages(JSP).

What is JSP?

Embedding of java code in HTML pages. It is more easier approach to create and maintain web application than Servlets. Servlet puts HTML code inside java code while JSP adds java code inside HTML. JSP can do everything that Servlet can, but JSP is more easier approach. Java Server Pages are the Java platform technology for delivering dynamic content to web clients in the portable, secure and well defined way. It is an extension to the servlet because it provides more functionality than servlet. A JSP page contains HTML code and JSP tag. JSP pages are easier to maintain than servlet because we can separate designing and development. It provides additional features such as Expression language, custom tags, implicit objects, predefined tags etc. Web container is responsible to convert JSP pages to Servlet.

jsp0

Advantages of JSP

  1. Easy to create and maintain
  2. Provides high performance and scalability
  3. It builds on Java platform so it is platform independent
  4. It can be easily managed because we can easily seperate our business logic with presentation logic. But in servlet, we mix our business logic with the presentation logic.
  5. If JSP page is modified, we do not need to redeploy the project. The servlet code needs to be updated and recompiled if we have a change the look and feel of the application.

Do I need to follow directory structure to run a simple JSP?

No, there is no need of directory structure if you do not have class file or tld file. For example, put jsp files in a folder directly and deploy that folder.

JSP implicit objects

  • request - HttpServletRequest object associated with it
  • response - HttpServletResponse object associated with it
  • page - 'This' object is equivalent to it
  • page context
  • config - ServletConfig object associated with it
  • out - JSPWriter object associated with it
  • session - HttpSession object associated with it
  • application - ServletContext object associated with it
  • exception - Throwable object associated with it

What are the uses of implicit objects in JSP?

  • Implicit objects let developers access container provide services and resources.
  • These are defined as implicit because you do not have to explicitly declare them.
  • They are defined in every JSP page and used behind the scenes by the container.
  • It can declared automatically, we need to use the reference variable associated with a given object.
  • These simplify JSP code and reduce the complexity of code by providing ready state objects. Example: request.getElementById("xyz");

Declaration in JSP

There are the JSP tag used to declare variables. Declarative are enclosed in the <%! %> tags and ends in semicolon.
Example:
<%@ page contentType = "text/html" %>

<%! int count = 0;
private int getCount() {
count ++;
return count;
} %>

Lifecycle of JSP

  1. Translation to Servlet code (somthing.jsp to something_jsp.java)
  2. Compilation to bytecode (something.java to something.class)
  3. Loading Servlet class
  4. Instantiate Servlet

  5. jspInit(): It is used to initialize and defined in javax.servlet.jsp.JspPage interface and you can override this method

  6. jspDestroy(): It is used for request processing and called from the servlet's destroy() method and you can override it too. It is defined in javax.servlet.jsp.JspPage interface

  7. _jspService(): It is used for destroying and called from servlet's service() method. The underscore in front of the method means you can not override it. Container passes request and response objects to this method. It is defined in javax.servlet.jsp.HttpJspPage interface

Expression in JSP

This tag is used to insert java values directly into the output.
Example:
// This is a expression.
Current time is <%= new java.util.Date() %>

Scriptlets in JSP

These are JSP tags which is used to enclose java code in the JSP pages. Scriptlet begins with <% tag and end with %> tag.
Example:
<%
// This is a scriptlet.
String userName = null;
userName = request.getParameter("userName");
%>

Directives in JSP

  1. Page directives: It is used for import statement and character coding.
    <%@ page import = "tom.*" session = "false" %>

  2. Taglib directives: It is used for custom tags.
    <%@ taglib tagdir = "/WEB-INF/tags/cool" prefix = "cool" %>

  3. Include directives: It is used for standard page heading or navigation bar.
    <%@ include file = "sample.html" %>

Attributes of JSP Page directives

  1. import
  2. isErrorPage
  3. errorPage
  4. isELIgnored
  5. isThreadSafe
  6. autoflush
  7. buffer
  8. contentType
  9. info
  10. pageEncoding
  11. language
  12. session
  13. extends

Action tags in JSP

JSP action tags are nothing but XML tags that direct the server to use existing components. These tags are given below:
  • jsp:include
  • jsp:forward
  • jsp:useBean
  • jsp:plugin
  • jsp:geProperty
  • jsp:setProperty
  • jsp:attribute
  • jsp:element
  • jsp:text
  • jsp:body
  • jsp:param

JSP API

    The JSP API consists of two packages,
  • javax.servlet.jsp
  • javax.servlet.jsp.tagext

javax.servlet.jsp package: It has 2 interfaces,
  • JSP page
  • HttpJSPPage

  • It has 6 classes,
  • JSPWriter
  • PageContext
  • JSPFactory
  • JSPEngineInfo
  • JSPException
  • JSPError

  • Related Articles

    Creating new JEE 5 applications (Java and EJB Web Services) using RAD 7.5

    Now days, most of the enterprise applications are developed in RAD 7.0+ (Rational Application Developer). The Web service application can be written in Java or in EJB. For creating Web services in Java or EJB, the vast documentation is available. Referring such documentation during speedy development period becomes a hectic job. So to avoid it, this article will help to easily develop a JEE5 application using simple steps.

    Program for Addition performer in Java

    This article provides a description to the addition performer in Java, that can perform addition operation on two input numbers. Code for the addition performer is provided in the article. The program is based on use of Swings. In Java Frames can be created and different text fields alongwith labels corresponding to them can be added. And use of all these elements can develope an efficient adder.

    What is difference between an Interface and Abstract class in Java?

    Are you looking for an article which explains the concepts of an Interface and Abstract class in simple language? Then your search ends here. Read this article, herein the detail about abstract class and an Interface is given. Also the important differences between an Interface and Abstract class is in Java is explained. This article highlights the important aspects regarding whether abstract class to be used or an Interface is to be used for given requirement of Enterprise application.

    Directory Structure - XML Output

    In this article, I have submitted a Java program. We will be able to get the dynamic XML file with treeview represents a folder and file structure of a given folder path using it. This Java program also show how to use a recursive function to read a directory structure and then save it to XML file. I commented properly for your better understanding how the program flow goes. The output XML file is well-formed

    More articles: Java

    Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: