Thursday 18 October 2012

JSP Hello World Example using Eclipse IDE and Tomcat web server


   JSP Hello World Example using Eclipse IDE and Tomcat web server

1. Create Dynamic Web Project

Select from the menu File --> New --> Dynamic Web Project.
Create Dynamic Web Project
Enter "HelloWorldJSP" as the project name. Keep rest of the settings as it is as shown in the following screenshot.
Enter project name
Click "Next" button.
Configure project
Click "Next" button.
Configure web module setting
Check 'Generate web.xml deployment descriptor' checkbox and click "Finish" button and Eclipse IDE will generate the web project automatically as shown below
Generated web project

2. Create Jsp page

Right click on 'WebContent' folder and select from context menu New --> Jsp File.
Create Jsp page
Write "helloWorld.jsp" in the 'File Name' field and Click "Finish" button.
Create new jsp file
Eclipse will generate a jsp page and open the same in the JSP editor as shown below
File: helloWorld.jsp
1
2
3
4
5
6
7
8
9
10
11
12
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
</body>
</html>


4. Write JSP Code

Edit the generated 'helloWorld.jsp' as per the following code.
File: helloWorld.jsp
1
2
3
4
5
6
7
8
9
10
11
12
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World - JSP tutorial</title>
</head>
<body>
    <%= "Hello World!" %>
</body>
</html>


5. Run Your Code

Right click on 'helloWorld.jsp' and select from context menu 'Run As' --> 'Run on Server'.
Run on Server
Select the existing tomcat server. If not available then manually define a new web server.
Run on Server
Click "Finish" button. HelloWorldJSP web application will be deployed in the tomcat web server.
Console Output

6. Browser Output

Eclipse will open a browser and your server side jsp code will print 'Hello World!' in the browser.
Browser Output

No comments:

Post a Comment