01 package interval;
02
03 import javax.servlet.*;
04 import javax.servlet.http.*;
05 import java.io.*;
06 import java.util.*;
07
08 public class ShoppingCartServlet extends HttpServlet {
09
10 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
11 //získanie objektu session
12 request.setCharacterEncoding("windows-1250");
13 HttpSession ses = request.getSession();
14
15 //získanie jednotlivých parametrov zo session ako Integer
16 Integer Philips_DVD640_in = (Integer)ses.getAttribute("Philips_DVD640");
17 Integer Pioneer_DV_454_in = (Integer)ses.getAttribute("Pioneer_DV-454");
18 Integer Philips_28PW8707_in = (Integer)ses.getAttribute("Philips_28PW8707");
19 Integer Sony_KV_32FX68K_in = (Integer)ses.getAttribute("Sony_KV-32FX68K");
20
21 //zmena parametrov z Integer na int
22 int Philips_DVD640 = Philips_DVD640_in.intValue();
23 int Pioneer_DV_454 = Pioneer_DV_454_in.intValue();
24 int Philips_28PW8707 = Philips_28PW8707_in.intValue();
25 int Sony_KV_32FX68K = Sony_KV_32FX68K_in.intValue();
26
27 if (request.getParameter("submit").trim().equals("Vložiť tovar do košíka")) {
28
29 //inkrementácia jednotlivých parametrov pomocou objektu request
30 Philips_DVD640 += Integer.valueOf(request.getParameter("Philips DVD640")).intValue();
31 Pioneer_DV_454 += Integer.valueOf(request.getParameter("Pioneer DV-454")).intValue();
32 Philips_28PW8707 += Integer.valueOf(request.getParameter("Philips 28PW8707")).intValue();
33 Sony_KV_32FX68K += Integer.valueOf(request.getParameter("Sony KV-32FX68K")).intValue();
34
35 //vlozenie inkrementovanych parametrov do session
36 ses.setAttribute("Philips_DVD640",new Integer(Philips_DVD640));
37 ses.setAttribute("Pioneer_DV-454",new Integer(Pioneer_DV_454));
38 ses.setAttribute("Philips_28PW8707",new Integer(Philips_28PW8707));
39 ses.setAttribute("Sony_KV-32FX68K",new Integer(Sony_KV_32FX68K));
40
41 //presmerovanie výstupu na úvodnú stránku
42 RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/shop.jsp");
43 response.setContentType("text/html;charset=windows-1250");
44 if (dispatcher != null) dispatcher.forward(request,response);
45 }
46 else {
47 //vytvorenie správy
48 String msg = "";
49 if (Philips_DVD640 != 0) msg = " <b>Philips DVD640</b> v počte " +Philips_DVD640+ " ks, ";
50 if (Pioneer_DV_454 != 0) msg += " <b>Pioneer DV-454</b> v počte " +Pioneer_DV_454+ " ks, ";
51 if (Philips_28PW8707 != 0) msg += " <b>Philips 28PW8707</b> v počte " +Philips_28PW8707+ " ks, ";
52 if (Sony_KV_32FX68K != 0) msg += " <b>Sony KV-32FX68K</b> v počte " +Sony_KV_32FX68K+ " ks. ";
53 if (msg.equals("")) msg = "Žiadna objednávka, ďakujeme za návštevu.";
54
55 response.setContentType("text/html;charset=windows-1250");
56 PrintWriter out = response.getWriter();
57
58 //ukončenie session (vyprázdnenie košíka)
59 ses.invalidate();
60
61 out.println("<html><head><title>Objednávka zaslaná ...</title></head><body>");
62 out.println("<b>Výsledok vášho nakupovania:</b><hr><p>");
63 if (!msg.equals("Žiadna objednávka, ďakujeme za návštevu.")) {
64 out.println("Objednali ste si : " +msg+ " <p>");
65 out.println("<em>Ďakujeme za návštevu, váš košík bol vyprázdnený a objednávka odoslaná.</em><p>");
66 } else out.println(msg+ " <p>");
67 out.println("<A href='/interval/shop.jsp' >Znova ísť nakupovať </A>");
68 out.println("</body></html>");
69 }
70 }
71 }
|