Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_dzienna_15_09
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
java_dzienna_15_09
Commits
6a348825
Commit
6a348825
authored
Sep 29, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Pokazywanie zdjęć
parent
9c1e30e3
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
141 additions
and
1 deletions
+141
-1
PhotoUtil.java
PC26-SklepWeb/src/main/java/sklep/photo/PhotoUtil.java
+43
-0
Photo.java
PC26-SklepWeb/src/main/java/sklep/web/Photo.java
+47
-0
sklep.properties
PC26-SklepWeb/src/main/resources/sklep.properties
+2
-0
products7.jsp
PC26-SklepWeb/src/main/webapp/products7.jsp
+48
-0
styl.css
PC26-SklepWeb/src/main/webapp/styl.css
+1
-1
No files found.
PC26-SklepWeb/src/main/java/sklep/photo/PhotoUtil.java
0 → 100644
View file @
6a348825
package
sklep
.
photo
;
import
java.io.File
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
sklep.db.DBException
;
import
sklep.db.DBSettings
;
import
sklep.db.RecordNotFound
;
public
class
PhotoUtil
{
private
static
final
String
EXT
=
".jpg"
;
public
static
File
getFile
(
int
productId
)
throws
DBException
,
RecordNotFound
{
Path
path
=
getPath
(
productId
);
File
file
=
path
.
toFile
();
if
(
file
.
exists
())
{
return
file
;
}
else
{
throw
new
RecordNotFound
(
"Cannot read photo for product id = "
+
productId
);
}
}
public
static
byte
[]
readBytes
(
int
productId
)
throws
DBException
,
RecordNotFound
{
Path
path
=
getPath
(
productId
);
try
{
return
Files
.
readAllBytes
(
path
);
}
catch
(
IOException
e
)
{
System
.
err
.
println
(
e
);
throw
new
RecordNotFound
(
"Cannot read photo for product id = "
+
productId
);
}
}
private
static
Path
getPath
(
int
productId
)
throws
DBException
{
String
dir
=
DBSettings
.
load
().
getProperty
(
"photo_dir"
);
String
fileName
=
productId
+
EXT
;
return
Paths
.
get
(
dir
,
fileName
);
}
}
PC26-SklepWeb/src/main/java/sklep/web/Photo.java
0 → 100644
View file @
6a348825
package
sklep
.
web
;
import
java.io.IOException
;
import
javax.servlet.ServletException
;
import
javax.servlet.ServletOutputStream
;
import
javax.servlet.annotation.WebServlet
;
import
javax.servlet.http.HttpServlet
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
sklep.db.RecordNotFound
;
import
sklep.photo.PhotoUtil
;
@WebServlet
(
"/Photo"
)
public
class
Photo
extends
HttpServlet
{
private
static
final
long
serialVersionUID
=
1L
;
/* Ten serwlet wczytuje z dysku plik ze zdjęciem o podanym numerze (z parametru productId).
* Aby odesłać odpowiedź "binarną" (a nie tekstową) używamy getOutputStream() zamiast getWriter().
* Aby przeglądarka wiedziała, że otrzymuje grafikę, ustawiamy content-type image/jpeg.
*/
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
String
parametrId
=
request
.
getParameter
(
"productId"
);
if
(
parametrId
==
null
)
{
return
;
}
try
{
int
id
=
Integer
.
parseInt
(
parametrId
);
byte
[]
bytes
=
PhotoUtil
.
readBytes
(
id
);
response
.
setContentType
(
"image/jpeg"
);
ServletOutputStream
output
=
response
.
getOutputStream
();
output
.
write
(
bytes
);
output
.
close
();
}
catch
(
RecordNotFound
e
)
{
response
.
setStatus
(
404
);
response
.
setContentType
(
"text/plain"
);
response
.
setCharacterEncoding
(
"utf-8"
);
response
.
getWriter
().
println
(
"Nie ma zdjęcia dla produktu o nr "
+
parametrId
);
}
catch
(
Exception
e
)
{
response
.
setStatus
(
500
);
e
.
printStackTrace
();
}
}
}
PC26-SklepWeb/src/main/resources/sklep.properties
View file @
6a348825
...
...
@@ -3,3 +3,5 @@ driver_class=org.postgresql.Driver
user
=
kurs
password
=
abc123
photo_dir
=
/home/patryk/sklep/foto
# To jest komentarz
PC26-SklepWeb/src/main/webapp/products7.jsp
0 → 100644
View file @
6a348825
<
%@
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 7 JSP z filtrem i zdjęciami
</h1>
<p>
[
<a
href=
"index.html"
>
powrót do spisu treści
</a>
]
</p>
<
%
--
Za
pomoc
ą
klasy
ProductBean
odczytamy
z
bazy
list
ę
produkt
ó
w
w
postaci
obiektowej
.
--
%
>
<jsp:useBean
id=
"bean"
class=
"sklep.beans.ProductBean"
/>
<h2>
Filtr cen
</h2>
<form
id=
"wyszukiwarka"
method=
"get"
>
<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>
<
%
--
Kryteria
wyszukiwania
przys
ł
ane
w
parametrach
z
formularza
przekazuj
ę
do
bean
za
pomoc
ą
setter
ó
w
.
--
%
>
<jsp:setProperty
name=
"bean"
property=
"minPrice"
param=
"min_price"
/>
<jsp:setProperty
name=
"bean"
property=
"maxPrice"
param=
"max_price"
/>
<c:forEach
var=
"product"
items=
"${bean.filteredProducts}"
>
<div
class=
"product"
>
<img
class=
"photo"
src=
"Photo?productId=${product.productId}"
alt=
""
/>
<h3>
${product.productName}
</h3>
<div
class=
"price"
>
Cena: ${product.price}
</div>
<div
class=
"price"
>
VAT ${product.vat * 100}%
</div>
<c:if
test=
"${not empty product.description}"
>
<p>
${product.description}
</p>
</c:if>
</div>
</c:forEach>
<p>
[
<a
href=
"index.html"
>
powrót do spisu treści
</a>
]
</p>
</body>
</html>
PC26-SklepWeb/src/main/webapp/styl.css
View file @
6a348825
...
...
@@ -17,7 +17,7 @@ h2, h3, h4 {
border
:
solid
2px
blue
;
margin
:
1em
auto
1em
50px
;
padding
:
1em
;
background-color
:
#DDFFDD
;
background-color
:
white
;
width
:
800px
;
min-height
:
230px
;
clear
:
right
;
...
...
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