Wednesday, 30 July 2014

getHeaders (String name)

public Enumeration<String> getHeaders(String name)
Returns all the values of the specified request header as an Enumeration of String objects.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form method="get" action="/servlet/HelloWorld">
            <input type="submit">
        </form>
    </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>HelloWorld</servlet-name>
        <servlet-class>HelloWorld</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/HelloWorld</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 java.util.*;

public class HelloWorld extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
       try(PrintWriter out = res.getWriter()) {
            res.setContentType("text/html");
            StringBuilder s1 = new StringBuilder();
            
            Enumeration<String> headers = req.getHeaderNames();
            
            while(headers.hasMoreElements()){
                String headerName = headers.nextElement();
                s1.append(headerName).append(":");
                
                /* Get the Header Values */
                Enumeration<String> headerValues = req.getHeaders(headerName);
                while(headerValues.hasMoreElements()){
                    s1.append(headerValues.nextElement());
                }
                s1.append("<br />");                
            }
            
            String str = "<html>" +
                         "<head>" +
                         "<title> Hello World </title>" +
                         "</head>" +
                         "<body>" +
                         "<h1> Headers are <br />" + s1 +
                         "</h1> </body>" +
                         "</html>";
            out.println(str);
       }
   } 
  
}

Output







Prevoius                                                 Next                                                 Home

GetHeaderNames() : Get all the header names

public Enumeration<String> getHeaderNames()
Returns an enumeration of all the header names this request contains. If the request has no headers, this method returns an empty enumeration.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form method="get" action="/servlet/HelloWorld">
            <input type="submit">
        </form>
    </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>HelloWorld</servlet-name>
        <servlet-class>HelloWorld</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/HelloWorld</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 java.util.*;

public class HelloWorld extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
       PrintWriter out =  null;
       try{
            out = res.getWriter();
            res.setContentType("text/html");
            StringBuilder s1 = new StringBuilder();
            
            Enumeration<String> headers = req.getHeaderNames();
            
            while(headers.hasMoreElements()){
                s1.append(headers.nextElement()).append("<br />");
            }
            
            String str = "<html>" +
                         "<head>" +
                         "<title> Hello World </title>" +
                         "</head>" +
                         "<body>" +
                         "<h1> Headers are <br />" + s1 +
                         "</h1> </body>" +
                         "</html>";
            out.println(str);
       }
       finally{
           if(out!=null){
               out.close();
           }
       }
   } 
  
}

Output



Prevoius                                                 Next                                                 Home

Accessing Headers

HttpServletRequest interface provides below methods to access headers.

    getHeader(String name)
    getHeaderNames()
    getHeaders(String name)

getHeader method returns the value of the specified request header as a String. There can be multiple headers with same name, if there are multiple headers with the same name, the getHeader returns the first header in the request.

The getHeaders method allows access to all the values associated with this header name.

Headers my contain string representation of integer and date. HttpServlet provides methods getIntHeader(String name) to get the integer value and getDateHeader(String name) to get the date as long value.


Prevoius                                                 Next                                                 Home

removeAttribute (String name) : Removes the Attribute

public void removeAttribute(String name)
Removes an attribute from this request. We don't use this method much, since attributes only persist as long as the request is being handled.

Related Links
Prevoius                                                 Next                                                 Home

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

Tuesday, 29 July 2014

setAttribute (String name, Object o) : Sets the Attribute

public void setAttribute(String name, Object o)
Stores an attribute in this request. Attributes are reset between requests. This method is most often used in conjunction with RequestDispatcher.

<!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>

<?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

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