Bonjour!
Je fais un bachelier en informatique de gestion et je suis un cours de projet de développement internet et intranet.
Dans ce cadre je dois faire un site de vente de chaussures en ligne en utilisant Tomcat6.
Pour vérifier si mon cartBean est null dans cartServlet je fais ceci:
Il me redirige systématiquement sur Welcome.html
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { cartBean = (CartBean) request.getSession().getAttribute("cartBean"); if(null == cartBean){ this.getServletContext().getRequestDispatcher("/Welcome.html").forward(request, response); } else{ this.getServletContext().getRequestDispatcher("/jsp/CartJsp.jsp").forward(request, response); } }
J'ai tout essayé mais rien ne fait...
voici les autres classes (histoire que vous comprenez le contexte)
shopServlet:
ShopJsp:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public class ShopServlet extends HttpServlet { private static final long serialVersionUID = 1L; private CartBean cartBean; private int quantity; private double price; private Article article; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { cartBean = (CartBean) request.getSession().getAttribute("cartBean"); if(null == cartBean){ cartBean = new CartBean(); System.out.println(); request.getSession().setAttribute("cartBean", cartBean); } String description = request.getParameter("description"); String priceString = String.valueOf(request.getParameter("price")); // Vérifie si la quantité est bien un int String quantityStr = String.valueOf(request.getParameter("quatity")); try{ price = Double.valueOf(priceString); quantity = Integer.parseInt(quantityStr); }catch(NumberFormatException nfe){ } //l'utilisateur n'a pas spécifié de quantité // Si quantity est vide if(null != request.getParameter("quantity")){ if(request.getParameter("quantity").equals("")){ request.setAttribute("message", "Veuillez précisser la quantité!"); this.getServletContext().getRequestDispatcher("/jsp/ErrorJsp.jsp").forward(request, response); } article = new Article(description, price); Facade.getInstance().addArticle(article, quantity); cartBean.setCart(Facade.getInstance().getArticles()); } this.getServletConfig().getServletContext().getRequestDispatcher("/jsp/ShopJsp.jsp").forward(request, response); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
CartJsp:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="modele.*"%> <%@ page import="bean.*"%> <%@ page import="java.util.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@page import="model.Article"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Catalogue</title> <% ServletContext context = this.getServletContext(); Map<Article, Integer> catalogue = new HashMap<Article, Integer>(); catalogue = (Map<Article, Integer>) Facade.getInstance().showStock().getArticles(); %> </head> <body> <h1 style=text-align:center>ShoesShop</h1> <br/> <br/> <a href="CartJsp.do">Mon Panier</a> <br/> <br/> <h2>Notre catalogue:</h2> <br/> <br/> <table border="1px"> <tr> <th>Article</th> <th>Prix</th> <th>Stock</th> <th>Quantité</th> <th> </th> </tr> <% for (Map.Entry<Article, Integer> e : catalogue.entrySet()) { %> <tr> <form name="shopServlet" action="ShopJsp.do"> <td><%=e.getKey().getDescription()%><input type="hidden" name="description" /></td> <td><%=e.getKey().getPrice()%><input type="hidden" name="price" /></td> <td><%=e.getValue()%><input type="hidden" name="stock" /></td> <td><input type="text" size="10" name="quantity" /></td> <td><input name="submit" type="submit" value="ajouter au panier" /></td> </form> </tr> <% } %> </table> </body> </html>
Facade (il n'y a pas de bd):
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="modele.*"%> <%@ page import="bean.*"%> <%@ page import="java.util.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@page import="model.Article"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Mon Panier</title> <% ServletContext context = this.getServletContext(); CartBean cartBean = (CartBean) request.getSession().getAttribute("cartBean"); %> </head> <body> <h1 style=text-align:center>ShoesShop</h1> <br/> <br/> <h2>Notre catalogue:</h2> <br/> <br/> <table border="1px"> <tr> <th>Article</th> <th>Prix</th> <th>Quantité</th> <th>Total</th> <th> </th> </tr> <% for (Map.Entry<Article, Integer> e : cartBean.getCart().entrySet()) { %> <tr> <form name="cartJsp" action="CartJsp.do"> <td><%=e.getKey().getDescription()%><input type="hidden" name="description" /></td> <td><%=e.getKey().getPrice()%><input type="hidden" name="price" /></td> <td><%=e.getValue()%><input type="hidden" name="quantity" /></td> <td><%=e.getKey().getPrice()*e.getValue()%></td> <td><input name="submit" type="submit" value="supprimer" /></td> </FORM> </tr> <% } %> </table> <br/> <br/> <h3 align="right">Total : <%= Facade.getInstance().showTotalPrice()%></h3> <input name="submit" type="submit" value="vider le panier" /> <br/> <br/> Commandez (mais pour cela, vous devez vous identifier)<a href="Identification.do">identification</a> </body> </html>
et CartBean:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 public class Facade { private static Facade instance; private Shop shop; private Cart cart; private Facade() { // TODO Auto-generated constructor stub shop = new Shop(); cart = new Cart(); } public static Facade getInstance(){ if (instance == null) instance = new Facade(); return instance; } public Stock showStock(){ return shop.getStock(); } public Map<Article, Integer> getArticles(){ return cart.getArticles(); } public void addArticle(Article article, int quantity){ cart.addArticle(article, quantity); } public void removeArticle(Article article, int quantity){ cart.removeArticle(article, quantity); } public void removeArticles(){ cart.removeArticles(); } public double showTotalPrice(){ return cart.showTotalPrice(); } public void addOrder(Cart cart, Client client){ this.addOrder(cart, client); } public List<Client> getClient() { return shop.getClients(); } public Map<Cart,Client> getOrders(){ return shop.getOrders(); } public Shop getShop() { return shop; } public void setShop(Shop shop) { this.shop = shop; } public Cart getCart() { return cart; } public void setCart(Cart cart) { this.cart = cart; } }
Merci beaucoup d'avance
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public class CartBean { private Stock stock; private Map <Article, Integer> cart; private Client client; private Article article; private double totalPrice; public Article getArticle() { return article; } public void setArticle(Article article) { this.article = article; } public double getTotalPrice() { return totalPrice; } public void setTotalPrice(double totalPrice) { this.totalPrice = totalPrice; } public Stock getStock() { return stock; } public void setStock(Stock stock) { this.stock = stock; } public Client getClient() { return client; } public void setClient(Client client) { this.client = client; } public void setCart(Map <Article, Integer> cart) { this.cart = cart; } public Map <Article, Integer> getCart() { return cart; } }![]()
Partager