wood burning stoves 2.0*
The moose likes JSP and the fly likes How can I catch the  radio button action in jsp? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How can I catch the  radio button action in jsp?" Watch "How can I catch the  radio button action in jsp?" New topic
Author

How can I catch the radio button action in jsp?

Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

actually I am developing a java program using JSP and forms.the first page is used to enter two parameters and then there will a proceed button to go further ahead then second page will display options with radio buttons like add,sub,multiplication,division. Among these radio buttons user need to select one option then he needs to click on result button. According to user selection the result should be diplayed.I have writtenthree jsp programs to meet my requirements named as firstpage.jsp,secondpage.jsp and operations.jsp but I dont no how to catch the action from the user selection and please take a look at my code
firstpage.java

secondpage.jsp

Result.jsp


so please help me out
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

JSP is merely a templating mechanism to create HTML pages to send to the browser. It will be of no help to you in dealing with user interaction with the controls. For that you'll need to employ JavaScript.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
long chen
Greenhorn

Joined: Sep 18, 2009
Posts: 1
second.jsp
<body>

<form action="Result.jsp" method="POST" >
FirstNumber:<input type="text" name="FIRSTNUMBER">

SecondNumber:<input type="text" name="SECONDNUMBER">

ADD:<input type="radio" name="result" value="ADD" >

SUB:<input type="radio" name="result" value="SUB" >

DIV:<input type="radio" name="result" value="DIV" >

MUL:<input type="radio" name="result" value="MUL" >

<input type="submit" value="Result" >

</form>

</body>
</html>

Result.jsp
<body>
<%
String Result = request.getParameter("result");
String s1 = request.getParameter("FIRSTNUMBER");
String s2 = request.getParameter("SECONDNUMBER");
int k=Integer.parseInt(s1);
if (Result.equals("ADD")) {
int add = Integer.parseInt(s1) + Integer.parseInt(s2);
out.println("Result=" + add);
} else if (Result.equals("SUB")) {
int sub = Integer.parseInt(s1) - Integer.parseInt(s2);
out.println("Result=" + sub);
} else if (Result.equals("MUL")) {
int mul = Integer.parseInt(s1) * Integer.parseInt(s2);
out.println("Result=" + mul);
} else if (Result.equals("DIV")) {
int div = Integer.parseInt(s1) / Integer.parseInt(s2);
out.println("Result = " + div);
}
%>
</body>
1. you can only getParameter from pre-page.
2. the radio should have the same name
3. "if" should have {}
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

I really appriciate your help and i have one doubt that is if the values are same in firstpage.jsp and secondpage.jsp then answer is ok but when i change values means that if i entered 3,4 in firstpage.jsp and 4,5 number in secondpage.jsp,then it is taking 4,5 means that it cant not able to get values from firstpage.jsp..am i right? for doing this can we use beans?again thanks for your help
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

Yes, one way to remember the values across requests is to store them away in a bean stored as a scoped variable in the session.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I catch the radio button action in jsp?
 
Similar Threads
how show values in jsp from getting database
JSP <a href> tags breaks page functionality
Tomcat: No Java compiler was found
UrlRewriting+servlets -Error
calendar