Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20230403
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
20230403
Commits
1f255ec0
Commit
1f255ec0
authored
Apr 24, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Koszyk w servletContext
parent
a1bf0c69
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
104 additions
and
0 deletions
+104
-0
AddToBasket.java
PC25-SklepWeb/src/main/java/sklep/basket/AddToBasket.java
+39
-0
products8.jsp
PC25-SklepWeb/src/main/webapp/products8.jsp
+65
-0
No files found.
PC25-SklepWeb/src/main/java/sklep/basket/AddToBasket.java
0 → 100644
View file @
1f255ec0
package
sklep
.
basket
;
import
java.io.IOException
;
import
jakarta.servlet.ServletException
;
import
jakarta.servlet.annotation.WebServlet
;
import
jakarta.servlet.http.HttpServlet
;
import
jakarta.servlet.http.HttpServletRequest
;
import
jakarta.servlet.http.HttpServletResponse
;
import
sklep.db.DBConnection
;
import
sklep.db.ProductDAO
;
import
sklep.model.Product
;
@WebServlet
(
"/add_to_basket"
)
public
class
AddToBasket
extends
HttpServlet
{
private
static
final
long
serialVersionUID
=
1L
;
@Override
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
try
{
int
productId
=
Integer
.
parseInt
(
request
.
getParameter
(
"productId"
));
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
Product
product
=
productDAO
.
findById
(
productId
);
Basket
basket
=
(
Basket
)
getServletContext
().
getAttribute
(
"basket"
);
if
(
basket
==
null
)
{
basket
=
new
Basket
();
getServletContext
().
setAttribute
(
"basket"
,
basket
);
}
basket
.
addProduct
(
product
);
}
}
catch
(
Exception
e
)
{
// ignorujemy błędy
}
}
}
PC25-SklepWeb/src/main/webapp/products8.jsp
0 → 100644
View file @
1f255ec0
<
%@
page
language=
"java"
contentType=
"text/html; charset=UTF-8"
pageEncoding=
"UTF-8"
%
>
<
%@
taglib
prefix=
"c"
uri=
"http://java.sun.com/jsp/jstl/core"
%
>
<!DOCTYPE html>
<html>
<head>
<meta
charset=
"UTF-8"
>
<title>
Lista produktów 7
</title>
<link
rel=
"stylesheet"
type=
"text/css"
href=
"styl.css"
>
</head>
<body>
<h1>
Lista produktów - wersja 8
</h1>
<jsp:useBean
id=
"bean"
class=
"sklep.web.ProductBean"
/>
<div
class=
"koszyk"
>
<h4>
Koszyk
</h4>
<ul>
<
%
--
Zauwa
ż
my
,
ż
e
dla
obiektu
koszyk
nie
wykonujemy
ju
ż
useBean
.
Po
prostu
zak
ł
adamy
,
ż
e
jest
obecny
(
w
sesji
).
Gdyby
go
nie
by
ł
o
,
to
p
ę
tla
si
ę
nie
wykona
.
--
%
>
<c:forEach
var=
"elm"
items=
"${basket.elements}"
>
<li>
${elm.productName} (${elm.quantity}) za
<b>
${elm.value}
</b></li>
</c:forEach>
</ul>
<p
class=
"total"
>
Do zapłaty: ${basket.totalValue}
</p>
</div>
<form
id=
"wyszukiwarka"
method=
"get"
>
<h2>
Filtr cen
</h2>
<table
class=
"formularz"
>
<tr><td><label
for=
"min_price"
>
Cena minimalna:
</label></td>
<td><input
type=
"number"
name=
"min_price"
value=
"${param.min_price}"
></td></tr>
<tr><td><label
for=
"max_price"
>
Cena maksymalna:
</label></td>
<td><input
type=
"number"
name=
"max_price"
value=
"${param.max_price}"
></td></tr>
<tr><td><button>
Szukaj
</button></td></tr>
</table>
</form>
<div>
Produkty o cenach
<c:if
test=
"${not empty(param.min_price)}"
>
od ${param.min_price}
</c:if>
<c:if
test=
"${not empty(param.max_price)}"
>
do ${param.max_price}
</c:if>
</div>
<jsp:setProperty
name=
"bean"
property=
"minPrice"
param=
"min_price"
/>
<jsp:setProperty
name=
"bean"
property=
"maxPrice"
param=
"max_price"
/>
<c:forEach
var=
"p"
items=
"${bean.filteredProducts}"
>
<div
class=
"product"
>
<img
class=
"photo"
src=
"photo?productId=${p.productId}"
alt=
""
/>
<h3>
${p.productName}
</h3>
<div
class=
"price"
>
Cena: ${p.price}
</div>
<div
class=
"price"
>
VAT ${p.vat * 100}%
</div>
<c:if
test=
"${not empty p.description}"
>
<p>
${p.description}
</p>
</c:if>
<div><a
href=
"add_to_basket?productId=${p.productId}"
>
dodaj do koszyka
</a></div>
</div>
</c:forEach>
</body>
</html>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment