postheadericon 13. Write a Servlet to display all the attributes available from request and context.


import javax.servlet.*;
import java.util.*;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;

@WebServlet(
name="WTAD",
urlPatterns={"/Ex13","/bipinrupadiya.blogspot.com"}
)
public class RequestAttributes2 extends HttpServlet
{
 
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<br> Server Port : " + request.getServerPort());
        out.println("<br> Server Name : " + request.getServerName());
        out.println("<br> Protocol    : " + request.getProtocol());
        out.println("<br> Character Encoding    : " + request.getCharacterEncoding());
        out.println("<br> Content Type    : " + request.getContentType());
        out.println("<br> Content Length  : " + request.getContentLength());
        out.println("<br> Remote Address  : " + request.getRemoteAddr());
        out.println("<br> Remote Host     : " + request.getRemoteHost());
     
out.println("<table border=1 align='CENTER'><tr bgcolor='#FFAD00'>") ;
out.println("<th>Header Name</th><th>Header Value</th></tr>");

        Enumeration parameters = request.getParameterNames();
        while(parameters.hasMoreElements())
        {
            String ParaName= (String) parameters.nextElement();
            out.println("<tr><td>" + ParaName+"</td>");
            out.println("<td>" + request.getParameter(ParaName)+"</td></tr>");          

        }
out.println("</table><br><br>");
out.println("<table border=1 align='CENTER'><tr bgcolor='#FFAD00'>") ;
out.println("<th>Attribute Name</th><th>Attribute Value</th></tr>");

        Enumeration attributes = request.getAttributeNames();
        while(attributes.hasMoreElements())
        {
            String attName = (String) attributes.nextElement();
            out.println("<tr><td>" + attName+"</td>");
            out.println("<td> " + request.getAttribute(attName)+"</td></tr>");          
        }
out.println("</table>");
    }
    public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}

}

0 comments:

Blog Archive

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.