Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_weekendowa_20221008
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_weekendowa_20221008
Commits
831573fc
Commit
831573fc
authored
Oct 22, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
kalkulator4: rozróżnienie GET i POST
parent
c84abab8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
24 additions
and
52 deletions
+24
-52
Kalkulator2.java
PC23-Serwlety/src/main/java/kalkulator/Kalkulator2.java
+2
-2
Kalkulator3.java
PC23-Serwlety/src/main/java/kalkulator/Kalkulator3.java
+2
-2
Kalkulator4.java
PC23-Serwlety/src/main/java/kalkulator/Kalkulator4.java
+20
-48
No files found.
PC23-Serwlety/src/main/java/kalkulator/Kalkulator2.java
View file @
831573fc
...
...
@@ -61,9 +61,9 @@ public class Kalkulator2 extends HttpServlet {
}
catch
(
Exception
e
)
{
out
.
printf
(
"<div class='error'>Błąd: %s</div>\n"
,
e
.
getMessage
());
}
out
.
println
(
"</body>"
);
out
.
println
(
"</html>"
);
}
out
.
println
(
"</body>"
);
out
.
println
(
"</html>"
);
}
}
PC23-Serwlety/src/main/java/kalkulator/Kalkulator3.java
View file @
831573fc
...
...
@@ -61,9 +61,9 @@ public class Kalkulator3 extends HttpServlet {
}
catch
(
Exception
e
)
{
out
.
printf
(
"<div class='error'>Błąd: %s</div>\n"
,
e
.
getMessage
());
}
out
.
println
(
"</body>"
);
out
.
println
(
"</html>"
);
}
out
.
println
(
"</body>"
);
out
.
println
(
"</html>"
);
}
@Override
...
...
PC23-Serwlety/src/main/java/kalkulator/Kalkulator4.java
View file @
831573fc
...
...
@@ -9,8 +9,6 @@ import javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
org.apache.commons.lang3.StringUtils
;
@WebServlet
(
"/Kalkulator4"
)
public
class
Kalkulator4
extends
HttpServlet
{
private
static
final
long
serialVersionUID
=
1L
;
...
...
@@ -38,32 +36,8 @@ public class Kalkulator4 extends HttpServlet {
out
.
println
(
"<input type='number' name='liczba2'>"
);
out
.
println
(
"<button>Oblicz</button>"
);
out
.
println
(
"</form>"
);
String
param1
=
request
.
getParameter
(
"liczba1"
);
String
param2
=
request
.
getParameter
(
"liczba2"
);
String
operacja
=
request
.
getParameter
(
"operacja"
);
if
(
StringUtils
.
isNoneBlank
(
param1
,
param2
,
operacja
))
{
try
{
long
liczba1
=
Long
.
parseLong
(
param1
);
long
liczba2
=
Long
.
parseLong
(
param2
);
long
wynik
=
switch
(
operacja
)
{
case
"+"
->
liczba1
+
liczba2
;
case
"-"
->
liczba1
-
liczba2
;
case
"*"
->
liczba1
*
liczba2
;
case
"/"
->
liczba1
/
liczba2
;
default
->
throw
new
IllegalArgumentException
(
"Nieznane działanie: "
+
operacja
);
};
out
.
printf
(
"<div class='wynik'>%d %s %d = <strong>%d</strong></div>\n"
,
liczba1
,
operacja
,
liczba2
,
wynik
);
}
catch
(
NumberFormatException
e
)
{
out
.
printf
(
"<div class='error'>Niepoprawny format liczby</div>\n"
);
}
catch
(
Exception
e
)
{
out
.
printf
(
"<div class='error'>Błąd: %s</div>\n"
,
e
.
getMessage
());
}
out
.
println
(
"</body>"
);
out
.
println
(
"</html>"
);
}
out
.
println
(
"</body>"
);
out
.
println
(
"</html>"
);
}
@Override
...
...
@@ -94,27 +68,25 @@ public class Kalkulator4 extends HttpServlet {
String
param2
=
request
.
getParameter
(
"liczba2"
);
String
operacja
=
request
.
getParameter
(
"operacja"
);
if
(
StringUtils
.
isNoneBlank
(
param1
,
param2
,
operacja
))
{
try
{
long
liczba1
=
Long
.
parseLong
(
param1
);
long
liczba2
=
Long
.
parseLong
(
param2
);
long
wynik
=
switch
(
operacja
)
{
case
"+"
->
liczba1
+
liczba2
;
case
"-"
->
liczba1
-
liczba2
;
case
"*"
->
liczba1
*
liczba2
;
case
"/"
->
liczba1
/
liczba2
;
default
->
throw
new
IllegalArgumentException
(
"Nieznane działanie: "
+
operacja
);
};
out
.
printf
(
"<div class='wynik'>%d %s %d = <strong>%d</strong></div>\n"
,
liczba1
,
operacja
,
liczba2
,
wynik
);
}
catch
(
NumberFormatException
e
)
{
out
.
printf
(
"<div class='error'>Niepoprawny format liczby</div>\n"
);
}
catch
(
Exception
e
)
{
out
.
printf
(
"<div class='error'>Błąd: %s</div>\n"
,
e
.
getMessage
());
}
out
.
println
(
"</body>"
);
out
.
println
(
"</html>"
);
try
{
long
liczba1
=
Long
.
parseLong
(
param1
);
long
liczba2
=
Long
.
parseLong
(
param2
);
long
wynik
=
switch
(
operacja
)
{
case
"+"
->
liczba1
+
liczba2
;
case
"-"
->
liczba1
-
liczba2
;
case
"*"
->
liczba1
*
liczba2
;
case
"/"
->
liczba1
/
liczba2
;
default
->
throw
new
IllegalArgumentException
(
"Nieznane działanie: "
+
operacja
);
};
out
.
printf
(
"<div class='wynik'>%d %s %d = <strong>%d</strong></div>\n"
,
liczba1
,
operacja
,
liczba2
,
wynik
);
}
catch
(
NumberFormatException
e
)
{
out
.
printf
(
"<div class='error'>Niepoprawny format liczby</div>\n"
);
}
catch
(
Exception
e
)
{
out
.
printf
(
"<div class='error'>Błąd: %s</div>\n"
,
e
.
getMessage
());
}
out
.
println
(
"</body>"
);
out
.
println
(
"</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