Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Get Parameter Name From Servlet Request 
 

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

                         

» View all related tutorials
Related Tags: java c database web jsp file ide browser data redirect servlet view user link page vi new this id create

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.