Home Servlets Get Parameter Name From Servlet Request

Related Tutorials


 
 

Share on Google+Share on Google+

Get Parameter Name From Servlet Request

Advertisement
This example illustrates about how to get parameter from jsp page in your servlet.

Get Parameter Name From Servlet Request

     

This example illustrates about how to get parameter from jsp page in your servlet. In the jsp (parameter.jsp) page we have taken three input fields having name as firstname, lastname and middle name. We can get values of these fields in our servlet page. When form is submitted the action will call the servlet "GetParameter".

Source code of parameter.jsp:

<%@page language="java" session="true" contentType="text/html;charset=ISO-8859-1" %> 
<b><font color="blue">Please Enter your First Name, Last Name and Middle Name:</font></b><br><br>
<form name="frm" method="post" action="../GetParameter">
  <table border = "0">
  <tr align="left" valign="top">
  <td>First Name:</td>
  <td><input type="text" name ="firstname" /></td>
  </tr>
  <tr align="left" valign="top">
  <td>Middle Name:</td>
  <td><input type="text" name ="middlename" /></td>
  </tr>
  <tr align="left" valign="top">
  <td>Last Name:</td>
  <td><input type="text" name ="lastname" /></td>
  </tr>
  <tr align="left" valign="top">
  <td></td>
  <td><input type="submit" name="submit"/></td>
  </tr>
  </table>
</form>

Running the above jsp program by this url: http://localhost:8080/CodingDiaryExample/JSP/parameter.jsp displays page like below:

Now, the following program illustrates about how to access values ("firstname", "middlename" and "lastname") of jsp file in servlet (GetParameter.java).

Here is the source code of GetParameter.java:

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

public class GetParameter extends HttpServlet {
  public void doPost(HttpServletRequest request,
  HttpServletResponse response
)
  throws IOException, ServletException{
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();  
  String firstName = request.getParameter("firstname");
  String middleName = request.getParameter("middlename");
  String lastName = request.getParameter("lastname");  
  out.println("<b><font color='blue'>Your FirstName is :</font></b>" 
  "<b>"+ firstName +"</b>" "<br>");
  out.println("<b><font color='blue'>Your Middle Name is :</font></b>"  
  "<b>"+ middleName +"</b>" "<br>");
  out.println("<b><font color='blue'>Your Last Name is :</font></b>"  
  "<b>"+ lastName +"</b>");
  }
}

In the above servlet,  get parameter by the  HttpServletRequest object. The getParameter() method returns the value of a request parameter (passed as an argument like: "firstname", "middlename" and "lastname") as a String or null  if the parameter does not exist.

Mapping of servlet (GetParameter.java) in  web.xml file:

<servlet>
  <servlet-name>GetParameter</servlet-name>
  <servlet-class>GetParameter</servlet-class>
</servlet> 
<servlet-mapping>
  <servlet-name>GetParameter</servlet-name>
  <url-pattern>/GetParameter</url-pattern>
</servlet-mapping>

From the above lines in  web.xml we are mapping the servlet with url. 

If we enters the values like in the figure below

then following output will be displayed.

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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: June 26, 2008

Related Tutorials

Discuss: Get Parameter Name From Servlet Request  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com