Home Servlets Passing Parameter Using Html Form
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Passing Parameter Using Html Form

Advertisement
This is a very simple example in which we are going to display the name on the browser which we have entered from the Html page.

Passing Parameter Using Html Form

     

This is a very simple example in which we are going to display the name on the browser which we have entered from the Html page. 

To get the desired result firstly we have to make one html form which will have only one field named as name in which we will enter the name. And we will also have one submit button, on pressing the submit button the request will go to the server and the result will be displayed to us. 

In the servlet which will work as a controller here picks the value from the html page by using the method getParameter().  The output will be displayed to you by the object of the PrintWriter class. 

The code of the program is given below: 

<html>

<head>
<title>New Page 1</title>
</head>

<body>

<h2>Login</h2>
<p>Please enter your username and password</p>
<form method="GET" action="/htmlform/LoginServlet">
  <p> Username  <input type="text" name="username" size="20"></p>
  <p> Password  <input type="text" name="password" size="20"></p>
  <p><input type="submit" value="Submit" name="B1"></p>
</form>
<p>&nbsp;</p>

</body>

</html>

LoginServlet.java

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

public class LoginServlet extends HttpServlet{
  public void doGet(HttpServletRequest request,
  HttpServletResponse response
)
 
throws ServletException, IOException {
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  String name = request.getParameter("username");
  String pass = request.getParameter("password");
  out.println("<html>");
  out.println("<body>");
  out.println("Thanks  Mr." "  " + name + "  " 
 
"for visiting roseindia<br>" );
  out.println("Now you can see your password : " 
 
"  " + pass + "<br>");
  out.println("</body></html>");
  }
}

 web.xml file for this program:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--<!DOCTYPE web-app
 PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd"> -->

<web-app>
 <servlet>
  <servlet-name>Hello</servlet-name>
  <servlet-class>LoginServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>Hello</servlet-name>
 <url-pattern>/LoginServlet</url-pattern>
 </servlet-mapping>
</web-app>

 The output of the program is given below:

 

Download this example:

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 12, 2008

Ask Questions?    Discuss: Passing Parameter Using Html Form   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Frackson Hector LUBINGU
December 29, 2011
Java Servlet

Hi. Have you got these tutorials in PDF format or in book format?? Let me know
DMCA.com