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
3031b2d1
Commit
3031b2d1
authored
Apr 05, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
kalkulator POST - refaktoryzacja
parent
6c552be9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
22 deletions
+47
-22
KalkulatorPost.java
PC24-Serwlety/src/main/java/formularze/KalkulatorPost.java
+47
-22
No files found.
PC24-Serwlety/src/main/java/formularze/KalkulatorPost.java
View file @
3031b2d1
...
...
@@ -14,10 +14,7 @@ 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"
);
// w tej wersji GET służy tylko do wyświetlenia pustego formularza
response
.
setContentType
(
"text/html"
);
response
.
setCharacterEncoding
(
"UTF-8"
);
PrintWriter
out
=
response
.
getWriter
();
...
...
@@ -27,37 +24,65 @@ public class KalkulatorPost extends HttpServlet {
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>"
);
wydrukujFormularz
(
out
);
if
(
parametr1
!=
null
&&
!
parametr1
.
isBlank
()
&&
parametr2
!=
null
&&
!
parametr2
.
isBlank
()
&&
operacja
!=
null
&&
!
operacja
.
isBlank
())
{
koniecStrony
(
out
);
}
@Override
protected
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
// doPost obsługuje przysłane dane z formularza
PrintWriter
out
=
poczatekStrony
(
response
);
wydrukujFormularz
(
out
);
try
{
String
parametr1
=
request
.
getParameter
(
"liczba1"
);
String
parametr2
=
request
.
getParameter
(
"liczba2"
);
String
operacja
=
request
.
getParameter
(
"operacja"
);
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>"
);
}
catch
(
Exception
e
)
{
out
.
println
(
"<div>Błąd: "
+
e
+
"</div>"
);
}
koniecStrony
(
out
);
}
private
void
koniecStrony
(
PrintWriter
out
)
{
out
.
println
(
"</body>"
);
out
.
println
(
"</html>"
);
}
@Override
protected
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
doGet
(
request
,
response
);
private
PrintWriter
poczatekStrony
(
HttpServletResponse
response
)
throws
IOException
{
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>"
);
return
out
;
}
private
void
wydrukujFormularz
(
PrintWriter
out
)
{
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>"
);
}
private
int
oblicz
(
int
liczba1
,
int
liczba2
,
String
operacja
)
{
return
switch
(
operacja
)
{
...
...
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