JSP in Java

JSP in Java. JSP represents Jakarta Server Pages (formerly Java Server Pages) given by Sun Microsystem in 1999 as server-side web technology to develop dynamic web pages.

One question that may come into our mind that “When Sun Microsystem (Oracle) is already given a technology called Servlet to develop server-side web components then why did they give another technology called JSP for the same purpose?”

Wayback ASP (Active server page) programmers did not like Servlet though it is having great features because servlet is not supporting tag-based programming. To attract those non-java programmers to Java they introduced JSP. JSP supports tag-based programming (like HTML, PHP, ASP) and it has all the features of the servlet. Internally every JSP program will be converted into a servlet component.

Initially, the industry used only JSP to develop Java-based websites, but later they started using both Servlet and JSP to develop Java-based websites and sharing the responsibilities.

Key Points on JSP in Java

  • JSP: Jakarta Server Pages (formerly Java Server Pages)
  • Given by: Sun Microsystem (now Oracle)
  • Initial Release: 1999
  • Latest Version: 3.x (3.0 version released in October 21, 2020)
  • Type of format: dynamic web page
  • File Extension: .jsp, .jspx, .jspf
  • Internet media type: application/jsp

Advantages of JSP in Java over Servlet

JSP in Java has the following advantages.

  • JSP supports tag-based programming like HTML, PHP, ASP.
  • It gives 9 implicit objects.
  • To use JSP, strong Java programming knowledge is not required.
  • It is suitable for both Java and non-Java programmers.
  • All features of the servlet can be used in the JSP. Internally every JSP component will be converted into servlet components.
  • No need for exception handling because the internally generated servlet component for the JSP will take care of it.
  • JSP allows separating business logic (Java code) from Presentation logic (HTML code).
  • Placing HTML code in a JSP file is not an error-prone process.
  • We can reduce/avoid Java code in the JSP file.
  • JSP gives built-in JSP tags and also allows to development of custom tags and to use of third-party supplied tags.
  • JSP is easy to learn and apply because the learning curve is very small compared to Servlet.
  • The modifications done in the JSP page/component will reflect directly without touching the source code. No need to compile the source code and reload the application.
  • JSP components or programs can be placed either in a public area or private area. When they are placed in a public area, then their configuration in web.xml is optional.
  • JSP can be used alone to develop complete websites or can be used in combination with Servlet to develop complex websites.
  • It allows us to integrate with HTML, CSS, JavaScript, Jquery, Angular, etc.
  • It gives the feel of working with HTML tags while working with tags.

In JSP vs Servlet, we discussed a detailed comparison between Servlet and JSP, limitations of Servlet, and advantages of JSP in Java over Servlet technology.

JSP API

JSP latest version is 3.x and its packages are,

  • jakarta.servlet.jsp
  • jakarta.servlet.jsp.tagext
  • jakarta.servlet.jsp.el

After changing the JEE name from Java EE to Jakarta EE (see 1, 2, 3 – Feb 2018) the package name “javax” is also changed to “jakarta”.

To execute Servlet components we need a Servlet Container or Servlet Engine. Whereas to execute a JSP component we need a JSP Container or JSP Engine. The JSP Container internally takes the support of the Servlet Container. 

Every JSP Container or JSP engine provides one JSP page compiler to translate JSP into an equivalent servlet component code. In the Tomcat server:- Servlet Container name is Catalina, and Jsp Container name is Jasper which gives JSP page compiler (jspc).

<Tomcat_home>\lib folder having 

  • servlet-api.jar:- representing servlet API
  • jsp-api.jar:- representing JSP API
  • catalina.jar:- representing servlet container
  • jasper.jar:- representing JSP container

Structure of JSP Programming

The file name must have extension .jsp, .jspx, or .jspf, but .jsp is normally used. It is also called a JSP component or JSP file or JSP page. The most regularly used term is the JSP page.

<filename>.jsp,

<%
   …
   … // Java code
   … // scriptlet having Java code
%>
<
   …
   … // HTML code with or without 
   … // JavaScript (jQuery, etc) code
   … // Or, ordinary text
   …
>
<%
   …
   … // Java code
   … // scriptlet having Java code
%>

JSP component/pages generate dynamic web pages. The fixed content will come because of template text (HTML code + ordinary text) and varying/dynamic content of the same page will come because of Java code placed on the JSP page. 

JSP in Java is freestyle programming whereas Servlet is traditional Java programming. Therefore in servlet first packages declaration, then import statement, the class declaration, and so on the proper structure should be there. But JSP is freestyle and wherever we can place anything. 

Sample of JSP Page

first.jsp,

<b> Welcome to JSP pages </b>
<br>
<b> Date and Time :: </b>
<%
   java.util.Date d = new java.util.Date();
   out.println(d);
%>
<br>
end of JSP 

In this first.jsp file, the output will never change because of the first three and last two lines of code which are template text (HTML code + ordinary text). The dynamic output i.e. current system date will come due to the scriptlet having Java code placed at the 5th and 6th line.

Develop JSP Component

Procedure to develop and deploy Java web application having JSP component as the web component.

Step1:- Keep the following software setup ready.

Step2:- Create Deployment Directory structure.

JspApp1
   |=> first.jsp
   |=> WEB-INF
      |=> web.xml

Step3:- Develop the resources (first.jsp, and web.xml)

first.jsp

<h1 style="color:blue; text-align:center">Welcome to Jakarta Server Pages</h1>
<br>
<b>Date and Time:: </b>
<% 
   java.util.Date d = new java.util.Date();
   out.println(d);
%>
<br>
End of JSP Page

If a JSP page/file/component is placed in a public area (outside of the WEB-INF folder) then its configuration in the web.xml file mapping URL is optional. But if the JSP page is placed in a private area (Inside the WEB-INF folder) then its configuration in web.xml file mapping with URL pattern is mandatory.

web.xml

<web-app/>

In this example, the JSP page is in a public area therefore mapping of the JSP page is optional. Hence, in the deployment descriptor file (web.xml) just open and close the web-app tag.

Step4) Start Tomcat server
Use <Tomcat_home>\bin\tomcat10.exe file.

Step5) Deploy the web application
Copy JspApp1 folder to \webapps folder.

Step6) Test the web application.
Type the request URL in the browser address bar:- http://localhost:8080/JspApp1/first.jsp

Why are we not adding the jsp-api.jar file to the classpath and why are we not compiling the JSP page/file from the command prompt? The request given to the JSP page will be taken by the JSP container and uses JSP page compiler (jspc) to translate the requested JSP page into an equivalent servlet component and that component executes with the support of the JSP container and servlet container. Therefore adding a jar file of JSP API is not required.

The modifications done in the JSP page/component will reflect directly without touching the source code. No need to compile the source code and reload the application, just refresh the JSP page and we will get the recent modification.

Limitations of Placing JSP Page in Public Area

  • We can request a JSP page by specifying the JSP file name in the request URL (like http://hostname:port/WebApp/page.jsp). The file extension .jsp tells that the web application is developed in JSP. This exposes the technology in which the web application is being developed and it helps hackers to hack easily.
  • There is no source code protection. 
  • If the JSP page is getting request scope data from the servlet component and displaying that data then the direct request to the public JSP page may give null values (ugly values). 

To overcome these problems, we should place the JSP page/component in a private area and map that JSP component with a URL pattern/URL.

Advantages of Keeping JSP Page or file in the private area

  • Since requests are private to the JSP component through its mapped URL or URL pattern. So, we can hide the technology of the website in which it is being developed. Moreover, in the URL pattern, we can give another extension.
  • We can protect the source code of the JSP component because it is placed in a private area.
  • Since it is there in a private area. So, we can avoid direct requests, and though it is displaying request scope data, we can avoid ugly values for direct requests.
JspApp1
   |==> WEB-INF
      |==> web.xml
      |==> classes
      |==> pages
         |==> first.jsp

The “classes” folder is there to place the servlet component.

web.xml file to map the JSP page placed in the private area (inside WEB-INF folder),

<web-app>
   <servlet>
      <servlet-name>test</servlet-name>
      <jsp-file>/WEB-INF/pages/first.jsp</jsp-file>
   </servlet>

   <servlet-mapping>
      <servlet-name>test</servlet-name>
      <url-pattern>/first</url-pattern>
   </servlet-mapping>
</web-app>

See the source code of this web application at GitHub.

Implicit and explicit objects in JSP

Implicit objects are the objects that are created by JVM/Container and can be used directly without writing any additional code to access them. Examples of implicit objects are “this” and “super” keywords. Explicit objects are the objects those are created by JVM/Container but we need to write additional code to access those objects.

The request, response, ServletContext object, HttpSession object etc are Container created built-in objects (not implicit objects). We need to write some extra code in our Servlet component to get them. For example:- To get access request, response objects we need to write service(-,-) / doXxx(-,-) method,  To get access of ServletContext objects we need to write ServletContext sc = getServletContext(); Similarly to get access to HttpSession object we need to write HttpSession ses = req.getSession();

The JSP gives 9 implicit objects created by Container. We can start using them in our JSP programming directly without placing any additional code.

JSP programming also we have two types of objects.
1) Explicit objects
2) Implicit Objects

Explicit objects:- Normally it is created by a programmer manually. Example:-

<% java.util.Date d = new java.util.Date() %>

Implicit objects:- It is created in the JES (JSP equivalent servlet) class itself. There are 9 implicit objects given in JSP. Example:-

  • request
  • response
  • page
  • pageContext
  • config (ServletConfig)
  • application (ServletContext)
  • out
  • session
  • exception

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

1 thought on “JSP in Java”

  1. I was looking at how to develop JSP Component and you explained it in very simple steps. Thanks for sharing.

Leave a Comment

Your email address will not be published. Required fields are marked *