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
8f6348aa
Commit
8f6348aa
authored
Apr 05, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Przykłady kalkulatorów
parent
383241b9
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
211 additions
and
0 deletions
+211
-0
pom.xml
PC24-Serwlety/pom.xml
+5
-0
Kalkulator.java
PC24-Serwlety/src/main/java/formularze/Kalkulator.java
+66
-0
Kalkulator_ApacheLang1.java
...lety/src/main/java/formularze/Kalkulator_ApacheLang1.java
+66
-0
Kalkulator_ApacheLang2.java
...lety/src/main/java/formularze/Kalkulator_ApacheLang2.java
+66
-0
index.html
PC24-Serwlety/src/main/webapp/index.html
+8
-0
No files found.
PC24-Serwlety/pom.xml
View file @
8f6348aa
...
@@ -30,5 +30,10 @@
...
@@ -30,5 +30,10 @@
<version>
9.1.0
</version>
<version>
9.1.0
</version>
<scope>
provided
</scope>
<scope>
provided
</scope>
</dependency>
</dependency>
<dependency>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-lang3
</artifactId>
<version>
3.12.0
</version>
</dependency>
</dependencies>
</dependencies>
</project>
</project>
PC24-Serwlety/src/main/java/formularze/Kalkulator.java
0 → 100644
View file @
8f6348aa
package
formularze
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
jakarta.servlet.ServletException
;
import
jakarta.servlet.annotation.WebServlet
;
import
jakarta.servlet.http.HttpServlet
;
import
jakarta.servlet.http.HttpServletRequest
;
import
jakarta.servlet.http.HttpServletResponse
;
@WebServlet
(
"/kalkulator"
)
public
class
Kalkulator
extends
HttpServlet
{
@Override
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
String
parametr1
=
request
.
getParameter
(
"liczba1"
);
String
parametr2
=
request
.
getParameter
(
"liczba2"
);
String
operacja
=
request
.
getParameter
(
"operacja"
);
response
.
setContentType
(
"text/html"
);
response
.
setCharacterEncoding
(
"UTF-8"
);
PrintWriter
out
=
response
.
getWriter
();
out
.
println
(
"<!DOCTYPE html>"
);
out
.
println
(
"<html><head>"
);
out
.
println
(
"<title>Kalkulator</title>"
);
out
.
println
(
"</head>"
);
out
.
println
(
"<body>"
);
out
.
println
(
"<form style='border:2px solid green; padding: 1em; margin: 2em 0'>"
);
out
.
println
(
"<input type='number' name='liczba1'>"
);
out
.
println
(
"<select name='operacja'>"
);
out
.
println
(
"<option value='+'>+</option>"
);
out
.
println
(
"<option value='-'>-</option>"
);
out
.
println
(
"<option value='*'>*</option>"
);
out
.
println
(
"<option value='/'>/</option>"
);
out
.
println
(
"</select>"
);
out
.
println
(
"<input type='number' name='liczba2'>"
);
out
.
println
(
"<button>Oblicz</button>"
);
out
.
println
(
"</form>"
);
if
(
parametr1
!=
null
&&
!
parametr1
.
isBlank
()
&&
parametr2
!=
null
&&
!
parametr2
.
isBlank
()
&&
operacja
!=
null
&&
!
operacja
.
isBlank
())
{
int
liczba1
=
Integer
.
parseInt
(
parametr1
);
int
liczba2
=
Integer
.
parseInt
(
parametr2
);
int
wynik
=
oblicz
(
liczba1
,
liczba2
,
operacja
);
out
.
println
(
"<div>Działanie: "
+
liczba1
+
" "
+
operacja
+
" "
+
liczba2
+
"</div>"
);
out
.
println
(
"<div>Wynik: "
+
wynik
+
"</div>"
);
}
out
.
println
(
"</body>"
);
out
.
println
(
"</html>"
);
}
private
int
oblicz
(
int
liczba1
,
int
liczba2
,
String
operacja
)
{
return
switch
(
operacja
)
{
case
"+"
->
liczba1
+
liczba2
;
case
"-"
->
liczba1
-
liczba2
;
case
"*"
->
liczba1
*
liczba2
;
case
"/"
->
liczba1
/
liczba2
;
default
->
throw
new
IllegalArgumentException
(
"Unexpected value: "
+
operacja
);
};
}
}
PC24-Serwlety/src/main/java/formularze/Kalkulator_ApacheLang1.java
0 → 100644
View file @
8f6348aa
package
formularze
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
org.apache.commons.lang3.StringUtils
;
import
jakarta.servlet.ServletException
;
import
jakarta.servlet.annotation.WebServlet
;
import
jakarta.servlet.http.HttpServlet
;
import
jakarta.servlet.http.HttpServletRequest
;
import
jakarta.servlet.http.HttpServletResponse
;
@WebServlet
(
"/kalkulator_lang1"
)
public
class
Kalkulator_ApacheLang1
extends
HttpServlet
{
@Override
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
String
parametr1
=
request
.
getParameter
(
"liczba1"
);
String
parametr2
=
request
.
getParameter
(
"liczba2"
);
String
operacja
=
request
.
getParameter
(
"operacja"
);
response
.
setContentType
(
"text/html"
);
response
.
setCharacterEncoding
(
"UTF-8"
);
PrintWriter
out
=
response
.
getWriter
();
out
.
println
(
"<!DOCTYPE html>"
);
out
.
println
(
"<html><head>"
);
out
.
println
(
"<title>Kalkulator</title>"
);
out
.
println
(
"</head>"
);
out
.
println
(
"<body>"
);
out
.
println
(
"<form style='border:2px solid green; padding: 1em; margin: 2em 0'>"
);
out
.
println
(
"<input type='number' name='liczba1'>"
);
out
.
println
(
"<select name='operacja'>"
);
out
.
println
(
"<option value='+'>+</option>"
);
out
.
println
(
"<option value='-'>-</option>"
);
out
.
println
(
"<option value='*'>*</option>"
);
out
.
println
(
"<option value='/'>/</option>"
);
out
.
println
(
"</select>"
);
out
.
println
(
"<input type='number' name='liczba2'>"
);
out
.
println
(
"<button>Oblicz</button>"
);
out
.
println
(
"</form>"
);
if
(
StringUtils
.
isNoneBlank
(
operacja
,
parametr1
,
parametr2
))
{
int
liczba1
=
Integer
.
parseInt
(
parametr1
);
int
liczba2
=
Integer
.
parseInt
(
parametr2
);
int
wynik
=
oblicz
(
liczba1
,
liczba2
,
operacja
);
out
.
println
(
"<div>Działanie: "
+
liczba1
+
" "
+
operacja
+
" "
+
liczba2
+
"</div>"
);
out
.
println
(
"<div>Wynik: "
+
wynik
+
"</div>"
);
}
out
.
println
(
"</body>"
);
out
.
println
(
"</html>"
);
}
private
int
oblicz
(
int
liczba1
,
int
liczba2
,
String
operacja
)
{
return
switch
(
operacja
)
{
case
"+"
->
liczba1
+
liczba2
;
case
"-"
->
liczba1
-
liczba2
;
case
"*"
->
liczba1
*
liczba2
;
case
"/"
->
liczba1
/
liczba2
;
default
->
throw
new
IllegalArgumentException
(
"Unexpected value: "
+
operacja
);
};
}
}
PC24-Serwlety/src/main/java/formularze/Kalkulator_ApacheLang2.java
0 → 100644
View file @
8f6348aa
package
formularze
;
import
static
org
.
apache
.
commons
.
lang3
.
ObjectUtils
.
allNotNull
;
import
static
org
.
apache
.
commons
.
lang3
.
StringUtils
.
trimToNull
;
import
static
org
.
apache
.
commons
.
lang3
.
math
.
NumberUtils
.
createInteger
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
jakarta.servlet.ServletException
;
import
jakarta.servlet.annotation.WebServlet
;
import
jakarta.servlet.http.HttpServlet
;
import
jakarta.servlet.http.HttpServletRequest
;
import
jakarta.servlet.http.HttpServletResponse
;
@WebServlet
(
"/kalkulator_lang2"
)
public
class
Kalkulator_ApacheLang2
extends
HttpServlet
{
@Override
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
Integer
liczba1
=
createInteger
(
trimToNull
(
request
.
getParameter
(
"liczba1"
)));
Integer
liczba2
=
createInteger
(
trimToNull
(
request
.
getParameter
(
"liczba2"
)));
String
operacja
=
request
.
getParameter
(
"operacja"
);
response
.
setContentType
(
"text/html"
);
response
.
setCharacterEncoding
(
"UTF-8"
);
PrintWriter
out
=
response
.
getWriter
();
out
.
println
(
"<!DOCTYPE html>"
);
out
.
println
(
"<html><head>"
);
out
.
println
(
"<title>Kalkulator</title>"
);
out
.
println
(
"</head>"
);
out
.
println
(
"<body>"
);
out
.
println
(
"<form style='border:2px solid green; padding: 1em; margin: 2em 0'>"
);
out
.
println
(
"<input type='number' name='liczba1'>"
);
out
.
println
(
"<select name='operacja'>"
);
out
.
println
(
"<option value='+'>+</option>"
);
out
.
println
(
"<option value='-'>-</option>"
);
out
.
println
(
"<option value='*'>*</option>"
);
out
.
println
(
"<option value='/'>/</option>"
);
out
.
println
(
"</select>"
);
out
.
println
(
"<input type='number' name='liczba2'>"
);
out
.
println
(
"<button>Oblicz</button>"
);
out
.
println
(
"</form>"
);
if
(
allNotNull
(
liczba1
,
liczba2
,
operacja
))
{
int
wynik
=
oblicz
(
liczba1
,
liczba2
,
operacja
);
out
.
println
(
"<div>Działanie: "
+
liczba1
+
" "
+
operacja
+
" "
+
liczba2
+
"</div>"
);
out
.
println
(
"<div>Wynik: "
+
wynik
+
"</div>"
);
}
out
.
println
(
"</body>"
);
out
.
println
(
"</html>"
);
}
private
int
oblicz
(
int
liczba1
,
int
liczba2
,
String
operacja
)
{
return
switch
(
operacja
)
{
case
"+"
->
liczba1
+
liczba2
;
case
"-"
->
liczba1
-
liczba2
;
case
"*"
->
liczba1
*
liczba2
;
case
"/"
->
liczba1
/
liczba2
;
default
->
throw
new
IllegalArgumentException
(
"Unexpected value: "
+
operacja
);
};
}
}
PC24-Serwlety/src/main/webapp/index.html
View file @
8f6348aa
...
@@ -7,6 +7,7 @@
...
@@ -7,6 +7,7 @@
<body>
<body>
<h1>
Przykłady serwletów – spis treści
</h1>
<h1>
Przykłady serwletów – spis treści
</h1>
<h2>
Podstawy
</h2>
<ul>
<ul>
<li><a
href=
"Hello"
>
Hello
</a></li>
<li><a
href=
"Hello"
>
Hello
</a></li>
<li><a
href=
'Ping'
>
Ping
</a>
- odczyt IP i nagłówków z zapytania
</li>
<li><a
href=
'Ping'
>
Ping
</a>
- odczyt IP i nagłówków z zapytania
</li>
...
@@ -14,5 +15,12 @@
...
@@ -14,5 +15,12 @@
<li><a
href=
"rozmowa"
>
Rozmowa
</a>
- przykład formularza
</li>
<li><a
href=
"rozmowa"
>
Rozmowa
</a>
- przykład formularza
</li>
</ul>
</ul>
<h2>
Kalkulator
</h2>
<ul>
<li><a
href=
"kalkulator"
>
wersja GET
</a>
- zadanie z zajęć
</li>
<li><a
href=
"kalkulator_lang1"
>
Apache Commons Lang
</a>
- StringUtils
</li>
<li><a
href=
"kalkulator_lang2"
>
Apache Commons Lang
</a>
- NumberUtils etc
</li>
</ul>
</body>
</body>
</html>
</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