Home Answers Viewqa JSP-Servlet Servlet-JSP population

 
 


Kiran
Servlet-JSP population
0 Answer(s)      2 years and 11 months ago
Posted in : JSP-Servlet

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 Pages:
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
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
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
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 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
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
java (servlet) - JSP-Servlet
java (servlet)  how can i disable back button in brower while using servlet or JSP
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
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
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
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 - 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
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.
how to write the coding for converting the database data for example population into any type of graph using jsp and servlets?//
for converting the database data for example population into any type of graph using jsp and servlets pls help me out how to write the coding for converting the database data for example population into any type of graph using jsp and servlet
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
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
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
Servlet - JSP-Servlet
Deploy the application Servlet  How to deploy my application in Servlet?  Hi,you can put yours applet .class in in one folder simillarly put all of your jsps in in other folder.in the plugin tag in the jsp page mention
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
servlet - JSP-Servlet
servlet  Develop a web page using Servlet for session tracking which ask for your name and address and print a message of welcome along with the number of times you have visited the page. If you are visiting the page first time
Servlet and Jsp - JDBC
Servlet and Jsp  Hai I need a program to update the table records ......The program should have only update code.....It shouldnot combine with Add or delete code. It is very urgent ... Send me the full code
servlet - JSP-Servlet
servlet  How to convert multiple servlets into a single servlet,without increasing number of html files
JAVA SERVLET - JSP-Servlet
JAVA SERVLET Log   Hi, A servlet file which create the log files ,then writing values to the list & log, we should have a thousand separator and no decimal values. This means instead of writing 986300.0 in the Log file
servlet and jsp - JSP-Servlet
servlet and jsp  Hi friend, please show one sample program, how to connect jsp and servlet using backend a ms-access.  Hi friend,<%@ page language="java" import="java.sql.*,java.util.*,java.text.*"
servlet and jsp - JSP-Servlet
servlet and jsp  Hello folks, I am very new JDBC. I am doing a project on java messaging service on the part of the project we are using JDBC. In my...   write in the jsp form action="path of servlet" and wirite
servlet - JSP-Servlet
data on a servlet. //myServlet.java// here do something like this-- String
servlet-jsp - Java Beginners
servlet-jsp  hello sir i want the total code to create a website by taking input as vehicle number and model of it from user and with that fetch the details from database by checking the vehicle number and model and shud display
Compiling a Servlet - JSP-Servlet
Compiling a Servlet  Respected sir, I just want to know how to compile a servlet coding? I need it in a sequential way so that I can understand it at the very first time as I am new to this technology
servlet - JSP-Servlet
servlet  I m new in servelet i need help to know abt servlet how to install tomcat, how to run tomcat, how to make servlet program, how run... and then develop simple applications. You can learn servlet at http
on servlet - JSP-Servlet
on servlet deployment  What we'll get on servlet deployment
servlet - JSP-Servlet
servlet navigation problem  Hi, i am facing problem in Servlet navigation
servlet and jsp - JSP-Servlet
servlet and jsp  can any one give me jsp-servlet related project-its urgent- 1-chat application 2-bug tracking system 3-online shopping 4-online...://www.roseindia.net/jsp/bank.shtml Thanks
Servlet and Jsp - JDBC
Servlet and Jsp  Sir I am getting Nullpointer exception in for(itr=itr=data.iterator();itr.hasNext();),of the below program. http://www.roseindia.net/mysql/select-box.shtml
Logout Servlet - JSP-Servlet
Logout Servlet  Hello. I am trying to write servlet that will log user out when the user clicks the log out button or link. Please help
Servlet - JSP-Servlet
Servlet  I have a html signup.html page containing name, id ,password and confirm password paremeters, and in the servlet called SignUp.java i... signup javacode.SignUp signup /jsp/SignUp
jsp/servlet - JSP-Servlet
jsp/servlet  Hello friends ! I want to create columns in a table at run time everyday. I am using mysql database.how should I do it? thanks in advance  Hi friend, Plz explain your problem in details to solve
servlet - JSP-Servlet
*; import javax.servlet.*; import javax.servlet.http.*; public class Servlet extends...(); RequestDispatcher dispatcher = context.getRequestDispatcher("/jsp/form.jsp... FormServlet /FormServlet Servlet Servlet Servlet /Servlet Thanks
servlet - JSP-Servlet
servlet  How can i use javascript in servlet page to print alert message  Hi friend, printAlert.js function printAlert...(); } } For more information on Servlet visit to : http://www.roseindia.net
servlet/jsp - JSP-Servlet
servlet/jsp  Thanks for ur feedback! But this program shows an error showing package.org.apache.commons.fileupload.servlet does not exists package.org.apache.commons.fileupload.disk does not exists
servlet - JSP-Servlet
servlet  hi sir,plz provide what is a servlet ,how to run the servlet and what r the good books for understanding servlets,plz provide me  Hi Friend, Please visit the following link: http://www.roseindia.net
Servlet - JSP-Servlet
Servlet  Hi, Can Servlet have instance variables.If yes please give an example. Thanks, Poonam

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.