Servlet - JSP Programming

Servlet - JSP Programming

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.
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
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
Java(Servlet) - Servlet Interview Questions
Java Servlet examples  Java and Servlet programming examples
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
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
Servlet
Servlet  how to navigate one servlet page to another servlet page
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
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
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  i want a program for counting the no of times the servlet has been invoked
Servlet
Servlet  What must be implemented by all Servlets?    The Servlet Interface must be implemented by all servlets
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
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
network programming
network programming   Tutorial for Network programming and administration
Servlet
Servlet  I want to know the steps to write a simple servlet program... .   Hello Friend, Follow these steps: Put servlet-api.jar inside the lib folder of apache tomcat. 1)create a servlet. import java.io.*; import
Servlet
Servlet   Why is Servlet so popular?   Because servlets are platform-independent Java classes that are compiled to platform-neutral byte code that can be loaded dynamically into and run by a Java technology-enabled Web
servlet
servlet  i want to use servlet application on my web page then what should i do. I have already webspace.   Hi Friend, Please visit the following link:ADS_TO_REPLACE_1 Servlet Tutorials Thanks
servlet
servlet  how to read a file from different folder using filereader in servlet   Hello Friend, Please visit the following link:ADS_TO_REPLACE_1 http://www.roseindia.net/servlets/servlet-read-file.shtml Here you
Servlet
Servlet  Hi, Can any one please expalin me below topics SERVLET ENGINE 2.WHY SUPER.INIT(); Thanks alot in advance!! Regards: Akash
servlet
servlet   Dear Deepak, is it compulsary to write the sevice() becoz i ve seen some example which does not ve sevice()..is it tue? plz replay me with thanks praveen
servlet
servlet  plz can anyone give me the link of javax library jar file. i badly need that. thanks in advance   Please visit the following link: Download Servlet API
servlet
servlet  I want a fully readymade project on online voting system with code in java servlet and database backend as msaccess.can u plz send me as soon as possible
Servlet
the same error <web-app> <servlet> <servlet-name>InsertServlet</servlet-name> <servlet-class>InsertServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>
Java Programming
Java Programming  Hi, What is Java Programming? How I can learn Java Programming in one month? Thanks
servlet
servlet  hi sir,this is ashok.i installed tomcat 6.0 and jdk-150.i checked the server it's working but when execute servlet program for the .class file it shows the errors javax.servlet is not exist.what can i do,please tell me
Servlet
Servlet  What is the actual use of servlets in programming?  Hi Friend, Servlets are the server side component. It acts as a controller. It can handle multiple requests concurrently, and can synchronize requests.The
servlet
servlet  How do we define an application level scope for servlet?   Application scope uses a single namespace, which means all your pages should be careful not to duplicate the names of application scope objects
servlet
servlet   try { con=DaoPack.createConnection(); } catch(Exception e) { e.printStackTrace(); } if(request.getParameter("addproduct")!=null) { ProductBean bean=new ProductBean
servlet
servlet   try { con=DaoPack.createConnection(); } catch(Exception e) { e.printStackTrace(); } if(request.getParameter("addproduct")!=null) { ProductBean bean=new ProductBean
servlet
com.ilp.tsi.pm.services.StockService; /** * Servlet implementation class AddServlet1 */ //Servlet for Adding the stock public class AddStockServlet extends...; import com.ilp.tsi.utils.*; /** * Servlet implementation class Changepwd
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
servlet
com.ilp.tsi.um.bean.BankBean; import com.ilp.tsi.um.service.BankService; /** * Servlet
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 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
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 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

Ads