GTU WTAD Practical 13 : Write a Servlet to display all the attributes available from request and context.

GTU WTAD Practical 13 :  Write a Servlet to display all the attributes available from request and context.

GTU WTAD Practical 13

As I have explained earlier with other servlet program that Servlet is used to generate html pages as per the requirement of the user, Here I am going to explain how to use request object and contact object of servlet. Here i have displayed many parameters which is very useful for programmers to track the request of user and take some actions. Serverlet parameters Like IP Address, Port, Server Name etc.

Jump to GTU WTAD Practical 14

File Name : index.jsp

File Location : Tomcat_Apache\webapps\gtu13\

<%
	response.sendRedirect("./AvailableAttributesContext");
%>

 

File Name : web.xml

File Location : Tomcat_Apache\webapps\gtu13\WEB-INF

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">  
  
  <display-name>GTU WTAD Practical 13  </display-name>
  <description>Write a Servlet to display parameters available on request.</description>
  
  <servlet>
    <servlet-name>AvailableAttributesContext</servlet-name>
    <servlet-class>AvailableAttributesContext</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>AvailableAttributesContext</servlet-name>
      <url-pattern>/AvailableAttributesContext</url-pattern>
  </servlet-mapping> 
  
</web-app>

 

File Name : AvailableAttributesContext.java

File Location : Tomcat_Apache\webapps\gtu13\WEB-INF\classes

/*
		GTU WTAD Practical 13 :  Write a Servlet to display all the attributes available from request and context.
*/

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

public class AvailableAttributesContext extends HttpServlet
{
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html><head><title>GTU WTAD Practical 13 : Write a Servlet to display all the attributes available from request and context.</title></head><body><center><h1>GTU WTAD Practical 13 :  Write a Servlet to display all the attributes available from request and context.</h1></center><br><br>");
		
		//Display Header Name and Value in Table Format 
		out.println("<table border=1>");
		out.println("<tr><th>Request Context</th><th>Parameter Values</th></tr>");
		
		out.println("<tr><td><b>"+ "Local Name : getLocalName() " 		+ "</b></td><td> " + request.getLocalName() + "</td></tr>");
		out.println("<tr><td><b>"+ "Local Port : getLocalPort() " 		+ "</b></td><td> " + request.getLocalPort() + "</td></tr>");
		out.println("<tr><td><b>"+ "Protocol : getProtocol() " 			+ "</b></td><td> " + request.getProtocol() + "</td></tr>");
		out.println("<tr><td><b>"+ "Remote Address : getLocalAddr()" 	+ "</b></td><td> " + request.getLocalAddr() + "</td></tr>");
		out.println("<tr><td><b>"+ "Server Name : getServerName()" 		+ "</b></td><td> " + request.getServerName() + "</td></tr>");		
		out.println("<tr><td><b>"+ "Server Port : getServerPort() " 	+ "</b></td><td> " + request.getServerPort() + "</td></tr>");		
		out.println("<tr><td><b>"+ "Char Encoding: getCharacterEncoding()"+ "</b></td><td>" +request.getCharacterEncoding()+"</td></tr>");
		out.println("<tr><td><b>"+ "Content Type: getContentType() " 	+ "</b></td><td> " + request.getContentType() + "</td></tr>");
		out.println("<tr><td><b>"+ "Content Length: getContentLength()"	+ "</b></td><td> " + request.getContentLength()+"</td></tr>");		out.println("</table>");
		
		out.println("</body></html>");
	}
}

 Output

GTU WTAD Practical 13

Leave a Reply