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.

No comments:

Post a Comment