Hola,
Les cuento. La aplicación tiene un controlador (servlet) llamado Dispatcher. Recibe las peticiones y despues de procesarlas las devuelve a una palntilla (template.jsp). Esta plantilla es genérica de manera que sea cual sea la petición, por medio de Tags Libraries y un archivo y JavaBeans asociados a la petión esta es traducida en el correspondiente HTML. El código es como sigue:
Dispatcher.-
package com.sun.ebank.web;
import javax.servlet.http.*;
import com.sun.ebank.util.Debug;
import java.util.*;
import java.math.BigDecimal;
public class Dispatcher extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
ResourceBundle messages =
(ResourceBundle) session.getAttribute("messages");
if (messages == null) {
Locale locale = request.getLocale();
Debug.print(locale.getDisplayName());
messages = ResourceBundle.getBundle("com.sun.ebank.web.WebMes sages", locale);
if (messages == null) {
Debug.print("Didn't locate resource bundle.");
}
session.setAttribute("messages", messages);
}
BeanManager beanManager =
(BeanManager) session.getAttribute("beanManager");
if (beanManager == null) {
Debug.print("Creating bean manager.");
beanManager = new BeanManager();
session.setAttribute("beanManager", beanManager);
}
String selectedScreen = request.getServletPath();
Debug.print(selectedScreen);
CustomerBean customerBean = new CustomerBean();
customerBean.setBeanManager(beanManager);
request.setAttribute("customerBean", customerBean);
if (selectedScreen.equals("/accountHist")) {
AccountHistoryBean accountHistoryBean = new AccountHistoryBean();
request.setAttribute("accountHistoryBean", accountHistoryBean);
try {
accountHistoryBean.setBeanManager(beanManager);
accountHistoryBean.setCustomerBean(customerBean);
accountHistoryBean.setAccountId(request.getParamet er(
"accountId"));
accountHistoryBean.setSortOption(Integer.parseInt(
request.getParameter("sortOption")));
accountHistoryBean.setActivityView(Integer.parseIn t(
request.getParameter("activityView")));
accountHistoryBean.setYear(Integer.parseInt(
request.getParameter("year")));
accountHistoryBean.setDate(Integer.parseInt(
request.getParameter("date")));
accountHistoryBean.setSinceDay(Integer.parseInt(
request.getParameter("sinceDay")));
accountHistoryBean.setSinceMonth(Integer.parseInt(
request.getParameter("sinceMonth")));
accountHistoryBean.setBeginDay(Integer.parseInt(
request.getParameter("beginDay")));
accountHistoryBean.setBeginMonth(Integer.parseInt(
request.getParameter("beginMonth")));
accountHistoryBean.setEndDay(Integer.parseInt(
request.getParameter("endDay")));
accountHistoryBean.setEndMonth(Integer.parseInt(
request.getParameter("endMonth")));
} catch (NumberFormatException e) {
// Not possible
}
accountHistoryBean.doTx();
} else if (selectedScreen.equals("/transferAck")) {
String fromAccountId = request.getParameter("fromAccountId");
String toAccountId = request.getParameter("toAccountId");
if ((fromAccountId == null) || (toAccountId == null)) {
request.setAttribute("errorMessage",
messages.getString("AccountError"));
try {
request.getRequestDispatcher("/error.jsp")
.forward(request, response);
} catch (Exception ex) {
}
} else {
TransferBean transferBean = new TransferBean();
request.setAttribute("transferBean", transferBean);
try {
transferBean.setMessages(messages);
transferBean.setBeanManager(beanManager);
transferBean.setFromAccountId(fromAccountId);
transferBean.setToAccountId(toAccountId);
transferBean.setTransferAmount((new BigDecimal(
request.getParameter("transferAmount"))).setScale( 2));
String errorMessage = transferBean.doTx();
if (errorMessage != null) {
request.setAttribute("errorMessage", errorMessage);
try {
request.getRequestDispatcher("/error.jsp")
.forward(request, response);
} catch (Exception ex) {
}
}
} catch (NumberFormatException e) {
request.setAttribute("errorMessage",
messages.getString("AmountError"));
try {
request.getRequestDispatcher("/error.jsp")
.forward(request, response);
} catch (Exception ex) {
}
}
}
} else if (selectedScreen.equals("/atmAck")) {
ATMBean atmBean = new ATMBean();
request.setAttribute("atmBean", atmBean);
try {
customerBean.setBeanManager(beanManager);
atmBean.setMessages(messages);
atmBean.setBeanManager(beanManager);
atmBean.setCustomerBean(customerBean);
atmBean.setAccountId(request.getParameter("account Id"));
atmBean.setAmount((new BigDecimal(request.getParameter("amount"))).setSca le(
2));
atmBean.setOperation(Integer.parseInt(request.getP arameter(
"operation")));
String errorMessage = atmBean.doTx();
if (errorMessage != null) {
request.setAttribute("errorMessage", errorMessage);
try {
request.getRequestDispatcher("/error.jsp")
.forward(request, response);
} catch (Exception ex) {
}
}
} catch (NumberFormatException e) {
request.setAttribute("errorMessage",
messages.getString("AmountError"));
try {
request.getRequestDispatcher("/error.jsp")
.forward(request, response);
} catch (Exception ex) {
}
}
}
try {
Debug.print("Forwarding to template.");
request.getRequestDispatcher("/template/template.jsp")
.forward(request, response);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) {
String selectedScreen = request.getServletPath();
Debug.print(selectedScreen);
request.setAttribute("selectedScreen", selectedScreen);
try {
request.getRequestDispatcher("/template/template.jsp")
.forward(request, response);
} catch (Exception ex) {
}
}
}
template.jsp .-
<%@ taglib uri="/WEB-INF/tlds/tutorial-template.tld" prefix="tt" %>
<%@ page errorPage="/template/errorpage.jsp" %>
<%@ include file="/template/screendefinitions.jspf" %>
<html>
<head>
<title>
<tt:insert definition="bank" parameter="title"/>
</title>
</head>
<body bgcolor="#ffffff">
<tt:insert definition="bank" parameter="banner"/>
<tt:insert definition="bank" parameter="links"/>
<tt:insert definition="bank" parameter="body"/>
</body>
</html>
aquí le paso las taglib en la primera línea y "/template/screendefinitions.jspf" en la tercera línea sería (lo paso en el siguiente post y les cuento)