Getting Request from Client

Getting Request from Client


Posted in : Servlet Posted on : October 30, 2010 at 6:19 PM Comments : [ 0 ]

In this tutorial you will learn how to get request patameters from client

Getting Request Parameter From Client

To get parameters from client side getParameter("") method HttpServletRequest interface is called. The argument of this method is the name of your html input box. An example is given below.

home.html

<HTML>
<HEAD>
<TITLE>Getting Parameter Example</TITLE>
</HEAD>
<BODY bgcoclor=megenta>
<form method="get" action="http://localhost:8080/param">
<center>
<table border=1>
<tr><td>Enter Your Name :</td>
<td><input type="text" name="userId" value=""></td>
</tr>
<tr><td>Enter Your PassWord :</td>
<td><input type="text" name="userPassWord" value=""></td>
</tr>
<tr><td><input type="submit" name="Submit" value="Submit"></td>
<td><input type="reset" name="reset" value="Refresh"></td>
<table>
</center>
</form>
</BODY>
</HTML>

GettingRequestFromClient.java

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GettingRequestFromClient extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)throws 
IOException,ServletException{
PrintWriter out=response.getWriter();
response.setContentType("Text/Html");
String userId=request.getParameter("userId");
String userPassWord=request.getParameter("userPassWord");
out.println("<html>");
out.println("<body bgcolor=pink >");
out.println("<h1> Getting Parameter from User Example</h1>");
out.println("User Id is -" +userId);
out.println("User PassWord is -" + userPassWord);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws 
IOException,ServletException{
doGet(request,response);
}
}
When you run this application it will display message as shown below:

 
 

Download this example code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics