Introduction to JSP: What are JSPs and How Do They Work?

Harshal Deshmukh
5 min readApr 28, 2023

--

JavaServer Pages (JSPs) is a popular technology used for developing dynamic web pages. It is a powerful server-side scripting language that allows developers to embed Java code within HTML pages. JSPs provide an efficient way to create dynamic web pages that can be customized for individual users, making them an essential tool for web developers.

In this blog post, we will have a quick introduction to JSPs, including what they are and how they work.

What is a JSP ?

A JavaServer Page (JSP) is a server-side technology that allows you to create dynamic web pages using Java code. JSPs are similar to PHP or ASP, but instead of using a scripting language like PHP or VBScript, you write Java code that is executed on the server. JSPs are part of the Java EE (Enterprise Edition) platform and are typically used to generate HTML, XML, or other types of content that are sent to the client’s web browser.

How do JSPs work ?

JSPs are compiled into Java Servlets (a Java class that runs on the server) before being executed. When a client sends a request to the server, the server looks at the URL to determine which JSP to execute. The JSP is then compiled into a Servlet and executed by the server. The output of the Servlet is sent back to the client as HTML or another type of content.

JSPs provide a number of features that make them a popular choice for web development. These features include:

  1. Scripting elements — JSPs provide a number of scripting elements that allow developers to embed Java code within HTML pages.
  2. Standard Tag Library — JSPs provide a standard tag library that includes a number of pre-defined tags that can be used to perform common tasks, such as accessing a database.
  3. Expression Language — JSPs provide an expression language that allows developers to access and manipulate data within a JSP page.
  4. Custom Tags — JSPs allow developers to define custom tags that can be used within a JSP page to perform specific tasks.

Syntax:

Since this is an extension of Java, code will be written in Java itself. There will be more features to play with addition to code Java features. There will be a block in which every code of the Java will be written. Outside this block HTML and the CSS, JavaScript, JQuery can be written. Now, time to see the scripting elements. These elements help us to write the java code into the JSP page.

Scriptlet Tag:

<%  Our java source code goes here %>

Expression Tag:

<%=  our expression goes here %>

Declaration Tag:

<%!  Our field or the method declaration goes here %>

Implicit objects of JSP in Java

JSP provides nine implicit objects to help developers fulfill various needs. out: It object can be used to display output on the screen

PrintWriter out=response.getWriter();

request: This object is used to retrieve data submitted by form elements.

String name=request.getParameter("user_name");

response: This object can be used to send requests to a page.

config: This object can be used to initialize parameters for a page or an entire application.

The other implicit objects include “application,” “session,” “pageContext,” “page,” and “exception.” JSP is a secure technology compared to other dynamic languages like PHP and ASP. Thanks to its popularity, JSP is still in demand for web development.

Examples:

<html>
<head>
<title>JSP Test Page</title>
</head>
<body>
<%
// This is single line comment
/*
This is multi-line comment line -1
This is multi-line comment line -2
*/
out.println("<h1>This is my first JSP Program.</h1>");
%>
</body>
</html>

Output:

<html>
<head>
<title>JSP Test Page</title>
</head>
<body>
<%@ page session = "true" %>
<%
int i = 10;
int j = 20;
int sum = i + j;
out.println("Sum of i and j will be: "+sum);
%>
</body>
</html>

Output:

Advantages of using JSPs

There are several advantages to using JSPs in your web application development:

· Dynamic content: JSPs allow you to create dynamic content that can change based on user input or other variables. This makes your web application more interactive and engaging for your users.

· Java integration: JSPs allow you to use Java code to generate dynamic content. This means you can use the full power of the Java programming language to create complex and sophisticated web applications.

· Separation of concerns: JSPs allow you to separate the presentation layer (the HTML and CSS) from the business logic layer (the Java code). This makes it easier to maintain and update your web application over time.

Disadvantages of using JSPs

· Though it is possible to access databases using JSP, it can be challenging as many servlets do not support it.

· Additionally, as JSPs are servlets, it can be difficult to trace errors in the code.

· Compiling JSPs can also take longer than on a server, which can slow down the development process.

Despite these challenges, JSP remains a popular technology for web development due to its ability to create dynamic web pages using Java code.

Conclusion

JSPs are a powerful tool for web developers, providing a way to create dynamic web pages that can be customized for individual users. They work by allowing developers to embed Java code within HTML pages, which is then compiled and executed as a Java Servlet.

JSPs provide a number of features that make them a popular choice for web development, including scripting elements, standard tag libraries, expression language, and custom tags.

--

--