Introduction to JSP
Last Updated :
28 Aug, 2024
In Java, JSP stands for Jakarta Server Pages( (JSP; formerly JavaServer Pages)). It is a server-side technology that is used for creating web applications. It is used to create dynamic web content. JSP consists of both HTML tags and JSP tags. In this, JSP tags are used to insert JAVA code into HTML pages. It is an advanced version of Servlet Technology i.e. a web-based technology that 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 a servlet by the JSP container before processing the client’s request. JSP has various features like JSP Expressions, JSP tags, JSP Expression Language, etc.
How is JSP more Advantageous than Servlet?
- They are easy to maintain.
- No recompilation or redeployment is required.
- Less coding is required in JSP.
- JSP has access to the entire API of JAVA.
- JSP are extended version of Servlet.
Features of JSP
- Coding in JSP is Easy: As it involves adding Java code to HTML/XML.
- Easy to Use and Learn: It is straightforward and accessible for both Java and non-Java programmers.
- It Does Not Require Advanced Knowledge of Java: Suitable for users with basic Java skills.
- Reduction in the Length of Code: JSP uses action tags, custom tags, etc., to minimize code length.
- Java Scriplets: Allows embedding Java code, variables, and expressions within JSP pages.
- JSP Expression: Evaluates expressions and converts them to strings.
- Declaration Tag: Used to declare variables and methods within JSP pages.
- Implicit Objects: Provides built-in objects that reduce the length of code.
- Make Interactive Websites: Facilitates the creation of dynamic web pages that interact with users in real-time.
- Connection to Database is Easier: Simplifies connecting to databases and allows for easy data reading and writing.
- Extension to Servlet: Inherits all features of servlets and includes implicit objects and custom tags.
- Portable, Powerful, Flexible, and Easy to Maintain: Browser and server independent, making it versatile and easy to manage.
- No Redeployment and No Re-Compilation: JSP is dynamic, secure, and platform-independent, so no need for redeployment or recompilation.
- Create HTML Page from Where Request Will Be Sent: For example,
try.html for sending requests to the server.
- To Handle User Requests, Create .jsp File: For instance,
new.jsp for handling requests.
- Create Project Folder Structure: Organize files and resources into a project structure.
- Create XML File: For configuration or data, e.g.,
my.xml.
- Create WAR File: Package the application into a WAR (Web Application Archive) file for deployment.
- Start Tomcat: Ensure the servlet container (Tomcat) is running.
- Run Application: Deploy and test the JSP application.
- It is Capable of Handling Exceptions: JSP can manage errors and exceptions effectively.
- Difficult to Debug for Errors: Debugging JSP pages can be challenging due to their mixed nature of HTML and Java code.
- First-Time Access Leads to Wastage of Time: Initial access can be slower as JSP pages need to be compiled.
- Output is HTML Which Lacks Features: JSP generates HTML, which may not support advanced features.
Creating a simple JSP Page
hello.JSP :
JSP simply puts Java inside HTML pages. You can take any existing HTML page and change its extension to “.jsp” instead of “.html”. In fact, this is the perfect exercise for your first JSP.
Take the HTML file you used in the previous exercise. change its extension from “.html” to “jsp”. Now load the new file, with the “.jsp” extension, in your browser.
You will see the same output, but it will take longer! But only the first time. If you reload it again, it will load normally.
What is happening behind the scenes is that your JSP is being turned into a Java file, compiled, and loaded. This compilation only happens once, so after the first load, the file doesn’t take long to load anymore. (But every time you change the JSP file, it will be re-compiled again.)
Of course, it is not very useful to just write HTML pages with a .jsp extension! We now proceed to see what makes JSP so useful.
Adding dynamic content via expressions:
As we saw in the previous section, any HTML file can be turned into a JSP file by changing its extension to .jsp . Of course , what makes JSP useful is the ability to embed Java. Put the following text in a file. jsp extension (let us call it hello.jsp) , place it in your JSP directory, and view it in a browser.
<HTML>
<BODY>
Hello! The time is now <%= new java.util.Date() %>
</BODY>
</HTML>
Notice that each time you reload the page in the browser, it comes up with the current time. The character sequence.
<%= and %> enclose Java expressions, which are evaluated at run time.
This is what makes it possible to use JSP to generate dynamic HTML pages that change in response to user actions or vary from user to user.
Explain JSP Elements:
We will learn about the various elements available in JSP with suitable examples. In JSP elements can be divided into 4 different types.
These are:
- Expression
- Scriplets
- Directives
- Declarations
Expression:
We can use this tag to output any data on the generated page. These data are automatically converted to string and printed on the output stream.
Syntax:
JSP Expressions are : <%="Anything" %>
NOTE : JSP Expressions start with Syntax of JSP Scriptles are with <%=and ends with %>. Between these, you can put anything that will convert to the String and that will be displayed.
Example:
<%="HelloWorld!" %>
Scriplets:
In this tag we can insert any amount of valid java code and these codes are placed in the _jsp Service method by the JSP engine.
Syntax:
<%//java codes%>
NOTE : JSP Scriptlets begins with <% and ends %> . We can embed any amount of Java code in the JSP Scriptlets. JSP Engine places these codes in the _jspService() method.
Variables available to the JSP Scriptlets are:
- Request
- Response
- Session
- Out
Directives:
A JSP “directive” starts with <%@ characters. In the directives, we can import packages , and define error-handling pages or the session information of the JSP page.
Syntax:
<%@directive attribute="value"% >
Declarations :
This tag is used for defining the functions and variables to be used in the JSP.
Syntax:
<%!
//java codes
%>
NOTE : JSP Declaratives begins with <%! and ends %> with We can embed any amount of java code in the JSP Declaratives. Variables and functions defined in the declaratives are class-level and can be used anywhere on the JSP page.
Example :
<%@page import="java.util.*"%>
<HTML>
<BODY>
<%!
Date theDate = new Date(); // Corrected the unwanted space in the declaration
Date getDate() {
System.out.println("In getDate() method");
return theDate;
}
%>
Hello! The time is now <%=getDate()%>
</BODY>
</HTML>
Example of a JSP Web Page:
<HTML>
<HEAD>
<TITLE>A Web Page</TITLE>
</HEAD>
<BODY>
<%out.println("Hello there!");%>
</BODY>
</HTML>
Run a Simple JSP Page:
- Step 1: Save the JSP file using “.jsp” extension (ex- “hello.jsp”)
- Step 2: Start the server
- Step 3: Place your application inside a folder
- Step 4: To execute the JSP script, simply start tomcat server and use a browser to browse an URL of the JSP page i.e. http://localhost:portnumber/YourApplicationContextRoot/jspfile then you will see the jsp file is being compiled.
Please Login to comment...