Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Create an application for for getting information about Header
Previous Home Next

In this program, Header can be track into servlet.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
 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">
    <servlet>
     <servlet-name>HeaderTrackerServlet</servlet-name>
     <servlet-class>r4r.servlet.HeaderTrackerServlet</servlet-class>
    </servlet>
	<servlet-mapping>
        <servlet-name>HeaderTrackerServlet</servlet-name>
        <url-pattern>/HeaderTrackerServlet</url-pattern>
    </servlet-mapping>
	 <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

Index.jsp

<%-- 
    Document   : index.jsp
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type"
		    content="text/html; charset=UTF-8">
        <title>r4r.co.in-servlet</title>
    </head>
    <body>
        <form>
          <h1>Access Servlet by servlet's Name!</h1>
          <a href="HeaderTrackerServlet">HeaderTrackerServlet</a>
        </form>
    </body>
</html>

Servlet Program

/*
 * Save as a HeaderTrackerServlet.java
 */
package r4r.servlet;
import java.io.*;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
/**
 *
 * @author R4R
 */
public class HeaderTrackerServlet extends HttpServlet {

 protected void processRequest
 (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
  response.setContentType("text/html;charset=UTF-8");
  PrintWriter out = response.getWriter();
  try {
/* TODO output your page here */
out.println("<html>");
out.println("<head>");
out.println("<title>" + getServletInfo() + "</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet HeaderTrackerServlet at "
                  + request.getContextPath() + "</h1>");
out.println(" <form name=\"form\" action=\
            "HeaderTrackerServlet\" method=\"POST\">");
out.println
 ("<BR><input type=\"submit\" value=\"Header Tracker \" />");
out.println("</form>");
out.println("</body>");
out.println("</html>");
  } finally {
out.close();
  }
 }

 @Override
 protected void doGet
	 (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
  processRequest(request, response);
 }

 @Override
 protected void doPost
  (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
  response.setContentType("text/html;charset=UTF-8");
  PrintWriter out = response.getWriter();
  try {
out.println("<html>");
out.println("<head>");
out.println("<title>" + getServletInfo() + " </title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Information about Header </h1>");
out.println
 ("<table border=\"1\" cellspacing=\"2\" cellpadding=\"2\">");
out.println("<thead><tr>");
out.println("<th>Header Name</th>");
out.println("<th> Header value</th>");
out.println("</tr></thead>");

//Get Header Property from request
Enumeration<String> enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
 String string = enumeration.nextElement();
 //Display header property
 out.println("<tbody><tr>");
 out.println("<td> " + string + " </td>");
 out.println("<td> " + "<I><b>" 
	  + request.getHeader(string) + "</I></b>" + "</td>");
 out.println("</tr></tbody>");
}

out.println("</table>");
out.println("</body>");
out.println("</html>");
  } finally {
//free resource
out.flush();
out.close(); 
  }
 }

 @Override
 public String getServletInfo() {
  return "r4r.co.in- HeaderTrackerServlet";
 }
}
Output of Program
Previous Home Next