How to Create and Run Web Project (Java) in Eclipse


Open Eclipse – > /*Click on File Menu

New Project in Eclipse
New Project

Java – Download Free EBooks and Whitepapers

Select Web -> Dynamic Web Project

Web Project in Eclipse
Web Project

Provide proper Project Name

New Dynamic Web Project in Eclipse
New Dynamic Web Project in Eclipse

Keep the Source Folder default

New Dynamic Web Project Configuration in Eclipse
New Dynamic Web Project Configuration

Keep the Context Root and Content Directory default

New Dynamic Web Project Configure Web Module Settings in Eclipse
New Dynamic Web Project Configure Web Module Settings

Click on Finish.

Now Verify Project Explorer in Eclipse

New Web Project in Eclipse (Project Explorer)
New Web Project (Project Explorer)

In Java Resource -> src

create servlet

New Servlet in Eclipse
New Servlet

Give Source Folder, Package and Class Name

Create a Servlet in Eclipse
Create a Servlet

Give Deployment Descriptor Specific Information

Specify modifiers, interfaces to implement, and method stubs to generate

Specify modifiers, interfaces to implement, and method stubs to generate
Specify modifiers, interfaces to implement, and method stubs to generate

Click Finish.

HelloWorldExample will contain…
===================================================================
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class HelloWorldExample
*/
@WebServlet(“/HelloWorldExample”)
public class HelloWorldExample extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* Default constructor.
*/
public HelloWorldExample() {
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out = response.getWriter();
out.println(“Hello World!!!!!!!!!!!!!!”);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}
===================================================================

Now you will see:

servlet-api.jar errors in eclipse
servlet-api.jar errors

Please Visit How to Solve Servlet Errors in Eclipse? (clean-clouds.com) to find the solution.

Once all errors are resolved

Servlet errors resolved
Servlet errors resolved

Now lets do entries in Deployment Descriptor, web.xml

======================================================================

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_3_0.xsd” metadata-complete=”true” version=”3.0″>
<description>
Servlet and JSP Examples.
</description>
Servlet and JSP Examples
<servlet>
<servlet-name>HelloWorldExample</servlet-name>
<servlet-class>HelloWorldExample</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloWorldExample</servlet-name>
<url-pattern>/HelloWorldExample</url-pattern>
</servlet-mapping>

</web-app>

======================================================================

Now lets try to run the project from the eclipse; but for that we need to add web server in eclipse. We will add tomcat 7.x in eclipse

Please visit How to add Tomcat 7.x in Eclipse?

Now lets try to run it in eclipse after adding Tomcat Server in Eclipse

Run Dynamic Web Project on Server From Eclipse
Run Dynamic Web Project on Server
Run Dynamic Web Project on Server From Eclipse-Select Resources
Run Dynamic Web Project on Server -Select Resources

Unblock Features in Windows Firewall

Unblock Features in Windows Firewall
Unblock Features in Windows Firewall

Now Tomcat will start

Tomcat Console in Eclipse
Tomcat Console

Verify the Tomcat status from the Server Tab

Tomcat Server Status in Eclipse
Tomcat Server Status

And we can see servlet is successfully executed at

Successful Servlet Execution in Tomcat Server in Eclipse
Successful Servlet Execution in Tomcat Server

You can copy same path and verify in any browser.

http://localhost:8080/JavaWebProject/HelloWorldExample

Successful Servlet Execution in Tomcat Server in Browser
Successful Servlet Execution in Tomcat Server in Browser

Done!!! 🙂

Other Interesting Tomcat related Articles:

Advertisements

13 thoughts on “How to Create and Run Web Project (Java) in Eclipse”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s