Signup/Sign In

Creating First Servlet Application using Eclipse IDE

Eclipse IDE is the most popular Java IDE used in the Industry. It is developed by an open source community and can be downloaded for free from Eclipse.org


Steps to create Servlet using Eclipse IDE

To create a Servlet application in Eclipse IDE you will need to follow the following steps:

  1. Goto File -> New -> Dynamic Web Project

    steps to create servlet application in eclipse


  2. Give a Name to your Project and click Next

    steps to create servlet application in eclipse


    steps to create servlet application in eclipse


  3. Check Generate web.xml Deployment Descriptor and click Finish

    steps to create servlet application in eclipse


  4. Now, the complete directory structure of your Project will be automatically created by Eclipse IDE.

    steps to create servlet application in eclipse


  5. Click on First project, go to Java Resources -> src. Right click on src select New -> Servlet

    steps to create servlet application in eclipse


  6. Give Servlet class name and click Next

    steps to create servlet application in eclipse


  7. Give your Servlet class a Nmae of your choice.

    steps to create servlet application in eclipse


  8. Leave everything else to default and click Finish

    steps to create servlet application in eclipse


  9. Now your Servlet is created, write some code inside it. You can take reference from the code in the picture below.

    steps to create servlet application in eclipse


  10. Add servlet-api.jar JAR file to your project. Click on Libraries, right click on Web App Libraries select Build Path -> Configure Build Path

    steps to create servlet application in eclipse


  11. Click on Add External JARs

    steps to create servlet application in eclipse


  12. This JAR is now added to your project's build path.
  13. Select servlet-api.jar from Apache Tomcat Directory

    steps to create servlet application in eclipse


    steps to create servlet application in eclipse


  14. Now all you have to do is Start the server and run the application.

    steps to create servlet application in eclipse


Example of Servlet Response on Eclipse

Step 1: Create a dynamic project on eclipse.


servlet-response

Step 2: Now create an HTML file.

servlet-response

And write the below code.

	
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="display" method="get">
Enter User name: <input type="text" name="val1">
Enter Password: <input type="text" name="val2">
<input type="submit" value="login">
</form>
</body>
</html> 
	

Step 3: now add the below code in web.xml file.

web.xml file is a deployment descripter. Here we have all the configurations.

	
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
 <servlet>
  	<servlet-name>abc3</servlet-name>
  	<servlet-class>com.app.studytonight.demo4</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>abc3</servlet-name>
  <url-pattern>/display</url-pattern>
  </servlet-mapping>
</web-app> 
	

Step 4: Now next create a servlet. For this create a class. Give the package name and the class name.

servlet-response
servlet-response

Add the below code in the class file.

Demo4.java

	
package com.app.studytonight;

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class demo4 extends HttpServlet{
   public void doGet(HttpServletRequest req,HttpServletResponse res)
   throws ServletException,IOException
   {
     res.setContentType("text/html");
     PrintWriter pwriter=res.getWriter();
     String uname=req.getParameter("val1");
     String pw=req.getParameter("val2");
     pwriter.println("User Details Page:");
     pwriter.println("Hello "+uname);
     pwriter.println("Your Password is **"+pw+"**");
     pwriter.close();
  }
}
	

Now, Run the code.

servlet-response
servlet-response