Bootstrap FreeKB - Java - Create a servlet in Eclipse
Java - Create a servlet in Eclipse

Updated:   |  Java articles

This assumes you have created your first dynamic web project in Eclipse

In the left panel of Eclipse, expand the Java Resources folder, right click on the src folder, and select New > Package. Give the package a name, such as com.main.servlet and select Finish. Right click on the newly created package and select New > Servlet. Give the servlet a name, such as Servlet, and select Finish.

Add the import Print Writer line near the top of the page.

import java.io.PrintWriter;

 

In the doGet method section of the markup, add the following.

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("Hello World");

 

Add the following markup to your web.xml file.

    <servlet>
        <servlet-name>Servlet</servlet-name>
        <servlet-class>com.jwt.servlet.Servlet</servlet-class>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>Servlet</servlet-name>
        <url-pattern>/servlet</url-pattern>    
    </servlet-mapping>

 

Run the application in Eclipse, or export the WAR and run the application on an application server. Now, when navigating to www.example.com/app_name/myServlet, "Hello World" should be displayed.




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 4c5706 in the box below so that we can be sure you are a human.