What is a JSP File?
A JavaServer Page (JSP) is a web page template that uses Java code to generate an HTML document dynamically. JSPs are run in a server-side component known as a JSP container, which translates them into equivalent Java servlets.
A JSP file is simply an HTML page with some Java code sprinkled in, providing dynamic content on your page.
The end result is an HTML page with content generated by Java code.
Where is the JSP Processed?
- JSP files are processed on the server, for example, on web servers or application servers like Tomcat, Glassfish, or JBoss.
- After processing, the Java code results are included in the HTML returned to the browser.
Where to Place JSP File?
- When creating a new Eclipse dynamic project, place the JSP file in the
WebContent
folder. - The JSP file must have a
.jsp
extension. - In enterprise J2EE web applications, it is recommended to keep JSP files inside the
WEB-INF
folder.
JSP Hello World Example using Eclipse
Let's create a step-by-step JSP Hello World program using Eclipse IDE.
1. Create a Dynamic Project
- Open Eclipse, then select
File -> New -> Dynamic Web Project
. - Enter the Project name as
jsp-helloworld
. - Make sure the target runtime is set up for Apache Tomcat v8.
- Click
Finish
.
Here is the structure of the generated project looks like the following:
2. Create helloworld.jsp
File
- In the
WebContent
folder, right-click and selectNew -> JSP File
. - Name the file as
helloworld.jsp
. - Click
Finish
.
Add the following code to the newly created helloworld.jsp
:
<%@ page import="java.time.LocalTime" %>
<%@ page import="java.time.LocalDate" %>
<html>
<body>
<h3>Hello World of Java!</h3>
Date and Time on Server: <%= LocalDate.now() %> <%= LocalTime.now() %>
</body>
</html>
Note that we have used Java 8 LocalDate
and LocalTime
classes to print the current date and time on the server side.
3. Running the JSP Hello World Program
Right-click on the helloworld.jsp
file and run it as a server. This will deploy the JSP application on the Tomcat server.
4. Demo
Once the JSP program is up and running on the Tomcat server, visit http://localhost:8080/jsp-helloworld/helloworld.jsp in your browser. The following page will be displayed:
We covered a very basic JSP Hello World example. We'll dig deeper into JSP in the following articles.
For more in-depth tutorials, visit JSP Tutorials.
Comments
Post a Comment
Leave Comment