Front Controller Design Pattern
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home Page</title>
</head>
<body>
<a href="StudentView.do">Show Student</a>
</body>
</html>
package design;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface Command {
String execute(HttpServletRequest request,HttpServletResponse response);
}
package design;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class StudentViewCommand implements Command {
@Override
public String execute(HttpServletRequest request, HttpServletResponse response) {
String page=null;
StudentVO studentVo=null;
//query data using delegate and dao populate in Value Object
studentVo=new StudentVO(1,"Ashok");
request.setAttribute("student", studentVo);
page="showStudent";
return page;
}
}
package design;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FrontController extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
String page=null;
Command command=null;
String requestUri=null;
CommandHelper helper=null;
Dispatcher dispatcher=null;
requestUri=request.getRequestURI();
helper=new CommandHelper();
System.out.println("Request URI : "+requestUri);
command=helper.getCommand(requestUri);
page=command.execute(request, response);
dispatcher=new Dispatcher();
dispatcher.dispatch(request, response, page);
}
}
package design;
public class CommandHelper {
public Command getCommand(String uri) {
if(uri.contains("StudentView.do")){
return new StudentViewCommand();
}
return null;
}
}
package design;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Dispatcher {
public void dispatch(HttpServletRequest request, HttpServletResponse response, String page) {
RequestDispatcher dispatcher=null;
if(page!=null){
dispatcher=request.getRequestDispatcher(mapPageToView(page));
try{
dispatcher.forward(request, response);
}catch(ServletException e){
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}//if
}
private String mapPageToView(String page) {
if(page.equals("showStudent")){
return "viewStudent.jsp";
}
return null;
}
}
package design;
public class StudentVO {
private int id;
private String name;
public StudentVO(int id, String name){
this.id=id;
this.name=name;
}
//setters and getters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
viewStudent.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<jsp:useBean id="student" type="design.StudentVO" scope="request" beanName="studentVO"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Student Information</title>
</head>
<body>
<p style="color: blue;font-size: large;font-family: sans-serif;">
Student Id : <jsp:getProperty property="id" name="student"/><br>
Student Name : <jsp:getProperty property="name" name="student"/>
</p>
</body>
</html>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>front</servlet-name>
<servlet-class>design.FrontController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>front</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
result :
http://localhost:8001/javaT/
http://localhost:8001/javaT/StudentView.do
browser :
Student Id : 1
Student Name : Ashok
console :
Request URI : /javaT/StudentView.do
|