Newsletter

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

Servlets » on Jan 14, 2013 { 13 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

<font face="verdana" size="2px">
   <form action="sum" method="post">
       Number1:<input type="text" name="n1"><br>
       Number2:<input type="text" name="n2"><br>
       <input type="submit" value="Calculate Sum">
  </form>
</font>

OngetParameter.java

package java4s;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class OngetParameter extends HttpServlet  
{
    protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
	{
		PrintWriter pw=res.getWriter();
		res.setContentType("text/html");

		String n1=req.getParameter("n1");
		String n2=req.getParameter("n2");

		int result=Integer.parseInt(n1)+Integer.parseInt(n2);		
		pw.println("Sum of two numbers " +result);
		pw.close();

	}

}

web.xml

<web-app>

    <servlet>
       <servlet-name>sumOfTwoNumbers</servlet-name>
       <servlet-class>java4s.OngetParameter</servlet-class>
    </servlet>

    <servlet-mapping>
       <servlet-name>sumOfTwoNumbers</servlet-name>
       <url-pattern>/sum</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
       <welcome-file>index.html</welcome-file>
    </welcome-file-list>

</web-app>

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 the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

13 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…

  13. John says:

    Thanks bro……………………..

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

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