Question: Servlet - JSP Programming

Ads

 
 
 

Share on Google+Share on Google+

Kiran
Servlet - JSP Programming
2 Answer(s)      6 years and 11 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.
Ads
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..

Ads









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
network programming
network programming   Tutorial for Network programming and administration
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
Tutorials   
Java Spring Hibernate Struts Training fat image from db fat image from db fat image from db fat image from db unable to get value returned from javascript variable in page How to change image size before adding to mysql using jsp Design a registration.jsp page with input for user registration details Cart Quantity jsp servlet coding issue servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? image pdf marking pdf marking Multiple image upload using JSP Read code from excel sheet and upload into database using JSP Student java about search engine Convert BLOB to PDF and save it to a location how to copy files from remote to local machine using SFTP SSL in java? Like Team viewer Socket and Servert -Jsp how to copy files from remote location to local using java? How To Store Multilple Images In MySQL database dynamically with image uploader and then retrieve them in image gallery using java how to create SOAP based web service in java? how to fetch data from mysql database table and draw a bar chart on that data using in jsp how to create users in LDAP using java? how to authenticate user using TIVOLI api in java? how to create users using IBM Tivoi API in jsp/servelet? Java/J2ee Please explain this code as many packages and classes are unknown to me .! how to download the uploaded folder files using jsp Login Query image is display from path of mysql database please reply the code to store dynamic fields in database? Reloading issue in jsp How to get Button Id ? calling one jap page from another jsp page login login Problem to get connection from DAO class to JDBC Page Loading Time Fix table's column's name row(1st row of the table) so that it does not move up when the table is scrolled up to view more rows below Sending hidden values

Ads

 
Advertisement null

Ads