Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_dzienna_15_09
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_dzienna_15_09
Commits
0e9e6483
Commit
0e9e6483
authored
Sep 28, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
GET i POST oddzielnie
parent
824df137
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
22 deletions
+50
-22
Kalkulator3.java
PC25-Serwlety/src/main/java/kalkulator/Kalkulator3.java
+50
-22
No files found.
PC25-Serwlety/src/main/java/kalkulator/Kalkulator3.java
View file @
0e9e6483
...
@@ -12,6 +12,11 @@ import javax.servlet.http.HttpServletResponse;
...
@@ -12,6 +12,11 @@ import javax.servlet.http.HttpServletResponse;
@WebServlet
(
"/Kalkulator3"
)
@WebServlet
(
"/Kalkulator3"
)
public
class
Kalkulator3
extends
HttpServlet
{
public
class
Kalkulator3
extends
HttpServlet
{
private
static
final
long
serialVersionUID
=
1L
;
private
static
final
long
serialVersionUID
=
1L
;
// W tej wersji wyświeltenie pustego formularza jest realizowane metodą GET,
// o obsługa wypełnionego formularza, czyli obliczenie i podanie wyniku, metodą POST.
// Możemy oddzielnie zaimplementować metody GET i POST w metodach serwletu:
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
response
.
setContentType
(
"text/html"
);
response
.
setContentType
(
"text/html"
);
...
@@ -38,36 +43,59 @@ public class Kalkulator3 extends HttpServlet {
...
@@ -38,36 +43,59 @@ public class Kalkulator3 extends HttpServlet {
out
.
println
(
"<input name='liczba2' type='number'>"
);
out
.
println
(
"<input name='liczba2' type='number'>"
);
out
.
println
(
"<button>=</button>"
);
out
.
println
(
"<button>=</button>"
);
out
.
println
(
"</form>"
);
out
.
println
(
"</form>"
);
out
.
println
(
"</body>"
);
out
.
println
(
"</html>"
);
}
protected
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
response
.
setContentType
(
"text/html"
);
response
.
setCharacterEncoding
(
"utf-8"
);
PrintWriter
out
=
response
.
getWriter
();
out
.
println
(
"<!DOCTYPE html>"
);
out
.
println
(
"<html>"
);
out
.
println
(
"<head>"
);
out
.
println
(
"<title>Kalkulator serwletowy</title>"
);
out
.
println
(
"<link rel='stylesheet' type='text/css' href='styl.css'>"
);
out
.
println
(
"</head>"
);
out
.
println
(
"<body>"
);
out
.
println
(
"<h1>Kalkulator 3</h1>"
);
out
.
println
(
"<p>Wpisz liczby i wybierz działanie:</p>"
);
out
.
println
(
"<form method='post'>"
);
out
.
println
(
"<input name='liczba1' type='number'>"
);
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 name='liczba2' type='number'>"
);
out
.
println
(
"<button>=</button>"
);
out
.
println
(
"</form>"
);
String
param1
=
request
.
getParameter
(
"liczba1"
);
String
param1
=
request
.
getParameter
(
"liczba1"
);
String
param2
=
request
.
getParameter
(
"liczba2"
);
String
param2
=
request
.
getParameter
(
"liczba2"
);
String
operacja
=
request
.
getParameter
(
"operacja"
);
String
operacja
=
request
.
getParameter
(
"operacja"
);
if
(
param1
!=
null
&&
param2
!=
null
&&
operacja
!=
null
)
{
try
{
try
{
long
liczba1
=
Long
.
parseLong
(
param1
);
long
liczba1
=
Long
.
parseLong
(
param1
);
long
liczba2
=
Long
.
parseLong
(
param2
);
long
liczba2
=
Long
.
parseLong
(
param2
);
long
wynik
=
switch
(
operacja
)
{
long
wynik
=
switch
(
operacja
)
{
case
"+"
->
liczba1
+
liczba2
;
case
"+"
->
liczba1
+
liczba2
;
case
"-"
->
liczba1
-
liczba2
;
case
"-"
->
liczba1
-
liczba2
;
case
"*"
->
liczba1
*
liczba2
;
case
"*"
->
liczba1
*
liczba2
;
case
"/"
->
liczba1
/
liczba2
;
case
"/"
->
liczba1
/
liczba2
;
default
->
throw
new
IllegalArgumentException
(
"Nieznane działanie: "
+
operacja
);
default
->
throw
new
IllegalArgumentException
(
"Nieznane działanie: "
+
operacja
);
};
};
out
.
printf
(
"<div class='wynik'>%d %s %d = <strong>%d</strong></div>\n"
,
out
.
printf
(
"<div class='wynik'>%d %s %d = <strong>%d</strong></div>\n"
,
liczba1
,
operacja
,
liczba2
,
wynik
);
liczba1
,
operacja
,
liczba2
,
wynik
);
}
catch
(
NumberFormatException
e
)
{
}
catch
(
NumberFormatException
e
)
{
out
.
printf
(
"<div class='error'>Niepoprawny format liczby</div>\n"
);
out
.
printf
(
"<div class='error'>Niepoprawny format liczby</div>\n"
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
out
.
printf
(
"<div class='error'>Błąd: %s</div>\n"
,
e
.
getMessage
());
out
.
printf
(
"<div class='error'>Błąd: %s</div>\n"
,
e
.
getMessage
());
}
}
}
out
.
println
(
"</body>"
);
out
.
println
(
"</body>"
);
out
.
println
(
"</html>"
);
out
.
println
(
"</html>"
);
}
}
protected
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
doGet
(
request
,
response
);
}
}
}
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