Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Create an application for set property into Header through servlet
Previous Home Next

In this program, header property set through 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>setHeaderServlet</servlet-name>
    <servlet-class>r4r.servlet.setHeaderServlet</servlet-class>
   </servlet>
	<servlet-mapping>
        <servlet-name>setHeaderServlet</servlet-name>
        <url-pattern>/setHeaderServlet</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="setHeaderServlet">setHeaderServlet</a>
        </form>
    </body>
</html>

Servlet Program

/*
 * Save as a setHeaderServlet.java
 */
package r4r.servlet;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

/**
 *
 * @author R4R
 */
public class setHeaderServlet extends HttpServlet {

 private int count = 0;

 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 setHeaderServlet at "
             + request.getContextPath() + "</h1>");
out.println(" <form name=\"form\" action=\
          "setHeaderServlet\" 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 {
  PrintWriter out = response.getWriter();

  try {
// Set Header Property
response.setHeader("Refresh", "10; URL=index.jsp");
response.setCharacterEncoding("charset=UTF-8");
response.setContentType("text/html");
response.setDateHeader("Expires", 10000); //expire page within 10 sec
response.setBufferSize(1 * 1024);  //1K buffer size
response.addIntHeader
	("AccessCount", ++count); //count number of time page access

// Get client Browser information
String browser = request.getHeader("User-Agent").toLowerCase();
if (browser.contains("msie")) {
 out.println("<B>Title : Microsoft Internet Explorer</B><BR>");
 out.println("Message : Currently used Microsoft Internet Explorer<BR>");
 out.println("Browser Properties = " + request.getHeader("User-Agent"));
} else if (browser.contains("firefox")) {
 out.println("<B>Title : Mozilla Firefox</B><BR>");
 out.println("Message : Currently used Mozilla Firefox<BR>");
 out.println("Browser Properties = " 
	 + request.getHeader("User-Agent"));
} else if (browser.contains("chrome")) {
 out.println("<B>Title : Google Chrome</B><BR>");
 out.println("Message : Currently used Google Chrome<BR>");
 out.println("Browser Properties = "
        + request.getHeader("User-Agent"));
} else if (browser.contains("opera")) {
 out.println("<B>Title : Opera </B><BR>");
 out.println("Message : Currently used Opera<BR>");
 out.println("Browser Properties = "
          + request.getHeader("User-Agent"));
} else if (browser.contains("safari")) {
 out.println("<B>Title : Apple Safari</B><BR>");
 out.println("Message : Currently used Apple Safari<BR>");
 out.println("Browser Properties = "
         + request.getHeader("User-Agent"));
} else if (browser.contains("Netscape")) {
 out.println("<B>Title : NetScape Browser</B><BR>");
 out.println("Message : Currently used NetScape Browser<BR>");
 out.println("Browser Properties = " 
	  + request.getHeader("User-Agent"));
} else {
 out.println("<B>Title : Unkonwn Browser</B><BR>");
 out.println
("Message : Sorry, Browser as well as Version property UnKnown<BR>");
 out.println("Browser Properties = " 
	   + request.getHeader("User-Agent"));
}
out.println
("<BR><BR><B>Don't Refresh page,expires within 10 <I>(seconds)</I></B>");
  } finally {
//free resource
out.flush();
out.close();
  }
 }

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