-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMyServlet.java
More file actions
69 lines (57 loc) · 2.32 KB
/
MyServlet.java
File metadata and controls
69 lines (57 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package MyPackage;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Servlet implementation class MyServlet
*/
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String[] jokes = {
"Why do Java developers wear glasses? Because they don't C#!",
"I told my computer I needed a break, and now it won't stop sending me Kit Kat bars.",
"Why don't programmers like nature? It has too many bugs.",
"How many programmers does it take to change a light bulb? None, it's a hardware problem.",
"Why do programmers prefer dark mode? Because light attracts bugs.",
"What's a Java developer's favorite bean? The CoffeeBean!",
"Why was the Java developer sad? Because they didn't get a byte!",
"Why did the programmer quit their job? Because they didn't get arrays.",
"What do you call a programmer from Finland? Nerdic.",
"Why did the computer go to therapy? It had a hard drive."
};
/**
* @see HttpServlet#HttpServlet()
*/
public MyServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String str1 = request.getParameter("num1");
String str2 = request.getParameter("num2");
String str3 = request.getParameter("bt1");
int a = Integer.parseInt(str1);
int b = Integer.parseInt(str2);
int ans;
if(str3.equals("1")) ans= a+b;
else if(str3.equals("2")) ans=a-b;
else if(str3.equals("3")) ans=a*b;
else ans=a/b;
int randomIndex = (int) (Math.random() * jokes.length);
String randomJoke = jokes[randomIndex];
response.sendRedirect("result.jsp?ans="+ans+"&joke="+randomJoke);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}