Java Server Pages Introduction
What is JSP :
Java Server Pages (JSP) is a platform independent presentation layer technology that comes with SUN s J2EE platform. JSPs are normal HTML pages with Java code pieces embedded in them. JSP pages will saved to .jsp extension.The JSPs have a translation or a compilation process where the JSP engine translates and compiles a JSP file into a Servlet. The translated and compiled Servlet moves to the execution phase (run time) where they can handle requests and send response.
Life Cycle of JSP :
JSP has three life cycle methods: jspInit(), _jspService() and jspDestroy(). You can override jspInit() and jspDestroy() but you can not override _jspService() method.
When a request is sent to Server first time and server identifies right resource(JSP) to handle a request, JSP is first translated to a Servlet.
After translating to a Servlet, it gets compiled and create class file of the servlet.
After successfully creation of complied class file, JSP engine will call jspInit() method to initialize class file.
After initialization, service method is called and it handles the request and create response to be delivered to browser.
Once the service is completed and response is sent back, jspDestroy() method is called and it will clean up the resources.
Difference between JSP and Servlets :
- JSP is used mainly for presentation only. A JSP can only be HttpServlet that means the only supported protocol in JSP is HTTP. But a servlet can support any protocol like HTTP, FTP, SMTP etc.
- Automatic recompilation of a jsp page by web container for any modification in the code;whereas we need to recompile servlet for any single change in the source code of the servlet
- Allows you to access a jsp page directly as simple HTML page,whereas servlets cannot be accessed directly and have to be first mapped into web.xml file.
JSP Comments :
In a jsp we should always try to use jsp style comments unless you want the comments to appear in the HTML.The jsp comment don't appear in the output produced by the jsp page when it runs. Jsp comments are used for documentation purpose:
There are two types of comments allowed in JSP page :
- JSP comment : This comment will not appear in the output.
<%-- JSP comment --%>
- HTML comment : This type of comment will appear in the output.
Directory Structure of JSP application :
JSP Environment Setup :
To setup your JSP development environment we need to follow steps which is mentioned bellow.
- Setting up Java Development Kit:
- Setting up Web Server: Tomcat:
Setting up Java Development Kit :
If you want to setup JDK in your system Click here to go to the tutorial which will guide you "how to install JDK:
Setting up Web Server: Tomcat:
To run JSP we need web server.A number of Web Servers that support JavaServer Pages are available in the market like tomcat,jboss etc.Some web servers are freely downloadable and Tomcat is one of them.
Apache Tomcat is an open source software implementation of the JavaServer Pages and Servlet technologies and can act as a standalone server for testing JSP. Here are the steps to setup Tomcat in your machine:
- Download latest version of Tomcat from Apache Site
- Once you downloaded the installation, unpack the binary distribution into a convenient location. For example in C:\Program Files\Apache Software Foundation\Tomcat 6.0 on windows. If you are using .exe file for installation, install it like other software in Windows.
Tomcat can be started by executing the following commands on windows machine.
Navigate to your tomcat installation bin folder and execute startup.bat batch file or click on tomcat6.exe file.
You will see the console like:
How to test your tomcat installation :
Start your tomcat server by steps mentioned above.Once server is in running state open your favorite browser and type http://localhost:9999/ in url.Here 9999 is port no of my system on which tomcat is running.By default it will run on 8080 port. You can change this port at the time of installation. After typing http://localhost:9999/ in your browser, following page will open.