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
6c552be9
Commit
6c552be9
authored
Apr 05, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
kalkulator POST - początkowa wersja
parent
8f6348aa
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
0 deletions
+73
-0
KalkulatorPost.java
PC24-Serwlety/src/main/java/formularze/KalkulatorPost.java
+72
-0
index.html
PC24-Serwlety/src/main/webapp/index.html
+1
-0
No files found.
PC24-Serwlety/src/main/java/formularze/KalkulatorPost.java
0 → 100644
View file @
6c552be9
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_post"
)
public
class
KalkulatorPost
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 method='post'>"
);
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>"
);
}
@Override
protected
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
doGet
(
request
,
response
);
}
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 @
6c552be9
...
@@ -18,6 +18,7 @@
...
@@ -18,6 +18,7 @@
<h2>
Kalkulator
</h2>
<h2>
Kalkulator
</h2>
<ul>
<ul>
<li><a
href=
"kalkulator"
>
wersja GET
</a>
- zadanie z zajęć
</li>
<li><a
href=
"kalkulator"
>
wersja GET
</a>
- zadanie z zajęć
</li>
<li><a
href=
"kalkulator_post"
>
wersja POST
</a></li>
<li><a
href=
"kalkulator_lang1"
>
Apache Commons Lang
</a>
- StringUtils
</li>
<li><a
href=
"kalkulator_lang1"
>
Apache Commons Lang
</a>
- StringUtils
</li>
<li><a
href=
"kalkulator_lang2"
>
Apache Commons Lang
</a>
- NumberUtils etc
</li>
<li><a
href=
"kalkulator_lang2"
>
Apache Commons Lang
</a>
- NumberUtils etc
</li>
</ul>
</ul>
...
...
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