1
+
1
2
// Import required java libraries
2
3
import java .io .*;
3
4
import javax .servlet .*;
6
7
7
8
// Extend HttpServlet class
8
9
public class ReadParams extends HttpServlet {
9
-
10
+
10
11
/**
11
- *
12
- */
13
- private static final long serialVersionUID = 1L ;
12
+ *
13
+ */
14
+ private static final long serialVersionUID = 1L ;
14
15
15
- // Method to handle GET method request.
16
+ // Method to handle GET method request.
16
17
public void doGet (HttpServletRequest request , HttpServletResponse response )
17
- throws ServletException , IOException {
18
-
18
+ throws ServletException , IOException {
19
+
19
20
// Set response content type
20
21
response .setContentType ("text/html" );
21
22
22
23
PrintWriter out = response .getWriter ();
23
24
String title = "Reading All Form Parameters" ;
24
- String docType =
25
- "<!doctype html public \" -//w3c//dtd html 4.0 " + "transitional//en\" >\n " ;
25
+ String docType = "<!doctype html public \" -//w3c//dtd html 4.0 " + "transitional//en\" >\n " ;
26
26
27
27
out .println (docType +
28
- "<html>\n " +
29
- "<head><title>" + title + "</title></head>\n " +
30
- "<body bgcolor = \" #f0f0f0\" >\n " +
31
- "<h1 align = \" center\" >" + title + "</h1>\n " +
32
- "<table width = \" 100%\" border = \" 1\" align = \" center\" >\n " +
33
- "<tr bgcolor = \" #949494\" >\n " +
28
+ "<html>\n " +
29
+ "<head><title>" + title + "</title></head>\n " +
30
+ "<body bgcolor = \" #f0f0f0\" >\n " +
31
+ "<h1 align = \" center\" >" + title + "</h1>\n " +
32
+ "<table width = \" 100%\" border = \" 1\" align = \" center\" >\n " +
33
+ "<tr bgcolor = \" #949494\" >\n " +
34
34
"<th>Param Name</th>" +
35
- "<th>Param Value(s)</th>\n " +
36
- "</tr>\n "
37
- );
35
+ "<th>Param Value(s)</th>\n " +
36
+ "</tr>\n " );
38
37
39
38
Enumeration paramNames = request .getParameterNames ();
40
39
41
- while (paramNames .hasMoreElements ()) {
42
- String paramName = (String )paramNames .nextElement ();
40
+ while (paramNames .hasMoreElements ()) {
41
+ String paramName = (String ) paramNames .nextElement ();
43
42
out .print ("<tr><td>" + paramName + "</td>\n <td>" );
44
43
String [] paramValues = request .getParameterValues (paramName );
45
44
@@ -48,25 +47,25 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
48
47
String paramValue = paramValues [0 ];
49
48
if (paramValue .length () == 0 )
50
49
out .println ("<i>No Value</i>" );
51
- else
50
+ else
52
51
out .println (paramValue );
53
52
} else {
54
53
// Read multiple valued data
55
54
out .println ("<ul>" );
56
55
57
- for (int i = 0 ; i < paramValues .length ; i ++) {
56
+ for (int i = 0 ; i < paramValues .length ; i ++) {
58
57
out .println ("<li>" + paramValues [i ]);
59
58
}
60
59
out .println ("</ul>" );
61
60
}
62
61
}
63
62
out .println ("</tr>\n </table>\n </body></html>" );
64
63
}
65
-
64
+
66
65
// Method to handle POST method request.
67
66
public void doPost (HttpServletRequest request , HttpServletResponse response )
68
- throws ServletException , IOException {
69
-
67
+ throws ServletException , IOException {
68
+
70
69
doGet (request , response );
71
70
}
72
71
}
0 commit comments