CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

JSP – An Introduction

Java Server Pages (JSP) is a technology used to create dynamic content for web pages.

How different it is from a normal HTML file? There is a lot of difference. Normal HTML files are served by the web servers whenever the user requests the file. Also, JSP pages are served by web servers. But there is a tricky thing that goes here. Web-server doesn’t serve you the JSP file as it is; it executes the code inside the JSP file and generates an output, what we called dynamic HTML, and served to the web browser.

In detail, below are the things that will happen when the user requests a JSP file from the web browser:

  • The user requests a JSP file from the web browser. The HTTP request goes to the webserver.
  • The web server recognizes that the HTTP request is for a JSP page and forwards the request to a JSP Engine. Why JSP Engine? Web-server functionality can be extended by adding extensions. JSP Engine is one of the extensions, can able to compile JSP pages.
  • JSP Engine loads the JSP file and checks whether the JSP syntax is correct or not. If the syntax check is correct JSP Engine then translates the requested JSP page into a Java Servlet. A Java Servlet is capable of dynamically processing the requests and constructing responses in a protocol-independent manner. JSP Engine compiles the Java Servlet code and generates a Servlet class. JSP Engine delegates the request to the Servlet Engine.
  • Servlet Engine’s responsibility is to execute the generated Servlet class. During execution, the Servlet produces an output. This output is in HTML or an XML format and Servlet Engine passes this to the web-server inside an HTTP response.
  • Finally, the webserver returns the response to the web browser. The web browser will display the returned HTML or an XML.

Pictorial representation of JSP life-cycle would look likes below:

JSP Lifecycle
JSP Lifecycle

How does the JSP file look like?

JSP code resides in “.jsp” files. The very first line of the file contains a page directive, which tells the webserver that the java code is added inside the file.

<%@page language="java" contentType="text/html"%>

A JSP file contains embedded HTML tags also.

Let’s take a simple JSP page “ip.jsp”; which displays the IP address of the system from where the request comes:

<%@page language="java" contentType="text/html"%>

<html>
   <head>
      <title>Your System IP Address</title>
   </head>
   
   <body>
      <%
         out.println("<br/>Your System IP Address is : " + request.getRemoteAddr());
      %>
   </body>
</html>

Observe that above “.jsp” file contains HTML tags and Java code. And you might be wondered where “out” and “request” objects are created. “out” and “request” are the implicit Java objects created by JSP Container and makes available to the developers. “out” object is of type JspWriterImpl class (which extends an abstract class JspWriter) and “request” object is of type HttpServletRequest class.

println is a method of JspWriter class used to display the text in HTML <body> element. “request” object contains the IP address from which the request was originated and the getRemoteAddr method is used to extract that address.

Once the JSP page is accessed, it will display an IP address of the system from which the request was originated in the web browser window.

.Paul.

JSP – An Introduction

Leave a Reply

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

Scroll to top