Pin It

How to display request parameters in a JSP coming in a request ?.

Program to demonstrate how to display request parameters in a JSP coming in a request

request.jsp


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.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>User Information</title>
</head>
<body>

    <form action="entry.jsp" method="get">
        <table cellspacing="0" cellpadding="5" border="1">
            <tr>
                <td align="right">First Name :</td>
                <td><input type="text" name="firstName"></td>
            </tr>
            <tr>
                <td align="right">Last Name :</td>
                <td><input type="text" name="lastName"></td>
            </tr>
            <tr>
                <td align="right">Email Address :</td>
                <td><input type="text" name="emailAddress"></td>
            </tr>

            <tr>
                <td align="right">Gender :</td>
                <td><input type="checkbox" name="male" value="Male" checked>Male</td>

            </tr>
            <tr>
                <td align="right"></td>
                <td><input type="checkbox" name="female" value="Female">Female</td>
            </tr>

            <tr>
                <td align="right">Favorite Hobbies :</td>
                <td><input type="radio" name="hobby" value="TV">TV</td>
            </tr>

            <tr>
                <td align="right"></td>
                <td><input type="radio" name="hobby" value="MOVIES">MOVIES</td>
            </tr>

            </tr>

            <tr>
                <td align="right">Select a country ...</td>
                <td><select name="country" multiple>
                        <option value="USA" selected>United States
                        <option value="CANADA">Canada
                        <option value="INDIA">India
                </select></td>
            </tr>

            <tr>
                <td></td>
                <td align="left"></br> <input type="submit" value="Submit"></td>
            </tr>

        </table>
    </form>

</body>
</html>


entry.jsp


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.
70.
71.
72.
73.
74.
75.
76.
77.
78.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>User Information</title>
</head>
<body>

    <h2>
        Hello Friends !!! ....<br> Below are the important methods of
        request<br> object for the coming parameters ...
    </h2>

    <hr>

    <h2>1. request.getParameter(String param)</h2>
    <h3>
        String firstName = request.getParameter("firstname"); : <br>
        returns the value of html/jsp element submitted in the form <br>
        having name attribute as firstname
    </h3>
    <%
        String firstName = request.getParameter("firstName");
    %>
    <h5>
        First Name :
        <%=firstName%>
    </h5>
    </h3>
    <hr>
    <h2>2. request.getParameterValues(String param)</h2>
    <h3>
        String[ ] countries = request.getParameterValues("country"); : <br>
        returns an array of string values of html/jsp element submitted in the
        form <br> having name attribute as country
    </h3>
    <%
        String[] countries = request.getParameterValues("country");
        for(int i = 0; i < countries.length; i++) {
    %>
    <h5>
        Country :
        <%=i + 1%>
        <%=countries[i]%></h5>

    <%
        }
    %>

    <hr>
    <h2>3. request.getParameterNames()</h2>
    <h3>
        Enumeration countries = request.getParameterNames()); : <br>
        returns an Enumeration of parameter names of html/jsp element
        submitted <br>in the form having name attribute as country
    </h3>
    <%
        java.util.Enumeration params = request.getParameterNames());
        while(params.hasMoreElements())) {
            String paramName = (String) params.nextElement();
            String paramValue = request.getParameter(paramName);
    %>

    <h5><%=paramName%>
        has a value
        <%=paramValue%></h5>

    <%
        }
    %>
    <hr>

</body>
</html>

Output on the browser window :


request.jsp






























entry.jsp


Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot