The lifecycle of a JavaServer Pages follow these phases:
- Translation of JSP Page
- Compilation of JSP Page
- Classloading (the classloader loads class file)
- Instantiation (Object of the Generated Servlet is created).
- Initialization ( the container invokes jspInit() method).
- Request processing ( the container invokes _jspService() method).
- Destroy ( the container invokes jspDestroy() method).
Creating a simple JSP Page
To create the first JSP page, write some HTML code as given below, and save it by .jsp extension. We have saved this file as index.jsp. Put it in a folder and paste the folder in the web-apps directory in apache tomcat to run the JSP page.index.jsp
Let’s see the simple example of JSP where we are using the scriptlet tag to put Java code in the JSP page. We will learn scriptlet tag later.
Code language: HTML, XML (xml)
<html> <body> <% out.print(2*10); %> </body> </html>
It will print 20 on the browser.
How to run a simple JSP Page?
Follow the following steps to execute this JSP page:
- Start the server
- Put the JSP file in a folder and deploy on the server
- Visit the browser by the URL http://localhost:portno/contextRoot/jspfile, for example, http://localhost:8888/myapp/index.jsp
Do I need to follow the directory structure to run a simple JSP?
No, there is no need of directory structure if you don’t have class files or TLD files. For example, put JSP files in a folder directly and deploy that folder. It will be running fine. However, if you are using Bean class, Servlet or TLD file, the directory structure is required.
The Directory structure of JSP
The directory structure of JSP page is same as Servlet. We contain the JSP page outside the WEB-INF folder or in any directory.
This article is contributed by Mansi Yadav. If you like Techarge and would like to contribute, you can also write an article using https://techarge.in/start-blogging-with-us/ or mail your article to firstname.lastname@example.org . See your article appearing on the Techarge Home page and help others to learn. 🙂