Extracting HTML form parameters Example

Extracting HTML form parameters Example


Posted in : Java Posted on : June 7, 2011 at 7:00 PM Comments : [ 0 ]

In this section we will discuss how can the html form parameter is extracted from HttpServletRequest.

Extracting HTML form parameters Example

In this section we will discuss how can the html form parameter is extracted from HttpServletRequest.

  • We will first create a html page
  • Then we will develop a java program where we will create an object of HttpServletRequest to extract the html form parameter.
  • Next edit the web.xml file.
  • Then finally run the program on server.

The following program describes that how a client enters its query and how the server responds as their query.

Extract.html

<html>
<head>
<title>Show UserId and Password</title>
</head>
<body bgcolor="yellow">
<h2>
<center>Sign In</center>
</h2>
<h3>Please Enter your information in their respective column</h3>
<form method="get" action="/bipul/servlet/ExtractHtmlForm">
<p><font color="red">Name <input type="text" name="name"></input></font></p>
<p><font color="red">UserId <input type="text" name="uid"
size="20"></font></input></p>
<p><font color="red">Password <input type="password"
name="pswrd" size="6"></font></input></p>
<p><input type="submit" value="submit"></input></p>
</form>
</body>
</html>

ExtractHtmlForm.java

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

public class ExtractHtmlForm extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String name, id, psw;
res.setContentType("text/html");
name = req.getParameter("name");
id = req.getParameter("uid");
psw = req.getParameter("pswrd");
PrintWriter pw = res.getWriter();
pw.println("<html>");
pw.println("<body bgcolor=red>");
pw.println("<h3>");
pw.println("Hello " + name + "<br>");
pw.println("Your UserId is = " + id + "<br>");
pw.println("your Password is = " + psw);
pw.println("</h3>");
pw.println("</html>");
pw.println("</body>");
}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<servlet>
<servlet-name>bipul</servlet-name>
<servlet-class>ExtractHtmlForm</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>bipul</servlet-name>
<url-pattern>/servlet/ExtractHtmlForm</url-pattern>
</servlet-mapping>
</web-app>

Output :

 

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics