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 1 Comment

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

 

What you are thinkig....

One Response 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.

If you want a pic to show with your comment, go get a gravatar !
Please post your questions on Java4s Answers forum

Name*
Ask a Question ?
or
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 Java4s.com
Tutorials Online :
spring Hibernate struts Json Ajax Log4j Log4j
coreJava Servlets


UpComing :
Jsps,Web Services
Java4s is optimized for learning java technologies, all the examples in this site are constantly reviewed to avoid errors. While using this site you agree to have read and accepted our terms of use and privacy policy
Especially i have prepared this blog by keeping fresher's in mind, however it will be very useful for real time developers too.


© 2014 Java4s All rights reserved. | strPro4Tut v(2.0) Theme designed by Team Java4s.