Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Servlet Servlet - JSP Programming
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Kiran
Servlet - JSP Programming
2 Answer(s)      4 years and 7 months ago
Posted in : JSP-Servlet


Hi,

I'm writing a jsp page as shown below. I have to buttons of type submit. Any time only one is selected(either add to cart or checkout). I'm programming in a servlet getting the input parameters. But when i try the command request.getparameter to know which submit button is clicked, I'm getting an error. I found that, if i click the "add to cart" button it is showing error with "checkout" parameter and viceversa. The starting of my servlet code is also pasted below. please let me know how to solve it.



<form method="post" action="updateCart">

<table border="1">
<tbody>
<tr>
<td><input type="checkbox" name="chair" value="chair"></td>
<td>Chair</td>
<td>$10</td>
<td><input type="text" name="chair1" size="5"></td>
</tr>
<tr>
<td><input type="checkbox" name="table" value="table"></td>
<td>Table</td>
<td>$20</td>
<td><input type="text" name="table1" size="5"></td>
</tr>
<tr>
<td><input type="checkbox" name="lamp" value="lamp"></td>
<td>Lamp</td>
<td>$30</td>
<td><input type="text" name="lamp1" size="5"></td>
</tr>
</tbody>
</table>
<p><input type="submit" name="add_to_cart" value="Add2Cart"> <input
type="submit" name="checkout" value="checkout"></p>
<p><br></p>
<p></p>
</form>


SERVLET CODE
--------------------------
1)String add_to_cart = null;
2)String checkout = null;

3)add_to_cart=request.getParameter("add_to_cart").toString();

4)checkout = request.getParameter("checkout").toString();

5)if (add_to_cart.equals("Add2Cart")){


......

If i click add to cart button error is with line 4. if i click checkout error is with line 3.

Advertisement
View Answers

June 9, 2010 at 1:47 PM


Hi Friend,

Give same name to both the buttons.

1)form.jsp:

<form name="form" method="post" action="../UpdateCart" >

<table border="1">
<tbody>
<tr>
<td><input type="checkbox" name="chair" value="chair"></td>
<td>Chair</td>
<td>$10</td>
<td><input type="text" name="chair1" size="5"></td>
</tr>
<tr>
<td><input type="checkbox" name="table" value="table"></td>
<td>Table</td>
<td>$20</td>
<td><input type="text" name="table1" size="5"></td>
</tr>
<tr>
<td><input type="checkbox" name="lamp" value="lamp"></td>
<td>Lamp</td>
<td>$30</td>
<td><input type="text" name="lamp1" size="5"></td>
</tr>
</tbody>
</table>
<p><input type="submit" name="Direction" value="Add2Cart"> <input
type="submit" name="Direction" value="checkout"></p>
<p><br></p>
<p></p>
</form>

2)In servlet, put the following code:

String button=request.getParameter("Direction");
if(button.equals("Add2Cart")){
out.println("Add2Cart-----------");
}
else if(button.equals("checkout")){
out.println("checkout-----------");
}

Hope that it will be helpful for you.
Thanks



June 9, 2010 at 1:54 PM


awesome.. thanks a lot.. it worked..


Related Tutorials/Questions & Answers:
java servlet programming - JDBC
java servlet programming  Hii! frnds My reqiurement... programming through servlets Thanx   Hi friend, i am sending multiple...)); // Read from file into servlet output stream ServletOutputStream
java servlet programming - JDBC
java servlet programming  how to insert value in a column of datatype - datetime in sql 2000 using java servlets???? can ne 1 help me out???  Hi neepa, you have to put the datetime in single quotes. For ex
Advertisements
Programming
Programming  Given a number n, write a programming to determine its square root if it is possible, in the contraly case print an appropriate massege on the screen
programming
Java Constructor programming for single and double constructor  write a program which have no argument constructor ,single parameter constructor constructor,double parameter constor,and the now when we create a object
java servlet programming - JDBC
java servlet programming  How to insert the value in a column of data... information: http://www.roseindia.net/jsp/fileupload.shtml Thanks  Hi friend, For read more information: http://www.roseindia.net/jsp
Servlet: What is it
What is Servlet? The Servlet is a class that is written in Java programming.... It interacts with clients via request-response programming model. Servlet... by it. For such applications, Java Servlet technology defines HTTP-specific servlet classes. A web
Java(Servlet) - Servlet Interview Questions
Java Servlet examples  Java and Servlet programming examples
Java Programming
Java Programming  Hi, What is Java Programming? How I can learn Java Programming in one month? Thanks
Web programming
Web programming  Write a program in servlet by using JDBC to display the records of employees from a table called employee
Programming (general)
Programming (general)  I haven't yet started college/university but I'm interested in studying programming. I wondering if programming is difficult or confusing with all the codes you have to learn? Also how many languages would
programming concept
programming concept  Write a program that allows the user to input a total dollar amount for an online shopping order and computes and outputs the shipping cost based on the following schedule
programming of foxpro
programming of foxpro  hello sir , sir i am use the foxpro . but i know about c . sir how can i learn fox pro . i know about something about foxpro
C Programming
C Programming  hi,how can i write a program in C programming to place reservation in air plane from the menu let the user to inter his/her gender(W or M)and draw seats by using ascii codes and choose where he/she will sit
programming error
programming error  I got an error when i did the following program the prg. is to "select the name from database when the age is given for the respective name" error i got is:SQL exception please give me the solution by providing
Socket Programming
Socket Programming  How to connect a computer through IP only and check whether it is up and running? I can't see any method in Socket programming where I can pass only IP address as argument. I need the pass port number also
Servlets Programming
how many times a servlet has been accessed. but after executing this i am getting... value once and then want to know how many times it(servlet) has been called. How... times the servlet has been accessed. /** * Servlet implementation class
 

 

 

DMCA.com