Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Create an application for access new URL through client servlet
Previous Home Next

In this program, access new URL through client 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>clientPullServlet</servlet-name>
        <servlet-class>r4r.servlet.clientPullServlet</servlet-class>
    </servlet>
	<servlet-mapping>
        <servlet-name>clientPullServlet</servlet-name>
        <url-pattern>/clientPullServlet</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="clientPullServlet">clientPullServlet</a>
    </form>
    </body>
</html>

Servlet Program

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

   private final static String LOCATION_URI =
	           "http://r4r.co.in/";

   protected void processRequest
	(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
   response.setContentType("text/html;charset=UTF-8");
   PrintWriter out = response.getWriter();

   response.setBufferSize(1 * 1024); 
   // 1K buffer memory size set to body
   // Refresh interval and location set in page header
   response.setHeader("Refresh", "10; URL=" + LOCATION_URI);
   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 clientPullServlet at " 
	       + request.getContextPath() + "</h1>");
  out.println("<I><b>Don't Refresh this page</b></I>");
  out.println("<BR>This browser will take you a new Location within 10 "
                      + "<I>" + "( seconds )" + "</I>");
  out.println("<BR>New Location is : " + "<b>" + LOCATION_URI + "<b>");
  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 {
   processRequest(request, response);
    }

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