Search This Blog

About Me

My photo
Blog Written By Dr. Digvijaysinh D Virpura

Monday, August 31, 2020

Steps to Create First Servlet

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


Step 1: First create one HTML File and add following code

  <html>
<head>
<title>Introductions</title>
</head>
<body>
   
<form action="FirstServlet" method="GET">

    If you don't mind me asking, what is your name?
<input name="name" type="TEXT" /><p>
<input type="SUBMIT" />
</p></form>
</body>
</html>

 Step 2: Now create one servlet file as FirstServlet and add following code

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

public class FirstServlet extends HttpServlet {

  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {

    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    String name = req.getParameter("name");
    out.println("<HTML>");
    out.println("<HEAD><TITLE>Hello, " + name + "</TITLE></HEAD>");
    out.println("<BODY>");
    out.println("Hello, " + name);
    out.println("</BODY></HTML>");
  }

  public String getServletInfo()
  {
    return "A servlet that knows the name of the person to whom it's" +
           "saying hello";
  }
}

 Step 3: Now go to html and run the file.

Create Servlet with doPost method.

 This blog will demonstrate with dopost method in Servlet    

Step 1: First create one html file and add following code in it.

 

<html>
<body>
<form action="HelloFormPost" method="POST">
First Name: <input name="first_name" type="text" />
<br />
Last Name: <input name="last_name" type="text" />
<input checked="checked" name="maths" type="checkbox" /> Maths
<input name="physics" type="checkbox" /> Physics
<input checked="checked" name="chemistry" type="checkbox" />
                                                Chemistry
<input type="submit" value="Select Subject" />
</form>
</body>
</html>

Step 2: Now create one servlet file as  HelloFormPost

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

// Extend HttpServlet class
public class HelloFormPost 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 Post 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\n\n\n"   +"<li><b>Maths Flag : </b>: "  + request.getParameter("maths") +
       "\n" +"<li><b>Physics Flag: </b>: "   + request.getParameter("physics") +
       "\n" +"<li><b>Chemistry Flag: </b>: "
                + request.getParameter("chemistry") + "\n" + "");
  }

 // Method to handle POST method request.
  public void doPost(HttpServletRequest request,
                     HttpServletResponse response)
      throws ServletException, IOException {
     doGet(request, response);
  }

}

 Step 3: Now go to html and run the file.

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