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
c84abab8
Commit
c84abab8
authored
Oct 22, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Kalkulator4 : copy&paste
parent
4909af53
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
126 additions
and
1 deletions
+126
-1
Kalkulator1.java
PC23-Serwlety/src/main/java/kalkulator/Kalkulator1.java
+2
-1
Kalkulator2.java
PC23-Serwlety/src/main/java/kalkulator/Kalkulator2.java
+2
-0
Kalkulator3.java
PC23-Serwlety/src/main/java/kalkulator/Kalkulator3.java
+2
-0
Kalkulator4.java
PC23-Serwlety/src/main/java/kalkulator/Kalkulator4.java
+120
-0
No files found.
PC23-Serwlety/src/main/java/kalkulator/Kalkulator1.java
View file @
c84abab8
...
...
@@ -39,7 +39,8 @@ public class Kalkulator1 extends HttpServlet {
long
wynik
=
liczba1
+
liczba2
;
out
.
println
(
"<div>"
+
liczba1
+
" + "
+
liczba2
+
" = "
+
wynik
+
"</p>"
);
}
out
.
println
(
"</body>"
);
out
.
println
(
"</html>"
);
}
}
PC23-Serwlety/src/main/java/kalkulator/Kalkulator2.java
View file @
c84abab8
...
...
@@ -61,6 +61,8 @@ 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>"
);
}
}
...
...
PC23-Serwlety/src/main/java/kalkulator/Kalkulator3.java
View file @
c84abab8
...
...
@@ -61,6 +61,8 @@ 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>"
);
}
}
...
...
PC23-Serwlety/src/main/java/kalkulator/Kalkulator4.java
0 → 100644
View file @
c84abab8
package
kalkulator
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
javax.servlet.ServletException
;
import
javax.servlet.annotation.WebServlet
;
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
;
@Override
protected
void
doGet
(
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><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</h1>"
);
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>"
);
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>"
);
}
}
@Override
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><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</h1>"
);
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>"
);
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>"
);
}
}
}
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