Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
alx_java2b_20250412
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
alx_java2b_20250412
Commits
6fbe257f
Commit
6fbe257f
authored
Apr 26, 2025
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ping i spis treści
parent
6658eb32
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
183 additions
and
0 deletions
+183
-0
Ping.java
PC22-Serwlety/src/main/java/serwlety/Ping.java
+164
-0
index.html
PC22-Serwlety/src/main/webapp/index.html
+19
-0
No files found.
PC22-Serwlety/src/main/java/serwlety/Ping.java
0 → 100644
View file @
6fbe257f
package
serwlety
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
java.time.LocalTime
;
import
java.util.Arrays
;
import
java.util.Enumeration
;
import
java.util.Map
;
import
jakarta.servlet.ServletException
;
import
jakarta.servlet.annotation.WebServlet
;
import
jakarta.servlet.http.Cookie
;
import
jakarta.servlet.http.HttpServlet
;
import
jakarta.servlet.http.HttpServletRequest
;
import
jakarta.servlet.http.HttpServletResponse
;
@WebServlet
(
"/ping"
)
public
class
Ping
extends
HttpServlet
{
private
static
final
long
serialVersionUID
=
1L
;
public
Ping
()
{
System
.
out
.
println
(
"Powstaje obiekt serwletu"
);
}
@Override
public
void
init
()
{
System
.
out
.
println
(
"init"
);
}
@Override
public
void
destroy
()
{
System
.
out
.
println
(
"destroy"
);
}
@Override
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
/* Gdy uruchamiamy / wgrywamy aplikację, serwer tworzy obiekt tej klasy (serwletu, np. Ping).
* Gdy przychodzi zapytanie HTTP typu GET skierowane pod adres tego serwletu (np. /Ping),
* serwer wywołuje metodę doGet i w obiekcie request przekazuje nam informację o zapytaniu (adres, parametry, w przypadku metody POST także treść),
* natomiast obiekt response dostajemy po to, aby za jego pomocą wygenerować odpowiedź.
*/
String
dt
=
LocalTime
.
now
().
toString
();
String
addr
=
request
.
getRemoteAddr
();
int
port
=
request
.
getRemotePort
();
String
userAgent
=
request
.
getHeader
(
"User-Agent"
);
System
.
out
.
println
(
dt
+
": zapytanie z adresu "
+
addr
+
":"
+
port
+
"\nz przeglądarki "
+
userAgent
);
response
.
setContentType
(
"text/plain"
);
response
.
setCharacterEncoding
(
"UTF-8"
);
response
.
addCookie
(
new
Cookie
(
"czas"
,
dt
));
PrintWriter
out
=
response
.
getWriter
();
out
.
println
(
"Halo halo, GET"
);
out
.
println
(
"ContextPath: "
+
request
.
getContextPath
());
out
.
println
(
"RequestURI: "
+
request
.
getRequestURI
());
out
.
println
(
"QueryString: "
+
request
.
getQueryString
());
out
.
println
();
out
.
println
(
"LocalName: "
+
request
.
getLocalName
());
out
.
println
(
"LocalAddr: "
+
request
.
getLocalAddr
());
out
.
println
(
"LocalPort: "
+
request
.
getLocalPort
());
out
.
println
();
out
.
println
(
"RemoteHost: "
+
request
.
getRemoteHost
());
out
.
println
(
"RemoteAddr: "
+
request
.
getRemoteAddr
()
+
":"
+
request
.
getRemotePort
());
// itd.
out
.
println
();
out
.
println
(
"parametr x: "
+
request
.
getParameter
(
"x"
));
out
.
println
(
"parametr y: "
+
request
.
getParameter
(
"y"
));
String
[]
wartosciX
=
request
.
getParameterValues
(
"x"
);
out
.
println
(
"x jako tablica: "
+
Arrays
.
toString
(
wartosciX
));
out
.
println
(
"Wszystkie parametry:"
);
for
(
Map
.
Entry
<
String
,
String
[]>
e
:
request
.
getParameterMap
().
entrySet
())
{
String
[]
v
=
e
.
getValue
();
out
.
println
(
" * "
+
e
.
getKey
()
+
" : "
+
Arrays
.
toString
(
v
));
}
out
.
println
();
out
.
println
(
"nagłówek Accept: "
+
request
.
getHeader
(
"Accept"
));
out
.
println
(
"nagłówek User-Agent: "
+
userAgent
);
out
.
println
(
"Wszystkie nagłówki:"
);
Enumeration
<
String
>
headerNames
=
request
.
getHeaderNames
();
while
(
headerNames
.
hasMoreElements
())
{
String
nm
=
headerNames
.
nextElement
();
out
.
println
(
nm
+
": "
+
request
.
getHeader
(
nm
));
}
out
.
println
();
out
.
println
(
"Wszystkie ciastka:"
);
String
poprzedniCzas
=
null
;
Cookie
[]
cookies
=
request
.
getCookies
();
if
(
cookies
!=
null
)
for
(
Cookie
cooky
:
cookies
)
{
out
.
println
(
"cookie "
+
cooky
.
getName
()
+
" "
+
cooky
.
getValue
());
if
(
"czas"
.
equals
(
cooky
.
getName
()))
{
poprzedniCzas
=
cooky
.
getValue
();
}
}
out
.
println
(
"czas teraz : "
+
dt
);
out
.
println
(
"czas poprzedni: "
+
poprzedniCzas
);
}
@Override
protected
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
String
contentType
=
request
.
getHeader
(
"Content-Type"
);
int
contentLenght
=
request
.
getContentLength
();
String
enc
=
request
.
getCharacterEncoding
();
response
.
setContentType
(
"text/plain"
);
response
.
setCharacterEncoding
(
"UTF-8"
);
PrintWriter
out
=
response
.
getWriter
();
out
.
println
(
"Halo halo, POST"
);
out
.
println
(
"Przyszła treść typu: "
+
contentType
+
" , enc: "
+
enc
);
out
.
println
(
"Rozmiar: "
+
contentLenght
);
out
.
println
();
// dane binarne: request.getInputStream()
try
(
BufferedReader
reader
=
request
.
getReader
())
{
out
.
println
(
"Cały tekst:"
);
String
line
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
out
.
println
(
line
);
}
}
}
@Override
protected
void
doPut
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
String
contentType
=
request
.
getHeader
(
"Content-Type"
);
int
contentLenght
=
request
.
getContentLength
();
String
enc
=
request
.
getCharacterEncoding
();
response
.
setContentType
(
"text/plain"
);
response
.
setCharacterEncoding
(
"UTF-8"
);
PrintWriter
out
=
response
.
getWriter
();
out
.
println
(
"Halo halo, PUT"
);
out
.
println
(
"Przyszła treść typu: "
+
contentType
+
" , enc: "
+
enc
);
out
.
println
(
"Rozmiar: "
+
contentLenght
);
out
.
println
();
try
(
BufferedReader
reader
=
request
.
getReader
())
{
out
.
println
(
"Cały tekst:"
);
String
line
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
out
.
println
(
line
);
}
}
}
@Override
protected
void
doDelete
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
response
.
setContentType
(
"text/plain"
);
response
.
setCharacterEncoding
(
"UTF-8"
);
PrintWriter
out
=
response
.
getWriter
();
out
.
println
(
"Halo halo, DELETE"
);
}
}
PC22-Serwlety/src/main/webapp/index.html
0 → 100644
View file @
6fbe257f
<!DOCTYPE html>
<html>
<head>
<meta
charset=
"UTF-8"
>
<title>
Aplikacja serwletowa
</title>
</head>
<body>
<h1>
Aplikacja serwletowa – spis treści
</h1>
<ul>
<li><a
href=
"index.html"
>
spis treści
</a></li>
<li><a
href=
"hello"
>
serwlet Hello
</a></li>
<li><a
href=
"ping"
>
ping
</a></li>
<li><a
href=
""
></a></li>
</ul>
</body>
</html>
\ No newline at end of file
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