Get all updates of Java4s
Follow Us:

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

Servlets » On Jan 14, 2013 { 12 Comments } 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

You Might Also Like

  ♦ About Author ♦

Java4s_Author
sivateja@java4s.com
Full Stack( Java/J2EE & UI/UX ) Developer, founder of Java4s.com.
Sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

12 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

    Regards
    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

  12. kishoreReddy says:

    Hi Abhilash,
    simple how to we are getting string values like that only we will get integers directly i tried …it works try once ..then send me Errors…

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
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_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
Upcoming
  Web Services [ SOAP ], JSP
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.