Java Server Pages (JSP) is a technology used to create dynamic content for web-pages.
How different it is from normal HTML file? There is a lot of difference. Normal HTML files are served by the web-servers whenever the user requests for the file. Also JSP pages are served by the web-servers. But there is a tricky thing goes here. Web-server doesn’t serve you the JSP file as it is; it executes the code inside JSP file and generates an output, what we called dynamic HTML and served to the web-browser.
In detail, below are the things will happen when the user requests a JSP file from the web-browser:
- User requests a JSP file from the web-browser. The HTTP request goes to the web-server.
- 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 extension, 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 web-server returns the response to the web-browser. Web-browser will display the returned HTML or an XML.
Pictorial representation of JSP lifecycle would look likes below:
How JSP file looks like?
JSP code resides in “.jsp” files. The very first line of the file contains a page directive, which tells to the web-server that the java code is added inside the file.
<%@page language="java" contentType="text/html"%>
A JSP file contains embedded HTML tags also.
Lets take a simple JSP page “ip.jsp”; which displays 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 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.