How JSP Works (Demonstrate with an Example)

JavaServer Pages (JSP) is a technology that simplifies the process of creating dynamic web content using Java. By embedding Java code within HTML pages, JSP enables developers to create robust and interactive web applications. In this blog post, we will explore how JSP works and demonstrate its functionality with a simple example.

How JSP Works

To understand how JSP works, it's essential to know the process it follows from the moment a client requests a JSP page to the point where the server sends back a dynamic response.

Step-by-Step Process

  1. Client Request: A client (usually a web browser) sends a request to the web server for a specific JSP page.

  2. Translation and Compilation: The web server translates the JSP file into a Java servlet. This translation process converts the JSP syntax and embedded Java code into a servlet class. The servlet class is then compiled into bytecode that can be executed by the Java Virtual Machine (JVM).

  3. Execution: The compiled servlet is loaded into memory and initialized. When the servlet is executed, it processes the client request by running the embedded Java code. This code can interact with databases, call JavaBeans, and perform other server-side operations.

  4. Response Generation: The servlet generates an HTML response based on the logic implemented in the JSP page. This response includes the dynamic content created by the embedded Java code.

  5. Client Response: The generated HTML response is sent back to the client's browser, which displays the content.

Demonstrating JSP with an Example

Let's demonstrate how JSP works with a simple example. We'll create a JSP page that greets the user by name and displays the current date and time.

Example JSP Page

Create a file named greeting.jsp and add the following code:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Greeting Page</title>
</head>
<body>
    <h1>Welcome, <%= request.getParameter("name") %>!</h1>
    <p>Today's date and time is: <%= new java.util.Date() %></p>
</body>
</html>

Explanation of the Code

  • The <%@ page contentType="text/html;charset=UTF-8" language="java" %> directive specifies the content type and character encoding for the page and indicates that the page uses Java.
  • The HTML structure defines the layout of the page.
  • The <%= request.getParameter("name") %> expression retrieves the value of the "name" parameter from the client request and displays it within the <h1> tag.
  • The <%= new java.util.Date() %> expression creates a new Date object and displays the current date and time within the <p> tag.

Step-by-Step Processing of the JSP Example

  1. Client Request: When the client sends a request to http://localhost:8080/greeting.jsp?name=John, the web server receives the request for the greeting.jsp page.

  2. Translation and Compilation: The web server translates greeting.jsp into a Java servlet. Here is a simplified version of what the servlet look like:

    public class greeting_jsp extends HttpServlet {
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            try {
                out.println("<html>");
                out.println("<head><title>Greeting Page</title></head>");
                out.println("<body>");
                out.println("<h1>Welcome, " + request.getParameter("name") + "!</h1>");
                out.println("<p>Today's date and time is: " + new java.util.Date() + "</p>");
                out.println("</body>");
                out.println("</html>");
            } finally {
                out.close();
            }
        }
    }
    
  3. Execution: The web server compiles this servlet into bytecode, loads it into memory, and initializes it. When the servlet executes, it processes the request by retrieving the "name" parameter from the query string and creating a new Date object.

  4. Response Generation: The servlet generates an HTML response. This is what the response look like:

    <html>
    <head>
        <title>Greeting Page</title>
    </head>
    <body>
        <h1>Welcome, John!</h1>
        <p>Today's date and time is: Thu Jul 11 14:00:00 IST 2024</p>
    </body>
    </html>
    
  5. Client Response: The generated HTML response is sent back to the client's browser, which displays the content.

Running the Example

  1. Deploy the JSP Page: Deploy the greeting.jsp file to a web server that supports JSP, such as Apache Tomcat.

  2. Access the JSP Page: Open a web browser and navigate to the URL of the JSP page. For example, http://localhost:8080/greeting.jsp?name=John.

  3. View the Output: The browser displays the dynamic content generated by the JSP page, greeting the user by name and showing the current date and time.

Conclusion

JavaServer Pages (JSP) is a powerful technology for creating dynamic web applications using Java. By embedding Java code within HTML, JSP enables developers to generate dynamic content easily. In this post, we explored the step-by-step process of how JSP works and demonstrated its functionality with a simple example. By understanding these concepts, developers can leverage JSP to build robust and interactive web applications.

Comments