Did this blog help you solve a problem? Tip me with Bitcoins at: 17dQktcAmU4urXz7tGk2sbuiCqykm3WLs6

Thursday, November 12, 2009

Hello World Java Web Application in Eclipse (Part 2)

Want to integrate charts into your webapp? Check out XChart.

Servlet, JSPs, web.xml, build.xml, build.properties


Download .war and full source code of this and other sample Java Web Applications here.

This is the second of a three part series of tutorials that shows how to create a Hello World Java Web Application in Eclipse. In Part 1, I demonstrate how to create your Java Web Application project in Eclipse and set up the directory structure of the project. In this part, I take you through getting the Java jar needed to code Java Servlets, creating a Java Servlet and a JSP file, configuring the web.xml and build.xml files. In Part 3, I show how to build the project with ANT using the build.xml file and how to deploy the project to a Tomcat server running on your development computer. Along the way, I point out a few tips and tricks for using the Eclipse EE IDE.



From part 1, we have a Java project in Eclipse named HelloWebApp with the following directory structure:


HelloWebApp (this is the project)
--src
----com.hello (this is actually a package, not a folder)
--lib
--web
----WEB_INF

Step 1: Put the servlet-api.jar, which comes with the Tomcat installation, in the lib folder. The jar is found in the lib folder in the Tomcat installation's root directory.



Step 2: Add the jar to the project's build path. Right-click on the jar in the lib folder of the Web App project and select Build Path --> Add to Build Path.



Step 3: Create a Servlet file. Right-click on the com.hello package in the src folder and select New --> Class. Name it HelloServlet.java. Copy and paste the following code into the file.

package com.hello;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet implements Servlet {

private static final long serialVersionUID = 1L;

public HelloServlet() {
super();
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

request.setAttribute("hello_string", "Hello WebApp!");

request.getRequestDispatcher("/hello.jsp").forward(request, response);

}
}


Step 4: Create a JSP file. Right-click on the "web" folder and select New --> Other... . A wizard will pop up and you need to find the JSP file type, which is in the Web folder. Name it hello.jsp. Copy and paste the following code into the file.

<%@ 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 Web App</title>
</head>
<body>
<h3><%=(String)request.getAttribute("hello_string")%></h3>
</body>
</html>



Step 5: Create another JSP file. Name it index.jsp. Copy and paste the following code into the file.

<%@ 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>Index</title>
</head>
<body>
<jsp:forward page="/hello" />
</body>
</html>


Step 6: Create an XML file. Right-click on the "WEB_INF" folder and select New --> File. Name it web.xml. Copy and paste the following code into the file.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<display-name>HelloWebApp</display-name>

<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.hello.HelloServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>


Step 7: Create another XML file. Right-click on the HelloWebApp project and select New --> File. Name it build.xml. Copy and paste the following code into the file.

<?xml version="1.0"?>
<project name="hello" default="deploy_local" basedir=".">

<property file="build.properties"/>

<path id="classpath">
<fileset dir="${lib.dir}" includes="servlet-api.jar"/>
</path>

<target name="clean">
<echo>Cleaning the ${build.dir}</echo>
<delete dir="${build.dir}"/>
<delete dir="${dist.dir}"/>
</target>

<target name="init" depends="clean">
<echo>Creating the build directory</echo>
<mkdir dir="${build.dir}/WEB-INF/classes"/>
<mkdir dir="${build.dir}/WEB-INF/lib"/>
<mkdir dir="${dist.dir}"/>
</target>

<target name="compile" depends="init">
<echo>Compile the source files</echo>
<javac srcdir="${src.dir}" destdir="${build.dir}/WEB-INF/classes">
<classpath refid="classpath"/>
</javac>
</target>

<target name="copy" depends="compile">
<copy todir="${build.dir}/WEB-INF">
<fileset dir="${web.dir}/WEB-INF"/>
</copy>
<copy todir="${build.dir}">
<fileset dir="${web.dir}"/>
</copy>
<copy todir="${build.dir}/WEB-INF/lib">
<fileset dir="${lib.dir}">
<exclude name="servlet-api.jar"/>
</fileset>
</copy>
</target>

<target name="war" depends="copy">
<echo>Building the war file</echo>
<war destfile="${dist.dir}/${project.name}.war" webxml="${build.dir}/WEB-INF/web.xml">
<fileset dir="${build.dir}"/>
</war>
</target>

<target name="deploy_local" depends="war">
<echo>Deploying .war to local Tomcat</echo>
<copy todir="${tomcat.dir}">
<fileset dir="${dist.dir}">
<include name="${project.name}.war"/>
</fileset>
</copy>
</target>

</project>


Step 8: Create a properties file. Right-click on the HelloWebApp project and select New --> File. Name it build.properties. Copy and paste the following code into the file.

project.name=HelloWebApp
lib.dir=lib
src.dir=src
web.dir=web
build.dir=build
dist.dir=dist
tomcat.dir=/Library/Tomcat/webapps


Here's what your new HelloWebApp Java project should now look like in the Eclipse Package Explorer View:



OK, that's what you need for a fully functional HelloWorld-type Java Web Application that runs on a Tomcat Server. In the next and final part, I will explain the build.xml file, use ANT to build the project and deploy it to the Tomcat server.


Piece of Cake!!

Download .war and full source code of this and other sample Java Web Applications here.
Hello World Java Web Application in Eclipse (Part 1)
Hello World Java Web Application in Eclipse (Part 3)

5 comments:

tea said...

The web.xml file defines each servlet and JSP page within a Web Application. It also enumerates enterprise beans referenced in the Web application in eclipse.

Chandan Rastogi said...

Hi, in phase 2 while creating a class HelloServlet.java I am not getting any option to insert the code.

Chandan Rastogi said...

While creating the HelloServlet.java in phase 2, I am unable to insert the code that told told.

Please advice.

gaurav kapoor said...

Hey Man!!
Thanks alot for your forum.

I was really struggling for similar kind of tutorial...You saved me.

Thanks!!!

gaurav kapoor said...

Thanks Thanks Thanks !!!
For this nice tutorial!!!

You really helped me..I was looking for similar tutorial.