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



Home Answers Viewqa JSP-Servlet Servlet - JSP Programming

Have Programming Question? Ask it here!
 
 
 


Kiran
Servlet - JSP Programming
2 Answer(s)      4 years and 2 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
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
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
Java Programming
Java Programming  Hi, What is Java Programming? How I can learn Java Programming in one month? Thanks
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  What is Servlet
Programming
Java NotesProgramming Here are some tips on making programming student problems easier. Start from an existing, running, program. If you can find... of the basic elements of Extreme Programming (a much hyped, but good
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  how to navigate one servlet page to another servlet page
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
servlet
servlet  is there any way to include pdf's in servlet
servlet
servlet  How many times the servlet is accessed
servlet
servlet  what are the methods and interfaces in the servlet api ?   Servlet Tutorials
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
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
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
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
programming c#
programming c#   create and make use of at least one class with appropriate instance methods to solve the programming problem. use console-based menus to navigate between the functionality offered by the application. at least one
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
Programming Assignment
Programming Assignment  Hey there, We were given an assignment to write a program to read 10 student marks from the keyboard without using an Array (only using switch statements, if statements or loops). As each mark is input
programming questions
programming questions  this is my assignment questions please help me... the programs in c# language Describe the following with respect to creating Web Forms in .Net environment: a. Web Form Life Cycle b. Creating a Web Form
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
Advertisements
 

 

 

DMCA.com