Context Object 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>Index Page</title>
</head>
<body>
<a href="studentView.do?id=10">Show Student Information</a>
</body>
</html>
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" />
<!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:x-large; 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.xml
<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">
<display-name>ApplicationControllerWeb</display-name>
<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>
package design;
public class StudentVO {
private String id;
private String name;
//constructor
public StudentVO(String id, String name){
this.id=id;
this.name=name;
}
//setters and getters
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package design;
public interface Command {
String execute(RequestContext requestContext);
}
package design;
public class StudentViewCommand implements Command {
@Override
public String execute(RequestContext requestContext) {
String id=null;
StudentVO studentVo=null;
id=requestContext.getParameter("id");
//call delegate and dao
studentVo=new StudentVO(id,"Ashok");
requestContext.setAttribute("student", studentVo);
return "showStudent";
}
}
package design;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
public class ContextFactory {
public RequestContext getContextObject(HttpServletRequest request){
Map<String, String[]> requestMap=null;
RequestContext requestContext=null;
HttpRequestMapper requestMapper=null;
requestMapper=new HttpRequestMapper();
requestMap=requestMapper.extract(request);
requestContext=new RequestContext(request.getRequestURI(),requestMap);
return requestContext;
}
public void bindContextObject(HttpServletRequest request,RequestContext requestContext){
HttpRequestMapper requestMapper=null;
requestMapper=new HttpRequestMapper();
requestMapper.bind(request, requestContext.getResponseMap());
}
}
package design;
import java.util.HashMap;
import java.util.Map;
public class RequestContext {
private String requestUri;
private Map<String, String[]> requestMap;
private Map<String, Object> responseMap;
public RequestContext(){
requestUri=null;
requestMap=new HashMap<String, String[]>();
responseMap=new HashMap<String, Object>();
}
public RequestContext(String requestUri, Map<String, String[]> requestMap) {
this.requestUri=requestUri;
this.requestMap=requestMap;
this.responseMap=new HashMap<String, Object>();
}
public String getParameter(String param) {
String[] val=null;
if(param != null){
val=requestMap.get(param);
}
return val[0];
}
public void setAttribute(String param, Object value) {
responseMap.put(param, value);
}
public String getRequestUri() {
return requestUri;
}
public Map<String, String[]> getRequestMap() {
return requestMap;
}
public Map<String, Object> getResponseMap() {
return responseMap;
}
}
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;
String view=null;
Dispatcher dispatcher=null;
RequestContext requestContext=null;
ContextFactory contextFactory=null;
ApplicationController applicationController=null;
// plubbing code (security, authorization)
// extracting data from protocol
contextFactory=new ContextFactory();
requestContext=contextFactory.getContextObject(request);
applicationController=new ApplicationController();
view=applicationController.process(requestContext);
// binding data back to protocol
contextFactory.bindContextObject(request, requestContext);
page=applicationController.mapViewToPage(view);
dispatcher=new Dispatcher();
dispatcher.dispatch(request, response, page);
}
}
package design;
public class ApplicationController {
public String process(RequestContext requestContext) {
String view=null;
Command command=null;
CommandHelper commandHelper=null;
commandHelper=new CommandHelper();
command=commandHelper.getCommand(requestContext.getRequestUri());
view=command.execute(requestContext);
return view;
}
public String mapViewToPage(String view) {
String page=null;
if(view !=null){
page="viewStudent.jsp";
}
return page;
}
}
package design;
public class CommandHelper {
public Command getCommand(String uri) {
Command command=null;
if(uri.contains("studentView.do")){
command= new StudentViewCommand();
}
return command;
}
}
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 rd=null;
rd=request.getRequestDispatcher(page);
try{
rd.forward(request, response);
}catch(ServletException e){
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}
package design;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
public class HttpRequestMapper {
public Map<String, String[]> extract(HttpServletRequest request) {
Map<String, String[]> requestMap=null;
requestMap=request.getParameterMap();
return requestMap;
}
public void bind(HttpServletRequest request, Map<String, Object> responseMap) {
Set<String> keys=null;
keys= responseMap.keySet();
for(String param : keys){
request.setAttribute(param, responseMap.get(param));
}
}
}
output :
http://localhost:8001/javaT/
http://localhost:8001/javaT/studentView.do?id=10
browser :
Student Id : 10
Student Name : Ashok
|