-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp11.java
More file actions
51 lines (48 loc) · 1.4 KB
/
App11.java
File metadata and controls
51 lines (48 loc) · 1.4 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
// index.html
<html>
<head>
<title>VoterApp</title>
</head>
<body>
<form action= "http://localhost:8080/Proj2/check" method="get">
<fieldset style="width:20%; background-color:#80ffcc">
<table>
<tr><td>Name</td><td><input type="text" name="name"></td></tr>
<tr><td>Age</td><td><input type="text" name="age"></td></tr>
<tr><td></td>
<td><input type = "submit" value="Check Eligibility"></td></tr>
</table>
</fieldset>
</form>
</body>
</html>
// App.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class App extends HttpServlet {
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException,ServletException {
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
String name = req.getParameter("name");
int age = Integer.parseInt(req.getParameter("age"));
if (age>=18) {
pw.println("<font color='green' size='4'>Welcome "+name+" to this site</font>");
}
else {
pw.println("<font color='red' size='4'>Hello "+name+", you are not authorized to visit the site</font>");
pw.println("<br><br><a href='index.html'>back</a>");
}
pw.close();
}
}
// web.xml
<servlet>
<servlet-name>App</servlet-name>
<servlet-class>App</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>App</servlet-name>
<url-pattern>/check</url-pattern>
</servlet-mapping>