Search This Blog

About Me

My photo
Blog Written By Dr. Digvijaysinh D Virpura

Monday, August 31, 2020

Configure Servlet in NetBeans. Steps to Create First Servlet.

 This blog will help you to create your first servlet program and also explain the steps to create it.

Step 1: First we required a server on which we can run our servlet application. One can install Glassfish server or Apache Tomcat Server.

To Add New Server follow these steps.

1. Go to services ->Right Click ->Add Server

2.  Choose a Server which you wan to install

 

3. Click Download Now

 

 

Step 2:   Go to you project -> New Project->Java Web->Web Application

 

Now give the name of project.

 

Now select the Server which you have installed on which we can run our first servlet application

 

Now Click->Next and then Finish

Now you can see your newly created project folder

 

 
Step 3: Now we will create a new program file.

Go to WebPages->RightClick->New->HTML


 Now give file name as first



Step 4: Now add following code in the First.html file

 

 Now go to SourcePackage->RightClick->New->Servlet

 

 

Add Following Code in HelloForm.Java

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

// Extend HttpServlet class
public class HelloForm extends HttpServlet {

  // Method to handle GET method request.
  public void doGet(HttpServletRequest request,HttpServletResponse response)
            throws ServletException, IOException
  {
      // Set response content type
      response.setContentType("text/html");

      PrintWriter out = response.getWriter();
      String title = "Using GET Method to Read Form Data";
      String docType =       "\n";
      out.println(docType +"\n" + "First Name: "
              +request.getParameter("first_name") + "\n"
              + "Last Name: " + request.getParameter("last_name")
              + "\n" + "");
  }
}

 

 
Step 5: Now go to First.html file 

RightClick->Run


No comments:

Post a Comment