STEP 6: Create and execute a Basic Servlet
  1. Same as in Step 5, go to the Filesystems window and then right-click on the

    C:\Documents and Settings\  choose New -> All Templates


  2. Choose Jervlet under node JSPs in JSPs&Servlets

    3.  Accept the default setting, you may change the Class Name

   

   4. Click Next and then Finish

   5. Notice now the servlet is created.

  

6. Replace the default code after line package com.mycompany; with the following codes:

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

public class NewServlet extends javax.servlet.http.HttpServlet {

public void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException, java.io.IOException {

performTask(request, response);

}
public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException, java.io.IOException {
performTask(request, response);

}
public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
{
try
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Simple Servlet");

}
catch(Throwable theException)
{
//theException.printStackTrace();
}
}
}

7. Compile and Execute it, you should see the Servlet working: