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





  • 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


Java/J2EE Developer & Web Designer, founder of Java4s.com. Sign-up for the email newsletter for your daily dose of Java tutorials.
Email: sivateja@java4s.com


11 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

  3. Hi Sivateja,

    I’m one of your site follower, first of all thanks for providing valuable information for us.

    I as it is done code in eclipse as you said above but i got error did not understand. Below posting the error and expecting a solution from you.

    HTTP Status 405 – HTTP method GET is not supported by this URL


    type Status report

    message HTTP method GET is not supported by this URL

    description The specified HTTP method is not allowed for the requested resource

  4. Hemant Bankar says:

    In market what is best understandable java servlet reference book…please give me idea……so i can refer it….

  5. surender says:

    Hi First of all thanks a lot for sharing this valuable info its really great site for the freshers.Here i have one doubt is Actin Servlet is single ton or not?

    Plz clarify in this regard…thanks a lot in advance

    Surender Y

  6. What about if user will provide non digit character in form..? Interesting, ya?

  7. Ramakrishna says:

    Hi Sivateja,

    Your tutorials very easy and understandable.I have one doubt,is that possible to define two url patterns for one servlet in web.xml,if yes, how it will take.

    Thanks in Advance

  8. Akshat says:

    What if i give input type=”number” and not text?

  9. Sandeep says:

    Hi Sivateja,

    Thanks for your awesome tutorials.Its easy to understand and implement. However for the above example I’m getting an error – HTTP Status 405 – HTTP method GET is not supported by this URL. I imported the downloaded project and tried. I’m using latest version of eclipse and JDK.

  10. Srikanth says:

    index.html is not showing as default page how to set index.html as default

  11. Abhilash G N says:

    what if the input type=”number”, how do we need to get this paramater


By posting your answer, you agree to our comments policy.
© 2010 - 2016
Java4s - Get It Yourself.
The content & design is copyrighted to Sivateja Kandula, it may not be reproduced on other websites.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs

Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links
Current & UpComing Tutorials
  Web Services [ SOAP ], JSP
Facebook_Java4s  Twitter_Java4s  GooglePlus_Java4s  Youtube_Java4s 
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2016 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.