Редактирование: Java EE Shopping cart skeleton

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск
Внимание: Вы не представились системе. Ваш IP-адрес будет записан в историю изменений этой страницы.
Правка может быть отменена. Пожалуйста, просмотрите сравнение версий, чтобы убедиться, что это именно те изменения, которые вас интересуют, и нажмите «Записать страницу», чтобы изменения вступили в силу.
Текущая версия Ваш текст
Строка 1: Строка 1:
 +
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 <b>http</b> protocol is a stateless protocol. It does not retain information between requests from the clients. To overcome this deficiency, the developers can use <b>cookies</b>, hidden variables, url encoded parameters or <b>sessions</b>.
 +
 +
== 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 <b>session</b> object that we can use.
 +
This object is an instance of <b>HttpSession</b> class, which handles all details behind storing and retrieving session data.
 +
 +
We have three jsp pages in our example. index.jsp will add items to the shopping cart. The remove.jsp will remove items from the cart. The cart.jsp will display items in the shopping cart.
 +
 +
<source lang="java">
 +
 +
<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>
 +
</source>
 +
 +
The index.jsp is used to add items to the shopping cart.
 +
 +
<source lang="java">
 +
<jsp:declaration>
 +
java.util.Enumeration parms;
 +
java.util.Enumeration values;
 +
</jsp:declaration>
 +
</source>
 +
 +
This is the declatration tag. Variable declared here are available in jsp expressions and scriptlets.
 +
 +
<source lang="java">
 +
 +
<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>
 +
</source>
 +
 +
In this scriptlet, we get all the parameters from the request and put them into the session.
 +
 +
<source lang="java">
 +
<form method="get" action="index.jsp">
 +
 +
</source>
 +
 +
We can use the same file that sends the form data to process the data.
 +
 +
<source lang="java">
 +
<td><input type="checkbox" <% if (session.getAttribute("scissors") != null)
 +
out.print("checked"); %> name="scissors"></td>
 +
</source>
 +
 +
The scriptlet embedded in this html code controls, whether to check the checkbox or not. I depends upon whether the item is already in session or not.
 +
 +
<source lang="java">
 +
 +
<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>
 +
</source>
 +
 +
The cart.jsp displays the content of the shopping cart.
 +
 +
<source lang="java">
 +
<jsp:scriptlet><![CDATA[
 +
java.util.Enumeration content = session.getAttributeNames();
 +
 +
while (content.hasMoreElements()) {
 +
    out.println(content.nextElement());
 +
    out.println("<br>");
 +
}
 +
 +
  ]]></jsp:scriptlet>
 +
</source>
 +
 +
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 won't compile.
 +
 +
<source lang="java">
 +
 +
<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>
 +
</source>
 +
 +
The remove.jsp file removes items from the shopping cart.
 +
 +
<source lang="java">
 +
<jsp:scriptlet>
 +
parms = request.getParameterNames();
 +
 +
while(parms.hasMoreElements()) {
 +
    String name = (String) parms.nextElement();
 +
    session.removeAttribute(name);
 +
}
 +
</jsp:scriptlet>
 +
</source>
 +
 +
This scriptlet removes items from the cart.
 +
 +
<source lang="java">
 +
<% if (session.getAttribute("scissors") != null) { %>
 +
 +
<tr>
 +
<td><input type="checkbox" name="scissors"></td><td>Scissors</td></td>
 +
</tr>
 +
<% } %>
 +
 +
</source>
 +
 +
This code displays scissors input tag only if it is present in the session.
 +
 +
[[image: java_ee_faq_remove.jpg| center]]
 +
 +
[[Категория:Java]]

Пожалуйста, обратите внимание, что все ваши добавления могут быть отредактированы или удалены другими участниками. Если вы не хотите, чтобы кто-либо изменял ваши тексты, не помещайте их сюда.
Вы также подтверждаете, что являетесь автором вносимых дополнений, или скопировали их из источника, допускающего свободное распространение и изменение своего содержимого (см. Wiki.crossplatform.ru:Авторское право). НЕ РАЗМЕЩАЙТЕ БЕЗ РАЗРЕШЕНИЯ ОХРАНЯЕМЫЕ АВТОРСКИМ ПРАВОМ МАТЕРИАЛЫ!