Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Create an application for access web.xml files parameters into Servlet
Previous Home Next

In this Servlet application, data is fetched from web.xml file and sends error to web.xml file to provoke respectively an error page on client site through Servlet.

Application directory structure

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>XMLServlet</servlet-name>
  <servlet-class>r4r.Servlet.XMLServlet</servlet-class>
  <init-param>
   <description>banned sites list which administrator wants to block
                 </description>
   <param-name>BannedSites</param-name>
   <param-value> 
     www.r4r.co.in 
     www.google.co.in
     www.google.com
     www.bing.com
   </param-value>
  </init-param>
 </servlet>
 <servlet-mapping>
  <servlet-name>XMLServlet</servlet-name>
  <url-pattern>/XMLServlet</url-pattern>
 </servlet-mapping>
 <session-config>
  <session-timeout>
    30
  </session-timeout>
 </session-config>
 <error-page>
  <!-- Provoke 403 error page when needed through servlet-->
  <error-code>403</error-code>
  <location>/403Error.jsp</location>
 </error-page>
</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-Index</title>
 </head>
 <body>
  <h1>Access Web page through webURL!</h1>
  <form action="XMLServlet" method="POST">
    Enter webURL: <input type="text" name="webURL" 
					value="" size="30" /><br/>
    <input type="submit" value="Process URL" />
    <input type="reset" value="Reset URL" />
  </form>
 </body>
</html>

403Error.jsp

<%-- 
 Document: 403Error.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-Error</title>
 </head>
 <body>
  <h1>Oops, Access Denied!Server declined to show this Webpage</h1>
  <p>Sorry, Requested webpage URL: 
	<strong>${webURL}</strong> is block by Administrator!
	Error code: HTTP Error 403 Forbidden</p>
 </body>
</html>

Servlet Program

/*
 * Save as a XMLServlet.java
 */
package r4r.Servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.StringTokenizer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *
 * @author R4R
 */
public class XMLServlet extends HttpServlet {

 private HashSet<String> blockSites = new HashSet<String>();

 @Override
 protected void doGet(HttpServletRequest request, 
	 HttpServletResponse response)
 throws ServletException, IOException {
  //doGet role play in index.jsp page
 }
 @Override
 protected void doPost(HttpServletRequest request,
	 HttpServletResponse response)throws ServletException,IOException
{
  response.setContentType("text/html;charset=UTF-8");
  PrintWriter out = response.getWriter();
  try {
//Get Parameter value from web.xml file
 String BannedSites = getServletConfig().getInitParameter("BannedSites");
 for (StringTokenizer st = new StringTokenizer(BannedSites);
	st.hasMoreTokens();)
	{
 String token = st.nextToken();
 blockSites.add(token); //store all value into HashSet
}

//get TextField value from index.jsp page
String webURL = request.getParameter("webURL");

//user won't allow to leave textFiled empty
if (!webURL.equals("")) {
 //is textFiled value match with HashSet value
 if (blockSites.contains(webURL)) {
//setAttribute which further call in error page
  request.setAttribute("webURL", webURL);
 //Sends an error response to the client(web.xml) for invoke error page
  response.sendError(HttpServletResponse.SC_FORBIDDEN, "error_page");
 } else {
//redirect response to the client to requested specified redirect location URL
  response.sendRedirect("http://" + webURL);
 }
} else {
 out.println("Please Don't leave TextField empty!<br/>");
 out.println("<a href=\"index.jsp\">Return to Index page</a>");
}
  } finally {
out.flush(); //free resource
out.close();
  }
 }

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