Servlet-JSP population

Servlet-JSP population

In refernce to this Question
http://www.roseindia.net/answers/viewanswers/9843.html

I reduced my servlet code and JSP code as shown below. I have one problem. If I check all the items i'm displaying in my jsp page my code is working fine. If I dont check any of the item in my table in jsp page, it is throwing me an error.

Please let me know how to change my code.

ServletCode:
------------------------------------
package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
* Servlet implementation class updateCart
*/
public class updateCart extends HttpServlet {



private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public updateCart() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session = request.getSession();

String paynow =null;
paynow = request.getParameter("direction").toString();
if(paynow.equals("PayNow")){

RequestDispatcher rd;
rd = request.getRequestDispatcher("/thankyou.jsp");
rd.forward(request,response);

}


String edit_to_cart = null;
edit_to_cart = request.getParameter("direction").toString();




String add_to_cart = null;
String checkout = null;
ArrayList <Integer> total = new ArrayList <Integer>();

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


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

//PrintWriter pw = response.getWriter();
//pw.println(add_to_cart);

if (add_to_cart.equals("Add2Cart")||edit_to_cart.equals("EditCart")){
String[] checkValues=request.getParameterValues("box");
int i=0;

for(String Value:checkValues){
int chair_num;

String number =request.getParameter("number");
chair_num = Integer.parseInt(number);
String price = request.getParameter("price");
int price_num = Integer.parseInt(price);
total.add((chair_num*price_num));
i++;


session.setAttribute("c1", chair_num);
}



RequestDispatcher rd;
rd = request.getRequestDispatcher("/cartupdate.jsp");
rd.forward(request,response);

}



if (checkout.equals("checkout1")){


session.setAttribute("efg",total);


RequestDispatcher rd;
rd = request.getRequestDispatcher("/checkout.jsp");
rd.forward(request,response);



}
}
}



JSP CODE:
-------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@page
language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@page import ="java.util.ArrayList" %>
<head>
<title>cartupdate</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<%@ include file="header.jsp"%>
<%@ include file="left.jsp"%>

<style>
div.form {
float: center;
width: 30%;
height:50%;
margin-right: auto;
}

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


<%
Class.forName("oracle.jdbc.driver.OracleDriver");

String serverName = "127.0.0.1";
String portNumber = "1521";
String sid = "orcl";
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
String username = "scott"; String password = "tiger";
Connection connection = DriverManager.getConnection(url, username, password);

Statement statement = connection.createStatement() ;
ResultSet rs;
rs = statement.executeQuery("select * from cartlist");
int i=1;
out.println("connection made");%>
<TABLE cellpadding="5" border="1">
<tr>
<td>&nbsp</td>
<td>Item</td>
<td>Price</td>
<td>Units</td>
</tr><%

while (rs.next()){


%>
<tr><td><input type="checkbox" name="box" value= "<%=i%>"/></td>
<td><input type="text" name= "name" value="<%=rs.getString(1)%>" readonly/></td>
<td><input type="text" name="price" value="<%=rs.getString(2)%>" readonly/></td>
<td><input type="text" name="number" /></td>
</tr>
<%i++;}%>


</table>
<p><input type="submit" name="direction" value="Add2Cart">
<input type="submit" name="direction" value="checkout1"><b>Click checkout once you Add2Cart</b> </p>
<p><br></p>
<p></p>
</form>

</body>

</html>
View Answers









Related Tutorials/Questions & Answers:
Servlet-JSP population - JSP-Servlet
Servlet-JSP population  Hi, I got my code worked. now I have another task. My jsp page on submit gets redirected to another servlet update cart... with textfields also. how the loop in the servlet should go to check all the boxes
Servlet-JSP population - JSP-Servlet
Servlet-JSP population  Hi, this is regarding my previous question... and display in the next page. I changed my servlet code something like this but i find some problem retrieving the values from the jsp page. As my cart in jsp
Advertisements
Servlet-JSP population - JSP-Servlet
Servlet-JSP population  In refernce to this Question http://www.roseindia.net/answers/viewanswers/9843.html I reduced my servlet code and JSP... in my jsp page my code is working fine. If I dont check any of the item in my
Servlet-JSP population - JSP-Servlet
Servlet-JSP population  Hi I have a login page which accepts inputs and take to a servlet, servlet makes a jdbc connection to oracle server... in I redirect to a JSP page which populates a list of items available
How to call servlet in JSP?
How to call servlet in JSP?  How to call servlet in JSP
Servlet - JSP-Servlet
Servlet and Java Code  Example and source code in Servlet and JSP
java (servlet) - JSP-Servlet
java (servlet)  how can i disable back button in brower while using servlet or JSP
servlet - JSP-Servlet
be the xml code which will describe a servlet and mapps an instance of it with specific url..???  Hi friend, Servlet and JSP Examples. Servlet and JSP Examples
atm code in servlet& jsp
atm code in servlet& jsp  pls send me the code of ATM project in servlet jsp . my requirements are first of all an user login window open then balance enquiry, withdraw of money, money transfer, then log out. pls send as early
servlet - JSP-Servlet
servlet  please post me some attractive learn simple steps tutoriols jsp&servlet with source code or example with netbeans. post all servlet and jsp simple example chapter by chapter who will build my knowldge very fastly
servlet/jsp - JSP-Servlet
servlet/jsp  hi, get me the source code to upload all file formats in servlet/jsp plaese help me as soon as possible its urgent!!! by saravanan.k  Hi friend, Code to help in solving the problem
What is the difference between JSF, Servlet and JSP?
What is the difference between JSF, Servlet and JSP?  What is the difference between JSF, Servlet and JSP
servlet - JSP-Servlet
Inserting values in servlet and jsp  How to Insert the values into database table using Servlet and JSP?  Insert the values into database table using Servlet and JSP.Code ExampleDataInsertionExam.javaimport java.io.
About jsp and Servlet - JSP-Servlet
About jsp and Servlet  Servlets are directly using java technologies. It is used in HTTP,FTP..etc, But Jsp only used in HTTP. Servlets are running fastly... Then why we moved from Servlet to JSP.. Pl Tell me the reasons
How to pass an arraylist from servlet to jsp?
How to pass an arraylist from servlet to jsp?  Hello, Can anyone please tell me how to pass an arraylist from servlet to jsp? I have two arraylist... servlet to jsp? Thanks in advance
Difference Between Servlet and JSP
Difference Between Servlet and JSP In this section we will describe the Servlet and JSP. We will compare the both technologies. We will discuss the differences between Servlet and JSP. Servlet Servlet is a Java class that is usually
Servlet & Jsp - Java Interview Questions
Servlet & Jsp  how to forward or redirect the client request from servlet to jsp? how a thread or request pass from one servlet prog to one jsp prog? is it possible of communicating from servlet to jsp prog? please explain
Servlet & Jsp - Java Interview Questions
Servlet & Jsp   is it possible of communicating from SERVLET to JSP... servlet to jsp. Servlet2.java: import java.io.*; import javax.servlet...."))){ String jsp = "./jsp/loginSuccess.jsp"; RequestDispatcher dispatcher
JSP with java/servlet - JSP-Servlet
(java bean or servlet) to fecth the database. Jsp would get the data from.../servlet-jsp-data-list.shtml Hope that it will be helpful for you. Thanks...JSP with java/servlet  Thanks Deepak for your answere to my previous
Crop image Using JQuery, servlet or JSP.
Crop image Using JQuery, servlet or JSP.  Hi sir I need the code of Cropping image using JQuery with the help of servlet or JSP. I need both the code Client side as well as of server side. Please Help me
How to pass multiple values from a servlet to JSP?
How to pass multiple values from a servlet to JSP?  hi, I want to pass multiple values form a servlet to JSP. Hw do i do that? I am able to send one value at a time without any prb, but I am unable to carry multiple (from two
How to carry multiple values from a Servlet to a JSP?
How to carry multiple values from a Servlet to a JSP?  By using the below code I am able to carry the username to a JSP (single value... needs to be carried from my servlet to a JSP. How do I do
Java servlet with jsp on sql server
Java servlet with jsp on sql server  How to delete a user by an admin with check box in Java Servlet with jsp on Sql Server?   Here is a jsp example that can delete the multiple record from the database by selecting
Servlet - JSP Programming - JSP-Servlet
Servlet - JSP Programming  Hi, I'm writing a jsp page as shown... to cart or checkout). I'm programming in a servlet getting the input parameters... servlet code is also pasted below. please let me know how to solve
Servlet-JSP Mapping - JSP-Servlet
Servlet-JSP Mapping  Dear Sir, My Query were: How I use the issueData() in Jsp which define in Servlet_Two.java. If any mistake in my code please...\.wlnotdelete\extract\myserver_ProjDemo_ProjDemo\jsp_servlet\__issue.java' failed
Servlet-JSP Mapping - JSP-Servlet
Servlet-JSP Mapping  Dear Sir, My Query were: How I use the issueData() in Jsp which define in Servlet_Two.java. If any mistake in my code please...\.wlnotdelete\extract\myserver_ProjDemo_ProjDemo\jsp_servlet\__issue.java' failed
Distribute Servlet or JSP on network - JSP-Servlet
Distribute Servlet or JSP on network  Sir Even after successfully deploying a servlet or JSP program on J2EE web server I am not been able..." For any more problem on Servlet and Jsp visit to : http://www.roseindia.net
Override any method in servlet or jsp - JSP-Servlet
Override any method in servlet or jsp   Hi Friend, Can we override any life cycle methods in servlet and jsp.  Hi Please click on this url: http://www.roseindia.net/jsp/simple-jsp-example
Delhi Population by District
Delhi Population by District Are you looking for the District wise population... stay in the Delhi and this way the population of Delhi is growing day-by-day. Due to good job opportunities and high living standards the population of Delhi
ModuleNotFoundError: No module named 'ubi-population-tool'
ModuleNotFoundError: No module named 'ubi-population-tool'  Hi, My... named 'ubi-population-tool' How to remove the ModuleNotFoundError: No module named 'ubi-population-tool' error? Thanks   Hi
Diff ways to call a EJB from Servlet, JSP - Java Interview Questions
Diff ways to call a EJB from Servlet, JSP  How can I call EJB from Servlet or JSP
ArrayList from JSP to Servlet - JSP-Servlet
in a scriplet in a jsp page. how can i access the arraylist in a servlet which... should it be declared in jsp and in servlet. do i need to do any casting...://www.roseindia.net/jsp/servlet-jsp-data-list.shtml Hope that it will be helpful for you
which version of eclipse is used to execute Servlet or JSP
Servlet or JSP. previosuly i installed eclipse 3.1 .i am having j2se 5.0 with which i...which version of eclipse is used to execute Servlet or JSP  Hi Can... JSP or Servlets with the help of Eclipse.Another point is that when i installed
servlet and jsp
servlet and jsp  how to connect an jsp and an servlet without connecting to database
Servlet,Jsp
Servlet,Jsp   This is my requirement?I have a login page and register page?If I give the url as http://localhost:8080/Project/Login.jsp ,it should go to login page?.then from login page on click of register button I can register
servlet and jsp
servlet and jsp  how can i get a form question from a database... with servlets and jspīs   Have a look at the following links: http://www.roseindia.net/jsp/paging.shtml http://www.roseindia.net/jsp/navigation
SERVLET AND JSP
SERVLET AND JSP  If i want tio create a login form using SUBMIT,ADD...;For more information visit the following links: http://www.roseindia.net/jsp/user...://www.roseindia.net/answers/viewqa/JSP-Servlet/16413-using-insert-and-delete-in-a-single
SERVLET AND JSP
SERVLET AND JSP  If i want tio create a login form using SUBMIT,ADD...;For more information visit the following links: http://www.roseindia.net/jsp/user...://www.roseindia.net/answers/viewqa/JSP-Servlet/16413-using-insert-and-delete-in-a-single
how do i upload a file by using servlet or jsp?
how do i upload a file by using servlet or jsp?  hi plz tell me the write java code
Fetching database field from servlet to jsp page ?
Fetching database field from servlet to jsp page ?  Hello Java... field. I wanted to pass some of the database field from servlet to jsp... (i am opening my database in servlet init() method ) how to pass rs.getString(8
Capturing JSP Content Within My Strut Action Servlet - JSP-Servlet
Capturing JSP Content Within My Strut Action Servlet  My end goal is to be able to grab the content from the JSP from within the calling servlet... with a buffered output. * You can use this to forward or include a Servlet or JSP page
how to connect the database using hibernet through servlet/jsp through form
how to connect the database using hibernet through servlet/jsp through form  plz give me the reply   Hi Friend, Please visit the following link:ADS_TO_REPLACE_1 http://www.roseindia.net/jsf/myfacesspring
how to store data in other table using servlet and jsp
how to store data in other table using servlet and jsp  pls can anyone tell how to store data in other table using servlet and jsp and want to display that data too.and the data in first table must be same.pls help
How to display Jfreechart from servlet in jsp web page at specified location
How to display Jfreechart from servlet in jsp web page at specified... in Servlet which is in image format.plz sir give me the code to display this chart in jsp web page . Thank you very much Sir
How to get the data from the database using Servlet or JSP program
How to get the data from the database using Servlet or JSP program  ... by using servlet or jsp program by typing the output as like this below.... Actual problem is that i have a simple program in jsp in the sense to get data
population problem plz help me befor 16 March 2011 !!
population problem plz help me befor 16 March 2011 !!  the Q is : How can i count how many years it will take for the population of a town to go over... population :" ); Scanner keyboard = new Scanner (System.in); popu = keyboard.nextInt
jsp/servlet - JSP-Servlet
jsp/servlet  How to create and save the excel file on given location using jsp/servlet?  hi Geetanjali, Read for more information, http://www.roseindia.net/jsp/poi/excelPOI.shtml Thanks
EJB and Servlet - JSP-Servlet
EJB and Servlet  hi all; I Have some Question regarding the EJB and servlet, How can we communicates servlet and EJB? can u give me a sample Code how to connect my servlet to ejb.. ty   Hi Friend
Java Servlet - JSP-Servlet
servlet - JSP-Servlet
servlet  http://localhost:8080/collegeapp/Avanthi.html after complition of my servlet program, i typed in the above url, then small window is displayed in my monitor, it asks connect to localhost, enter user name

Ads