Servlet - JSP
Here is my complete code. all my code is running with out any error. the for loop in servlet is running as many times as my checkboxes were checked.
the number and price were being read correct. But my total array list is not working fine. its not returning the product when i try printing in the checkout.jsp page. i tried printing the value stored in total arraylist by using total.indexof(0) & total.indexof(1). they are not storing the product. They are returning -1 and -1. please let me know how to resolve it.
Thanks,
CARTUPDATE.JSP:
-------------------------------
<!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> </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>
Servlet Code:
----------------------
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);
int p =(chair_num*price_num);
i++;
total.add(p);
}
int a1 = total.indexOf(0);
int a2 = total.indexOf(1);
session.setAttribute("c1", i);
session.setAttribute("c2", a1);
session.setAttribute("c3", a2);
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);
}
}
}
Checkout.jsp:
---------------------------------
<!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.util.ArrayList" %>
<head>
<title>checkout</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<%@ include file="header.jsp"%>
<%@ include file="left.jsp"%>
<form method="post" action= "updateCart">
Checkout
<%String s2 = session.getAttribute("c1").toString();
int j = Integer.parseInt(s2);
String s4 = session.getAttribute("c2").toString();
String s5 = session.getAttribute("c3").toString();
int k = Integer.parseInt(s4);
int l = Integer.parseInt(s5);
out.println(j);
out.println(k);
out.println(l);%>
<%ArrayList<Integer> s3= new ArrayList<Integer>(); %>
<% s3 = (ArrayList)session.getAttribute("efg"); %>
<% for(int i:s3){
//int d3=Integer.parseInt(i);
out.println(i);}%>
<br>
<br>
<br>
<p><input type="submit" name="direction" value="EditCart"> <input
type="submit" name="direction" value="PayNow"></p>
</form>
</body>
</html>
View Answers
June 11, 2010 at 11:32 AM
Hi Friend,
We have modified your code:
1)jsp file
<!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>
<style>
div.form {
float: center;
width: 30%;
height:50%;
margin-right: auto;
}
</style>
<form method="post" action="../ServletExample">
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection("jdbc:
mysql://localhost:3306/register","root";, "root");
Statement statement = connection.createStatement() ;
ResultSet rs = statement.executeQuery("select * from item");
int i=1;
out.println("connection made");%>
<TABLE cellpadding="5" border="1">
<tr>
<td> </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("Item_Name")%>" readonly/></td>
<td><input type="text" name="price" value="<%=rs.getString("price")%>" 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>
June 11, 2010 at 11:38 AM
continue..
2)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;
public class ServletExample extends HttpServlet {
private static final long serialVersionUID = 1L;
public ServletExample() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
PrintWriter out=response.getWriter();
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();
if (add_to_cart.equals("Add2Cart")||edit_to_cart.equals("EditCart")){
String[] checkValues=request.getParameterValues("box");
int j=0;
String price[] =request.getParameterValues("price");
int p[]=new int[price.length];
int price_num[]=new int[price.length];
if(price !=null) {
for(int i=0;i<price.length;i++) {
price_num[i]=Integer.parseInt(price[i]);
}
}
String number[] =request.getParameterValues("number");
int num[]=new int[number.length];
if(number !=null) {
for(int i=0;i<number.length;i++) {
num[i]=Integer.parseInt(number[i]);
}
}
for(int i=0;i<num.length;i++){
p[i]=num[i]*price_num[i];
total.add(p[i]);
}
int a1 = total.indexOf(0);
int a2 = total.indexOf(1);
session.setAttribute("c1", j);
session.setAttribute("c2", a1);
session.setAttribute("c3", a2);
RequestDispatcher rd;
rd = request.getRequestDispatcher("/cartupdate.jsp");
rd.forward(request,response);
}
if (checkout.equals("checkout1")){
String price[] =request.getParameterValues("price");
int p[]=new int[price.length];
int price_num[]=new int[price.length];
if(price !=null) {
for(int i=0;i<price.length;i++) {
price_num[i]=Integer.parseInt(price[i]);
}
}
String number[] =request.getParameterValues("number");
int num[]=new int[number.length];
if(number !=null) {
for(int i=0;i<number.length;i++) {
num[i]=Integer.parseInt(number[i]);
}
}
for(int i=0;i<num.length;i++){
p[i]=num[i]*price_num[i];
total.add(p[i]);
}
session.setAttribute("efg",total);
RequestDispatcher rd;
rd = request.getRequestDispatcher("/modified/action.jsp");
rd.forward(request,response);
}
}
}
June 11, 2010 at 11:39 AM
continue..
3)jsp:
<!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.util.*" %>
<head>
<title>checkout</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<form method="post" action= "updateCart">
<table>
Checkout
<%Iterator itr;%>
<% List data= (List)session.getAttribute("efg");
for (itr=data.iterator(); itr.hasNext(); )
{
%>
<tr>
<td width="119"><%=itr.next()%></td>
</tr>
<%}%>
<p><input type="submit" name="direction" value="EditCart"> <input
type="submit" name="direction" value="PayNow"></p>
</table>
</form>
</body>
</html>
Thanks
Related Tutorials/Questions & Answers:
ServletServlet how to navigate one
servlet page to another
servlet page
Advertisements
servletservlet is there any way to include pdf's in
servlet servletservlet How many times the
servlet is accessed
servletservlet what are the methods and interfaces in the
servlet api ?
Servlet Tutorials
servletservlet what are the all necessary configuration to run a
servlet servletservlet how to interact with a
servlet from a swing program
servletservlet I designed 1 html form & a
servlet but when I click on form I don't get output of
servlet Please help
servletservlet I designed 1 html form & a
servlet but when I click on form I don't get output of
servlet Please help
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
servletservlet i want a program for counting the no of times the
servlet has been invoked
ServletServlet What must be implemented by all Servlets?
The
Servlet Interface must be implemented by all servlets
servletservlet how to create a login form using
servlet using submit,edit delete button
servletservlet 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
servletservlet dear sir
servlet and html not run on eclips plz help me
ServletServlet 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
servletservlet 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
servletservlet 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
ServletServlet 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
servletservlet 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
servletservlet 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>
servlet 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 and doPost methods in the
servlet. I think, but unfortunately I have a terrible teacher
servletservlet 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 to
servlet. So, if anyone can help me so I can see how to connect the two I would
servletservlet 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
servlet com.ilp.tsi.um.bean.BankBean;
import com.ilp.tsi.um.service.BankService;
/**
*
Servlet servlet - Servlet Interview Questionsnaming convention for
servlet in java Need to know the naming convention for
servlet in java Hi,Yes servlet.jar and
servlet-api.jar is the same file. In case of tomcat servlet.jar is just renamed to
servlet servlet - Servlet Interview Questions, the
servlet container will load the
servlet class and create
servlet object when it receives the first request call for that particular
servlet.
But, by using the load-on-startup in web.xml for a particular
servlet will make the
servlet servlet mappingservlet mapping Defined
servlet mapping in web.xml ?
<servlet>
<
servlet-name>Hello</
servlet-name>
<
servlet-class>HelloWorld</
servlet-class>
</servlet>
<
servlet-mapping>
servlet - Servlet Interview Questions,
Service method in called by the
servlet container to process a request from the browser.
When user calls a
servlet, it's service method is executed by
servlet container to process the user request.
So, service method is called
servlet-errorservlet-error where we declare a
servlet errors in web application