2 Replies - 4320 Views - Last Post: 02 June 2012 - 03:32 PM

#1 greek_magician  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-June 12

Pass int value from jsp to jsp page

Posted 02 June 2012 - 10:05 AM

Good evening,

I have a simple problem. I'm making a shopping basket and i want to add some prices for the items but i can not figure out how to pass the value from one page to another in order to view the total ammount of payment in the cart view.
For example, i want a cpu to cost 200 and a vga 300. When someone goes to cart, there should be a total 500.

Here is the code so far:

add.jsp
    
<center>
	<html>
		<head>
			<title>My Shopping Cart</title>
			<style>
				*{
					font-size:12px; 
					font-family:Verdana; 
				}
				input
				{ 
					border:1px solid #cccccc
				}
			</style>
		</head>
		<body>
			<!--declaring variables here-->
			<jsp:declaration>
				java.util.Enumeration parms;
				java.util.Enumeration values;
				int cpu=5;
			</jsp:declaration>

			<!--put variables into session-->
			<jsp:scriptlet>
				parms = request.getParameterNames();
				values = request.getParameterNames();

				while(parms.hasMoreElements()) 
				{
					String name = (String) parms.nextElement();
					String value = (String) values.nextElement();
					session.setAttribute(name, value);
				}
			</jsp:scriptlet>

			<!--references-->
			<img src="images/add.png" onclick="document.location='add.jsp'">
			<img src="images/remove.png" onclick="document.location='remove.jsp'">
			<img src="images/cart.png" onclick="document.location='cart.jsp'">

			<!--header-->
			<h2>Check to add items</h2>

			<form method="get" action="add.jsp">
				<table>
					<tr>
						<td>
							<input type="checkbox" <% if (session.getAttribute("cpu") != null)
							out.print("checked"); %> name="cpu">
						</td>
						<td>cpu</td>
						
					</tr>
					
					<tr>
						<td>
							<input type="checkbox" <% if (session.getAttribute("vga") != null)
							out.print("checked"); %> name="vga">
						</td>
						<td>vga</td>
					</tr>
					
					<tr>
						<td>
							<input type="checkbox" <% if (session.getAttribute("motherboard") != null)
							out.print("checked"); %> name="motherboard">
						</td>
						<td>motherboard</td>
					</tr>
					
					<tr>
						<td>
							<input type="checkbox" <% if (session.getAttribute("ssd") != null)
							out.print("checked"); %> name="ssd">
						</td>
						<td>ssd</td>
					</tr>
					
					<tr>
						<td>
							<input type="checkbox" <% if (session.getAttribute("case") != null)
							out.print("checked"); %> name="case">
						</td>
						<td>case</td>
					</tr>
				</table>
				
				<br/><br/>
				
				<input type="submit" value="submit">
			</form>
		</body>
	</html>
</center>



remove.jsp
<center>
	<html>
		<head>
			<title>My Shopping Cart</title>
			<style>
				*{ 
					font-size:12px; 
					font-family: Verdana; 
				}
				input 
				{ 
					border:1px solid #cccccc 
				}
			</style>
		</head>
		
		<body>
			<jsp:declaration>
				java.util.Enumeration parms;
			</jsp:declaration>

			<jsp:scriptlet>
				parms = request.getParameterNames();

				while(parms.hasMoreElements()) 
				{
					String name = (String) parms.nextElement();
					session.removeAttribute(name);
				}
			</jsp:scriptlet>


			<img src="images/add.png" onclick="document.location='add.jsp'">
			<img src="images/remove.png" onclick="document.location='remove.jsp'">
			<img src="images/cart.png" onclick="document.location='cart.jsp'">

			<h2>Check to remove items</h2>

			<form method="get" action="remove.jsp">

				<table>
					<% if (session.getAttribute("cpu") != null) { %>
						<tr>
							<td><input type="checkbox" name="cpu"></td><td>cpu</td></td>
						</tr>
					<% } %>
					
					<% if (session.getAttribute("vga") != null) { %>
					<tr>
						<td><input type="checkbox" name="vga"></td><td>vga</td></td>
					</tr>
					<% } %>
					
					<% if (session.getAttribute("motherboard") != null) { %>
					<tr>
						<td><input type="checkbox" name="motherboard"></td><td>motherboard</td></td>
					</tr>
					<% } %>
					
					<% if (session.getAttribute("ssd") != null) { %>
					<tr>
						<td><input type="checkbox" name="ssd"></td><td>ssd</td></td>
					</tr>
					<% } %>
					
					<% if (session.getAttribute("case") != null) { %>
					<tr>
						<td><input type="checkbox" name="case"></td><td>case</td></td>
					</tr>
					<% } %>
				</table>
				
				<br/><br/>
				
				<input type="submit" value="submit">
			</form>
		</body>
	</html>
</center>


cart.jsp
<center>
	<html>
		<head>
			<title>My Shopping Cart</title>
			<style>
				*{
					font-size:12px;
					font-family:Verdana; 
				}
			</style>
		</head>
		
		<body>
			<img src="images/add.png" onclick="document.location='add.jsp'">
			<img src="images/remove.png" onclick="document.location='remove.jsp'">
			<img src="images/cart.png" onclick="document.location='cart.jsp'">

			<h2>Shopping Cart Status:</h2>

			<jsp:scriptlet>
				<![CDATA[ 
					java.util.Enumeration content = session.getAttributeNames();
					
					while (content.hasMoreElements()) 
					{
						out.println(content.nextElement());
						out.println("<br>");
					}
				 ]]>
			</jsp:scriptlet>
		</body>
	</html>
</center>


Is This A Good Question/Topic? 0
  • +

Replies To: Pass int value from jsp to jsp page

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,518
  • Joined: 23-August 08

Re: Pass int value from jsp to jsp page

Posted 02 June 2012 - 03:09 PM

Moved to JavaEE; JSP is NOT Javascript, it's Java Server Pages

EDIT: Actually, you didn't put it in the wrong spot, someone else moved it to Javascript.
Was This Post Helpful? 0
  • +
  • -

#3 greek_magician  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-June 12

Re: Pass int value from jsp to jsp page

Posted 02 June 2012 - 03:32 PM

There isn't any JSP section so i originally place it at Web Development section.

Anyway, any kind of help???
I made some progress by just printing some values in add/remove pages but i cant figure out how to print a second value (except from the item) through the scriptlet in the cart page.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1