Wednesday 30 July 2014

getAttributeNames() : Get All the Attribute names

Enumeration<String> getAttributeNames()
Returns an Enumeration containing the names of the attributes available to this request. This method returns an empty Enumeration if the request has no attributes available to it.

Register.jsp
<!DOCTYPE html>
<html>
    <head>
        <title>User Registration</title>
    </head>
    <body>
        <noscript>Java Script functionality disabled, Please enable it</noscript>
        <form id="formRegistration" action="/servlet/GetAttributes" method="post">
            <table id="registrationTable">
                <caption>Please fill the details to Register</caption>
                <tr>
                    <td>Mail Id:<span class="required1"><sup>*</sup></span></td>
                    <td><input type="text" name="mailID" id="mailID" required="required" /></td>
                </tr>
                <tr>
                    <td>Country<span class="required1"><sup>*</sup></span></td>
                    <td><input type="text" name="country" id="confPasswd" required="required" /></td>
                </tr>
                <tr>
                    <td>Password:<span class="required1"><sup>*</sup></span></td>
                    <td><input type="password" name="passwd" id="passwd" required="required" /></td>
                </tr>
                <tr>
                    <td>Confirm Password<span class="required1"><sup>*</sup></span></td>
                    <td><input type="password" name="confPasswd" id="confPasswd" required="required" /></td>
                </tr>
                
                <tr>
                    <td><input type="submit" value="Register" id="register" /></td>
                    <td><input type="reset" value="Clear Data" /></td>             
                </tr>
            </table>
        </form>
    </body>
</html>

Welcome.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Welcome</title>
    </head>
    <%@ page import="java.util.*"%>
    
    <%
        Enumeration<String> attr = request.getAttributeNames();
        StringBuilder str = new StringBuilder();
        
        while(attr.hasMoreElements()){
            str.append(attr.nextElement() +"<br />");
        }
     %>
    <body>
        <h1>Attributes are <%= str%>.</h1>
    </body>
</html>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>GetAttributes</servlet-name>
        <servlet-class>GetAttributes</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>GetAttributes</servlet-name>
        <url-pattern>/GetAttributes</url-pattern>
    </servlet-mapping>
</web-app>

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;

public class GetAttributes extends HttpServlet {
    
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{
        PrintWriter out = res.getWriter();

        String userName = req.getParameter("mailID");
        String password = req.getParameter("passwd");
        String conPassword = req.getParameter("confPasswd");
        String myCountry = req.getParameter("country");
        
        String welcome = "Welcome " + userName;
        String country = "You are from " + myCountry;
        
        req.setAttribute("welcome", welcome);
        req.setAttribute("country", country);
        
        RequestDispatcher rd = req.getRequestDispatcher("Welcome.jsp");
        rd.forward(req, res);
    }
}

Output









Prevoius                                                 Next                                                 Home

No comments:

Post a Comment