Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

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

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

Servlet Program

/*
 * Save as a sendRedirectServlet.java
 */
package r4r.servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
 *
 * @author R4R
 */
public class sendRedirectServlet 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 clientPullServlet at " 
	     + request.getContextPath() + "</h1>");
  out.println("<form action=\"sendRedirectServlet\
                      " method=\"POST\">");
  out.println("Enter WebSite URL: <input type=\
   "text\" name=\"webURL\" value=\"\" size=\"50\" /><BR/>");
  out.println("<input type=\"submit\" value=\
                "URL SendRedirect\" name=\"redirect\"/>");
  out.println("<input type=\"submit\" value=\
              "URL Forward \" name=\"forward\"/>");
   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();
   response.setBufferSize(1 * 1024); // 1K buffer memory size set to body

   //Get input URL form TextField
   String webURL = request.getParameter("webURL");
   try {
  if (!webURL.equals("")) {
 if (request.getParameter("redirect") != null) {
// used page redirect for forward request to another server
response.sendRedirect("http://" + webURL);
 } else {
/*
 *Interface RequestDispatcher is capable to receives requests from the client
 *and sends them to any resource (such as a servlet, HTML file, or JSP file)
 * on the same server.
 */
// used RequestDispatcher for froward request to server
RequestDispatcher rd = 
	getServletContext().getRequestDispatcher("/requestPage.jsp");
request.setAttribute("webURL", "http://" + webURL); //set attribute
rd.forward(request, response); // forward request
 }
  }
   } finally {
  //free resource
  out.flush();
  out.close();
   }
    }

    @Override
    public String getServletInfo() {
   return "r4r.co.in-sendRedirectServlet";
    }
}

RequestPage.jsp

<%-- 
    Document   : requestPage.jsp
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
   <head>
 <!--   <meta http-equiv="Content-Type" 
          content="text/html; charset=UTF-8">-->
  <meta http-equiv="Refresh" content="1;
                URL=<%=request.getAttribute("webURL")%>">
  <title>r4r.co.in-IntermediatePage</title>
   </head>
    <body>
     <p>Intermediate Page! Page refresh within 1 sec 
	  and redirected to requested URL</p>
    </body>
</html>
Output of Program
Previous Home Next