Thursday, September 16, 2010

Calculator Servlet using Radio Buttons

Design HTML or JSP Page to accept user input :

Enter the following code snippet in the  body  tag:

<form action="calculation">
   <input type="text" name="fnum"/>
   <br/>
   <input type="text" name="snum"/>
   <br/>
   Select Operation
   <br/>
   <input type="radio" name="calc" value="Add"/>Add
   <input type="radio" name="calc" value="Sub"/>Subtract
   <input type="radio" name="calc" value="Div"/>Divide
   <input type="radio" name="calc" value="Multi"/>Multiply
   <br/>
   <input type="submit" value="Calculate" name="submit"/>
</form>


Retrive user input & display the output of the calcultion:

Enter the following code in the doGet() of the servlet:


//assuming request as the object of the HttpServletRequest Class.
//retriving & storing the values from the textboxes into the String Variables.
String n1 = request.getParameter("fnum");
int
num1 = Integer.parseInt(n1); //Converting String into Integer Variable
String n2 =
request.getParameter("snum");
int
num2 = Integer.parseInt(n2); int ans=0;

//performing calculation according to the selection made from the Radio Buttons named "calc".
if(request.getParameter("calc").equals("Add"))
ans = num1+num2;
if(
request.getParameter("calc").equals("Sub"))
ans = num1-num2;
if(
request.getParameter("calc").equals("Div"))
ans = num1/num2;
if(
request.getParameter("calc").equals("Multi"))
ans = num1*num2;


//assuming response as the object of the HttpServletResponse Class.
//displaying output to the user 
response.getWriter().println(ans); 
 
 
 

22 comments:

  1. This is quite educational arrange. It has famous breeding about what I rarity to vouch. Colossal proverb. This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved. This arrange is synchronous s informative impolite festivity to pity. I appreciated what you ok extremely here.
    Data Science training in rajaji nagar
    Data Science with Python training in chennai
    Data Science training in electronic city
    Data Science training in USA
    Data science training in pune

    ReplyDelete
  2. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.
    Most of ideas can be nice content.The people to give them a good shake to get your point and across the command.

    Java training in Chennai

    Java training in Bangalore

    Java online training

    Java training in Pune


    ReplyDelete
  3. Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting.So know it's helpful.
    python Online training in chennai
    python training institute in marathahalli
    python training institute in btm
    Python training course in Chennai

    ReplyDelete
  4. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
    AWS Training in Bangalore with Placements | AWS Training in Bangalore Cost
    AWS Training in Pune With Placement | AWS Devops Training in Pune
    AWS Online Training | AWS Online Training Cost
    AWS Training in Bangalore cost| Aws training in Bangalore Besant Technologies

    ReplyDelete
  5. Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision. 
    Microsoft azure training in Bangalore
    Power bi training in Chennai

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Math is intense however it's still do-capable in the event that you invest enough energy and exertion on it. These classes are presumably not the most arresting ones for most of understudies, however they give you a thought of what you are relied upon to know and how much.
    calculus

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Amazing, this is great as you want to learn more, I invite to This is my page. 120 kg to lbs

    ReplyDelete
  11. Really i appreciate the effort you made to share the knowledge. The topic here i found was really effective...

    Start your journey with Best SAP Course and get hands-on Experience with 100% Placement assistance from experts Trainers @Softgen Infotech Located in BTM Layout Bangalore. Expert Trainers with 8+ Years of experience, Free Demo Classes Conducted.

    ReplyDelete
  12. I just recently discovered your blog and have now scrolled through the entire thing several times. I am very impressed and inspired by your skill and creativity, and your "style" is very much in line with mine. I hope you keep blogging and sharing your design idea


    data science training in chennai

    data science training in velachery

    android training in chennai

    android training in velachery

    devops training in chennai

    devops training in velachery

    artificial intelligence training in chennai

    artificial intelligence training in velachery

    ReplyDelete
  13. You're welcome! I'm glad you found the information helpful. If you have any specific questions about a Full Stack Developer course in Marathahalli
    python training in marathahalli

    ReplyDelete