5-steps for creating and running a Java Servlet program

5-steps for creating and running a Java Servlet program

This tutorial gives step-by-step guide for creating and running a servlet program on windows platform.

  • Step 1: Create an application directory parellel to the ROOT directory under /webapps directory, here “demoApp”. Also create “WEB-INF” folder under recently created application directory and a “classes” folder under “WEB-INF” directory.
  • Step 2: Create a new java file, here “demoServlet.java”, and write this code inside this file:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class demoServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
res.setContentType(“text/html”);
PrintWriter out = res.getWriter();
String myName = req.getParameter(“myname”);
out.println(“<html>”);
out.println(“<head><title>Hello “+myName+”</title></head>”);
out.println(“<body>”);
out.println(“Welcome “+myName+”!”);
out.println(“</body> </html>”);
}
}

  • Step 3: Create a web.xml file under /WEB-INF directory. Put these configuration under web.xml file:
  • Step 4: Compile the java class created in 2nd step using command prompt (javac demoServlet.java) from “WEB-INF/classes” folder.
  • Step 5: Browse “http://localhost:8080/newApp/demoServlet?myname=user” from browser. You will see an output, something like this screen-shot:


Here, I have assumed that you have Java Development Kit (JDK) and Apache Tomcat Server installed and configured on your machine.

8 thoughts on “5-steps for creating and running a Java Servlet program

  1. Pingback: Thanks for the great post...

  2. Pingback: Really great post

  3. Pingback: Jackson Jones

  4. Pingback: Julia Jones

  5. Pingback: Josh Richards

  6. Pingback: taxi miami

  7. Pingback: zquiet

  8. Pingback: Laser

Leave a Reply

Your email address will not be published. Required fields are marked *