返回介绍

Shopping cart

发布于 2025-02-22 22:20:10 字数 8537 浏览 0 评论 0 收藏 0

In this part of the JEE tutorials, we will create a simple skeleton of a shopping cart. We will demonstrate how to use a session object in JSP.

The HTTP protocol is a stateless protocol. It does not retain information between requests from the clients. To overcome this deficiency, the developers can use cookies, hidden variables, URL encoded parameters or sessions.

Shopping cart

When we create a shopping cart in our application, we have to store user's state. For example, we put a book in books.jsp page into our cart then move to cds.jsp and want to select another item. We must retain all information about books that have been put into the shopping cart. Every JSP page has an implicit session object that we can use. This object is an instance of HttpSession class, which handles all details behind storing and retrieving session data.

We have three JSP pages in our example. The index.jsp adds items to the shopping cart. The remove.jsp removes items from the cart. The cart.jsp will display items in the shopping cart.

index.jsp

<html>
<head>
<title>Shopping cart</title>
<style>
* { font-size: 12px; font-family: Verdana }
input { border: 1px solid #ccc }
</style>
</head>
<body>

<jsp:declaration>
java.util.Enumeration parms;
java.util.Enumeration values;
</jsp:declaration>

<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>


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


<h2>Add to shopping cart</h2>

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

<table>
<tr>
<td><input type="checkbox" <% if (session.getAttribute("scissors") != null) 
out.print("checked"); %> name="scissors"></td>
<td>Scissors</td>
</tr>
<tr>
<td><input type="checkbox" <% if (session.getAttribute("book") != null)
 out.print("checked"); %> name="book"></td>
<td>Book</td>
</tr>
<tr>
<td><input type="checkbox" <% if (session.getAttribute("pen") != null)
 out.print("checked"); %> name="pen"></td>
<td>Pen</td>
</tr>
<tr>
<td><input type="checkbox" <% if (session.getAttribute("bottle") != null)
 out.print("checked"); %> name="bottle"></td>
<td>Bottle</td>
</tr>
<tr>
<td><input type="checkbox" <% if (session.getAttribute("glass") != null)
out.print("checked"); %> name="glass"></td>
<td>Glass</td>
</tr>
</table>
<br><br>
<input type="submit" value="submit">
</form>


</body>
</html>

The index.jsp is used to add items to the shopping cart.

<jsp:declaration>
java.util.Enumeration parms;
java.util.Enumeration values;
</jsp:declaration>

This is the declaration tag. Variable declared here are available in JSP expressions and scriptlets.

<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>

In this scriptlet, we get all the parameters from the request and put them into the session.

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

We can use the same file that sends the form data to process the data.

<td><input type="checkbox" <% if (session.getAttribute("scissors") != null) 
out.print("checked"); %> name="scissors"></td>

The scriptlet embedded in this HTML code controls, whether to check the checkbox. It depends upon whether the item is already in the session.

cart.jsp

<html>
<head>
<title>Shopping cart</title>
<style>
* { font-size: 12px; font-family: Verdana }
</style>
</head>
<body>


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


<h2>The shopping cart</h2>

<jsp:scriptlet><![CDATA[ 
java.util.Enumeration content = session.getAttributeNames();

while (content.hasMoreElements()) {
  out.println(content.nextElement());
  out.println("<br>");
}

 ]]></jsp:scriptlet>

</body>
</html>

The cart.jsp displays the content of the shopping cart.

<jsp:scriptlet><![CDATA[ 
java.util.Enumeration content = session.getAttributeNames();

while (content.hasMoreElements()) {
  out.println(content.nextElement());
  out.println("<br>");
}

 ]]></jsp:scriptlet>

This scriptlet displays items in the cart. Notice the <![CDATA[ ... ]]> In this example, if we use scriplets in XML format, we have to put the <![CDATA[ ... ]]> into the scriptlet body. It is because we print the <br> tag. Otherwise the JSP does not compile.

remove.jsp

<html>
<head>
<title>Shopping cart</title>
<style>
* { font-size: 12px; font-family: Verdana }
input { border: 1px solid #ccc }
</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='index.jsp'">
<img src="images/remove.png" onclick="document.location='remove.jsp'">
<img src="images/cart.png" onclick="document.location='cart.jsp'">

<h2>Remove items from cart</h2>

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

<table>
<% if (session.getAttribute("scissors") != null) { %>
<tr>
<td><input type="checkbox" name="scissors"></td><td>Scissors</td></td>
</tr>
<% } %>
<% if (session.getAttribute("book") != null) { %>
<tr>
<td><input type="checkbox" name="book"></td><td>Book</td></td>
</tr>
<% } %>
<% if (session.getAttribute("pen") != null) { %>
<tr>
<td><input type="checkbox" name="pen"></td><td>Pen</td></td>
</tr>
<% } %>
<% if (session.getAttribute("bottle") != null) { %>
<tr>
<td><input type="checkbox" name="bottle"></td><td>Bottle</td></td>
</tr>
<% } %>
<% if (session.getAttribute("glass") != null) { %>
<tr>
<td><input type="checkbox" name="glass"></td><td>Glass</td></td>
</tr>
<% } %>
</table>
<br><br>
<input type="submit" value="submit">
</form>

</body>
</html>

The remove.jsp removes items from the shopping cart.

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

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

This scriptlet removes items from the cart.

<% if (session.getAttribute("scissors") != null) { %>
<tr>
<td><input type="checkbox" name="scissors"></td><td>Scissors</td></td>
</tr>
<% } %>

This code displays scissors input tag only if it is present in the session.

remove.jsp
Figure: remove.jsp

A shopping cart skeleton to demonstrate session object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文