Tuesday 29 July 2014

getAttribute (String name) : Get the Value associated with attribue name

public Object getAttribute(String name)
Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.

<!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>
    <%
        Object obj1 = request.getAttribute("welcome");
        Object obj2 = request.getAttribute("country");
        String welcome = (String) obj1;
        String country = (String) obj2;
     %>
    <body>
        <h1><%= welcome%>. <%= country %>.</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