Servlet-Running-on-Tomcat

How to create a Sample Servlet using Eclipse?

Let us create a sample servlet and deploy it on Tomcat server. First of all, create a Web Project in Eclipse. For instructions creating a web project in eclipse, please go through the following question. ( http://www.testingdocs.com/questions/how-to-deploy-a-war-file-in-tomcat-using-eclipse/ )

Create Sample Servlet

Right click the source folder and choose New >> Servlet as shown in below picture.

Create Sample Servlet

 

Give the package name and the Servlet class name of your choice.

 

Create Servlet Window

 

In the next wizard screen, enter servlet deployment descriptor specific information.

 

Servlet Deployment Descriptor Information

In the next screen, choose the auto generated method stubs. For simple servlet like ours doGet and doPost would suffice. Click on the Finish button to complete the process.

Give some implementation in the method stubs. I’m gonna just say hello world and tell visitor to visit my website for QA stuff as shown below:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 // TODO Auto-generated method stub
 response.getWriter().append("Served at: ").append(request.getContextPath());
 {
 response.setContentType("text/html");
 PrintWriter out = response.getWriter();
 out.println("<html>");
 out.println("<head>");
 out.println("<title>Hello World!</title>");
 out.println("</head>");
 out.println("<body>");
 out.println("<h1>Hello World!</h1>");
 out.println("Visit www.TestingDocs.com for interesting QA stuff..!!");
 out.println("</body>");
 out.println("</html>");
 }
 }

 

Save the changes and deploy the application in Tomcat server as shown in above link.

Note that trying to visit the url before deploying the servlet, would display 404 not found error.

 

goGet method stub

Visit the servlet url or run the servlet on the server :

http://localhost:8080/MyWebProject/HelloWorldExample

 

Servlet Running on Tomcat

 

You are done with creating , deploying and running a sample Servlet in Tomcat server using Eclipse.

Related Posts

Google-Auto-Suggestion-Box-1024x555

Automation

Write a java program to capture auto suggestions list

Tomcat-deployment

Automation

How to deploy a .WAR file in Tomcat using Eclipse?

ArrayList-in-Java-768x497

Automation

What is an ArrayList?

String-literal-common-pool-1024x586

Automation

What is the difference between String Literal and String Object?

2FA-GitHub-account-1024x363

Automation

How to enable 2FA in GitHub?

Simple-JavaFX-Application-1024x524

Automation

What is JavaFX API?

Navigation

  • Home
  • Selenium
  • JBehave
  • TestNG

Random Questions

  • What is Docker?
  • Setters_and_Getters_JavaBeanHow to generate setter and getter automatically for a Bean?
  • Bubble SortWrite a java program for Bubble Sort?
  • Identity-Matrix-4x4--1024x573Program to generate and print an identity matrix?
  • What is an Eclipse Perspective?
  • Write a program to convert date format from one Timezone to another?Write a program to convert date format from one Timezone to another?
  • Array Reflection in MirrorArray reflection in the mirror java program?
  • 3 tier architectureWhat is Client/Server Architecture?
  • What is GitHub Desktop?
  • Breakpoint Java ProgramHow to debug a Java program using Eclipse IDE?
  • RunConfigurationDemoCan we have multiple main methods in a class in Java language?
  • Implement-Two-Stacks-in-a-Single-Array-1024x552Two Stacks in an Array
  • String-literal-common-pool-1024x586What is the difference between String Literal and String Object?
  • Commons CLI libraryWhat is Commons CLI library?
  • Distance converter java programWrite a program to convert distance in kilometers to miles and vice versa?
TestingDocs.com
  • Home
  • Selenium
  • JBehave
  • TestNG
© 2016 - 2019 TestingDocs.com All Rights Reserved
TestingDocs.com

TestingDocs.com

Links

  • Home
  • Selenium
  • JBehave
  • TestNG

Search