Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230928
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
javab_20230928
Commits
8913ded6
Commit
8913ded6
authored
Oct 18, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
obsługa POST i zapis produktów
parent
b2410629
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
3 deletions
+33
-3
EditProduct.java
PC25-SklepWeb/src/main/java/sklep/web/EditProduct.java
+33
-3
No files found.
PC25-SklepWeb/src/main/java/sklep/web/EditProduct.java
View file @
8913ded6
package
sklep
.
web
;
import
java.io.IOException
;
import
java.math.BigDecimal
;
import
jakarta.servlet.RequestDispatcher
;
import
jakarta.servlet.ServletException
;
...
...
@@ -17,7 +18,6 @@ import sklep.model.Product;
@WebServlet
(
"/edit"
)
public
class
EditProduct
extends
HttpServlet
{
@Override
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
...
...
@@ -40,11 +40,41 @@ public class EditProduct extends HttpServlet {
e
.
printStackTrace
();
}
}
// Forward to "wewnętrzne przekierowanie" obsługi zapytania do innego komponentu
// aplikacji.
// Forward to "wewnętrzne przekierowanie" obsługi zapytania do innego komponentu aplikacji.
// Tutaj "wyświetlamy" formularz edycji produktu.
RequestDispatcher
dispatcher
=
request
.
getRequestDispatcher
(
"product_form.jsp"
);
if
(
dispatcher
!=
null
)
dispatcher
.
forward
(
request
,
response
);
}
@Override
protected
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
// W tej wersji nie obsługujemy błędów - w razie błędu wyświetli się strona z wyjątkiem
// W przypadku braku ID zostanie utworzony nowy produkt, a w przypadku podania ID (gdy to była edycja istniejącego) - zostanie zastąpiony.
request
.
setCharacterEncoding
(
"UTF-8"
);
String
parametrId
=
request
.
getParameter
(
"productId"
);
Integer
productId
=
(
parametrId
==
null
||
parametrId
.
isEmpty
())
?
null
:
Integer
.
valueOf
(
parametrId
);
String
parametrPrice
=
request
.
getParameter
(
"price"
);
BigDecimal
price
=
new
BigDecimal
(
parametrPrice
);
String
parametrVat
=
request
.
getParameter
(
"vat"
);
BigDecimal
vat
=
(
parametrVat
==
null
||
parametrVat
.
isEmpty
())
?
null
:
new
BigDecimal
(
parametrVat
);
String
name
=
request
.
getParameter
(
"productName"
);
String
description
=
request
.
getParameter
(
"description"
);
Product
product
=
new
Product
(
productId
,
name
,
price
,
vat
,
description
);
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
productDAO
.
save
(
product
);
db
.
commit
();
// Gdy udało się zapisać, to przejdziemy z powrotem do listy.
// To jest przekierowanie przeglądarki do inny adres.
response
.
sendRedirect
(
"products9.jsp"
);
}
catch
(
DBException
e
)
{
e
.
printStackTrace
();
}
}
}
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