Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  
 

Servlet - JSP Programming

Ads

 
 

Share on Google+Share on Google+

Kiran
Servlet - JSP Programming
2 Answer(s)      7 years 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
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
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
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
servlet
servlet  I have to do a project for my web programming class where we... servlet file which prints out the user's inputs. I need to use the post method to pass the data from html to the java servlet and also use both doGet
servlet
servlet  I have to do a project for my web programming class where we create a html file that contains four text boxes, 'Watch me', 'Color' and 'size... to servlet. So, if anyone can help me so I can see how to connect the two I would
Servlet
Servlet  What is Servlet
network programming
network programming   Tutorial for Network programming and administration
Servlet
Servlet  how to navigate one servlet page to another servlet page
Java Programming
Java Programming  Hi, What is Java Programming? How I can learn Java Programming in one month? Thanks
servlet
servlet  How many times the servlet is accessed
servlet
servlet  is there any way to include pdf's in servlet
servlet
servlet  what are the methods and interfaces in the servlet api ?   Servlet Tutorials
servlet
servlet  what are the all necessary configuration to run a servlet
servlet
servlet  how to interact with a servlet from a swing program
servlet
servlet  I designed 1 html form & a servlet but when I click on form I don't get output of servlet Please help
servlet
servlet  I designed 1 html form & a servlet but when I click on form I don't get output of servlet Please help
servlet - Servlet Interview Questions
servlet  sir, while performing the servlet programming why we use WEB-INF in capital letters and lib and classes folders are in small leeters
r programming
r programming  Hi, How r programming is playing a big role in the Big Data time? These days companies are employing programmers with R Programming... in the market. Whey r programming is so important? Thanks   R
Web programming
Web programming  Write a program in servlet by using JDBC to display the records of employees from a table called employee
Servlet
override Service method in the servlet when you extend GenericServlet to create servlet as it is mandatory to override it. But, when you extend HttpServlet to create a servlet then you can't override service method as there is a need to override
Servlet
Servlet  What must be implemented by all Servlets?    The Servlet Interface must be implemented by all servlets
servlet
servlet  i want a program for counting the no of times the servlet has been invoked
servlet
of the Servlet API. It contains the classes necessary for a standard, protocol-independent servlet. Every servlet must implement the Servlet interface in one
servlet
servlet  how to create a login form using servlet using submit,edit delete button
servlet
servlet  dear sir servlet and html not run on eclips plz help me
servlet
servlet  can i stoar record in variable which selected from table in servlet
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