You can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Example of request.getparameter(), Retrieve Parameters from HTML Form

Servlets » On Jan 14, 2013 By Sivateja

Let us see how to use request.getParameter() method in the servlet class, to retrieve the input values from HTML page.  Friends it is base concept on retrieving the input data, so observe very carefully, also this is the first example we are seeing on retrieving the values form the input pages.

Hmmm let us calculate sum of two numbers [ by taking these numbers from the input HTML page ]

Directory Structure

Files Required

  • index.html
  • OngetParameter.java
  • web.xml

index.html

OngetParameter.java

web.xml

Explanation:

  • Once we run the application, browser will display index.html as front page.  Because we have given index.html in welcome file list [ line number 14 in web.xml ]
  • Enter input values and press ‘Calculate Sum‘ button
  • Now come to OngetParameter.java > just retrieve the input values like req.getParameter(“n1″) & req.getParameter(“n2″)
  • getParameter() is the method in request object, which returns String value always
  • So convert that string output to Integer [ line number 21]
  • Integer.parseInt(-String-) gives integer value
  • Output

Output






By [ Java/J2EE Developer & Web Designer ]
Founder of Java4s.com - Indian, lives in United States. You can contact me at sivateja [at] java4s.com
Follow me on


Comments

2 Responses to “Example of request.getparameter(), Retrieve Parameters from HTML Form”
  1. hareesha says:

    Hi Teja,

    I have one doubt here ,what if we have two welcome files which have same parameters n1 and n2.From which file the servlet retrievs the req parameters.And in this particular example how the control goes from welcome file to java class defined, because we didnt specify the form submit path.

  2. RameshNaidu says:

    i have one doubt servlet is a singleton or not….i am very confuse please tell me……i am searched so many websites but some websites tell it’s singleton and some web site tells it’s not singleton….please clarify my doubt

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links
Current & UpComing Tutorials
Sitemap :
spring Hibernate struts Json Ajax Log4j Log4j coreJava Servlets Web Services


Upcoming
  Web Services [ In Progress ], JSP
Facebook_Java4s  Twitter_Java4s  GooglePlus_Java4s  Youtube_Java4s 
© 2010 - 2014 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.