Form data and servlets


Simple program to demonstrate accessing a form data in a servlet

 

// Form to submit user name

 

<!-- html forms -->

 

<html>

 

<head>

     <title>HTML Forms and Servlets</title>

     <style>

          label

          {

              font-family:vivaldi;

              font-size:x-large;

              letter-spacing:2px;

              line-height:40px;

          }

     </style>

</head>

 

<body>

     <form method="get" action="http://localhost:8088/anu/formdata">

 

          <center>

          <h2>HTML and Servlets</h2></br>

         

          <label>First Name  </label>

          <input type="text" Name="UsrName" tabindex=1/>

          </br> </br>

         

          <button class="btn" type="button " >Click me!!! </button>

          </center>

             

     </form>

</body>

</html>

 

 

// Servlet to access form data and display a welcome message

 

import javax.servlet.http.*; 

import javax.servlet.*; 

import java.io.*;

import java.util.*; 

 

public class formdata extends HttpServlet

{ 

     public void doGet(HttpServletRequest req,HttpServletResponse res)

throws ServletException, IOException 

     { 

          //setting the content type

          res.setContentType("text/html");

 

          //create the stream to write the data 

          PrintWriter pw=res.getWriter();

            

          String N = (String)req.getParameter("UsrName");

         

          //writing html in the stream 

          pw.println("<html><body>"); 

         

         

          if(N.length()==0)   

              pw.println("<H2>Welcome Guest.</H2>");

          else

              pw.println("<H2>Welcome "+N+".</H2>");

         

          pw.println("</body></html>"); 

         

          //closing the stream

          pw.close(); 

     }

} 

 

 

   

 

 

 

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu