PBDispatcherServletpublic class PBDispatcherServlet extends HttpServlet This class acts as a controller for the Project Billboard
example, dispatching to separate Action objects for request
processing. |
Fields Summary |
---|
private Hashtable | actions |
Methods Summary |
---|
public void | destroy()Removes the EmployeeRegistryBean and NewsBean
servlet context attributes.
getServletContext().removeAttribute("empReg");
getServletContext().removeAttribute("news");
| public void | doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)Performs the same processing as for a POST request.
doPost(request, response);
| public void | doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)Locates the Action object corresponding to the requested
action, or the login Action in case the user is not yet
authenticated, and dispatch the processing to the selected
Action object.
String actionName = request.getParameter("action");
if (actionName == null) {
response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE);
return;
}
Action action = (Action) actions.get(actionName);
if (action == null) {
response.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
return;
}
// Use the login action if the user is not authenticated
if (!isAuthenticated(request) &&
!("authenticate".equals(actionName) ||
"logout".equals(actionName))) {
action = (Action) actions.get("login");
}
action.perform(this, request, response);
| public void | init()Creates the application scope objects used by the Actions
and JSP pages in this application.
Note! In this example, a DataSource object is created using
hardcoded information. In a real application, it should be
retrieved through JNDI or created based on configurable
information if JNDI is not used. If the application contains
more than one servlet, it may also be better to do all
initialization in a servlet dedicated to setting up the
application environment instead.
DataSource ds = null;
try {
ds = new DataSourceWrapper("sun.jdbc.odbc.JdbcOdbcDriver",
"jdbc:odbc:example", null, null);
}
catch (Exception e) {} // Ignore all in this example
EmployeeRegistryBean empReg = new EmployeeRegistryBean();
empReg.setDataSource(ds);
getServletContext().setAttribute("empReg", empReg);
NewsBean news = new NewsBean();
getServletContext().setAttribute("news", news);
initActions();
| private void | initActions()Initializes the set of Action objects used by the
application. Instead of hardcoding this list, it can
be based on configuration information, such as
servlet initialization parameters.
actions = new Hashtable();
actions.put("authenticate", new AuthenticateAction());
actions.put("logout", new LogoutAction());
actions.put("storeMsg", new StoreMsgAction());
actions.put("updateProfile", new UpdateProfileAction());
actions.put("showPage", new ShowPageAction());
actions.put("login", new LoginAction());
| private boolean | isAuthenticated(javax.servlet.http.HttpServletRequest request)Returns true if the session contains the authentication token.
boolean isAuthenticated = false;
HttpSession session = request.getSession();
if (session.getAttribute("validUser") != null) {
isAuthenticated = true;
}
return isAuthenticated;
|
|