com.ora.jsp.servlets
Class PBControllerServlet

java.lang.Object
  |
  +--javax.servlet.GenericServlet
        |
        +--javax.servlet.http.HttpServlet
              |
              +--com.ora.jsp.servlets.PBControllerServlet
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public class PBControllerServlet
extends javax.servlet.http.HttpServlet

This class is an example of a servlet used as the Controller for the Project Billboard application.

It initializes a number of servlet context attributes in its init() method. The doGet()/doPost() methods perform authentication and access control, and perform the action specified by the action request parameter is the client is authorized.

Version:
1.0
Author:
Hans Bergsten, Gefion software
See Also:
Serialized Form

Fields inherited from class javax.servlet.http.HttpServlet
HEADER_IFMODSINCE, HEADER_LASTMOD, LSTRING_FILE, lStrings, METHOD_DELETE, METHOD_GET, METHOD_HEAD, METHOD_OPTIONS, METHOD_POST, METHOD_PUT, METHOD_TRACE
 
Fields inherited from class javax.servlet.GenericServlet
config
 
Constructor Summary
PBControllerServlet()
           
 
Method Summary
 void destroy()
          Removes the EmployeeRegistryBean and NewsBean servlet context attributes.
private  void doAuthenticate(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Autheticates a user with help from the EmployeeRegistryBean, using the "userName" and "password" request parameters.
private  void doForwardToLogin(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Forwards the request to the login JSP page, with an "errorMsg" parameter containing text to be displayed on the login page.
 void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Processes a GET request by calling doPost().
private  void doLogout(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Invalidates the session, thereby removing the authentication token, and redirects to the login page.
 void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Performs authentication, if needed, and access control.
private  void doShowPage(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Forwards to the specified JSP page.
private  void doStoreMsg(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Creates a new NewsItemBean and sets its properties based on the "category" and "msg" request parameters, plus the firstName and lastName properties of the authenticated user (an EmployeeBean accessible as the "validUser" session attribute).
private  void doUpdateProfile(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Updates the projects property of an authenticated user, represented by the "validUser" session attribute, using the EmployeeRegistryBean.
private  void forward(java.lang.String url, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Forwards the request to the specified relative URL.
private  java.lang.String getShowPageURL(javax.servlet.http.HttpServletRequest request)
          Returns an absolute URL, suitable for redirecting a request back to this servlet, with an "action" parameter set to "showPage" and a "page" parameter set to the specified (relative) page URL.
 void init()
          Creates an EmployeeRegistryBean and a NewsBean, and saves them as servlet context attributes.
private  boolean isAuthenticated(javax.servlet.http.HttpServletRequest request)
          Returns true if an authentication token object is found in the session.
 
Methods inherited from class javax.servlet.http.HttpServlet
, doDelete, doHead, doOptions, doPut, doTrace, getAllDeclaredMethods, getLastModified, maybeSetLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Constructor Detail

PBControllerServlet

public PBControllerServlet()
Method Detail

init

public void init()
          throws javax.servlet.ServletException
Creates an EmployeeRegistryBean and a NewsBean, and saves them as servlet context attributes.
Overrides:
init in class javax.servlet.GenericServlet

destroy

public void destroy()
Removes the EmployeeRegistryBean and NewsBean servlet context attributes.
Overrides:
destroy in class javax.servlet.GenericServlet

doGet

public void doGet(javax.servlet.http.HttpServletRequest request,
                  javax.servlet.http.HttpServletResponse response)
           throws java.io.IOException,
                  javax.servlet.ServletException
Processes a GET request by calling doPost().
Overrides:
doGet in class javax.servlet.http.HttpServlet

doPost

public void doPost(javax.servlet.http.HttpServletRequest request,
                   javax.servlet.http.HttpServletResponse response)
            throws java.io.IOException,
                   javax.servlet.ServletException
Performs authentication, if needed, and access control. If the user is authorized, performs the action specified by the "action" request parameter.
Overrides:
doPost in class javax.servlet.http.HttpServlet

isAuthenticated

private boolean isAuthenticated(javax.servlet.http.HttpServletRequest request)
Returns true if an authentication token object is found in the session.

doForwardToLogin

private void doForwardToLogin(javax.servlet.http.HttpServletRequest request,
                              javax.servlet.http.HttpServletResponse response)
                       throws java.io.IOException,
                              javax.servlet.ServletException
Forwards the request to the login JSP page, with an "errorMsg" parameter containing text to be displayed on the login page.

doAuthenticate

private void doAuthenticate(javax.servlet.http.HttpServletRequest request,
                            javax.servlet.http.HttpServletResponse response)
                     throws java.io.IOException,
                            javax.servlet.ServletException
Autheticates a user with help from the EmployeeRegistryBean, using the "userName" and "password" request parameters. If the user can be authenticated, the "validUser" session attribute is set to an instance of the EmployeeBean, to serve as an authentication token in this application.

Cookies with the user name and password are set or reset as specified by the "remember" request parameter.


doLogout

private void doLogout(javax.servlet.http.HttpServletRequest request,
                      javax.servlet.http.HttpServletResponse response)
               throws java.io.IOException
Invalidates the session, thereby removing the authentication token, and redirects to the login page.

doStoreMsg

private void doStoreMsg(javax.servlet.http.HttpServletRequest request,
                        javax.servlet.http.HttpServletResponse response)
                 throws java.io.IOException,
                        javax.servlet.ServletException
Creates a new NewsItemBean and sets its properties based on the "category" and "msg" request parameters, plus the firstName and lastName properties of the authenticated user (an EmployeeBean accessible as the "validUser" session attribute). The NewItemBean is then added to the NewsBean. This action is only performed for POST request. Before returning, the client is redirected to the main page, where the new message is displayed.

doUpdateProfile

private void doUpdateProfile(javax.servlet.http.HttpServletRequest request,
                             javax.servlet.http.HttpServletResponse response)
                      throws java.io.IOException,
                             javax.servlet.ServletException
Updates the projects property of an authenticated user, represented by the "validUser" session attribute, using the EmployeeRegistryBean. This action is only performed for POST requests. Before returning, the client is redirected to the main page, where the new set of projects are displayed.

doShowPage

private void doShowPage(javax.servlet.http.HttpServletRequest request,
                        javax.servlet.http.HttpServletResponse response)
                 throws java.io.IOException,
                        javax.servlet.ServletException
Forwards to the specified JSP page. The reason for using this action instead of requesting a JSP page directly is to let the PBDispatcher handle authentication and access control even for JSP pages. Note that nothing prevents a user from requesting a JSP page directly, so this is not a secure way to perform access control. What it does give is automatic redirection to the login page for users that are not properly logged in. In this application, as in most applications with a servlet as a controller, accessing a JSP page directly doesn't reveal any restricted information however; the JSP page only displays information available in beans, created by the controller.

getShowPageURL

private java.lang.String getShowPageURL(javax.servlet.http.HttpServletRequest request)
Returns an absolute URL, suitable for redirecting a request back to this servlet, with an "action" parameter set to "showPage" and a "page" parameter set to the specified (relative) page URL.

forward

private void forward(java.lang.String url,
                     javax.servlet.http.HttpServletRequest request,
                     javax.servlet.http.HttpServletResponse response)
              throws java.io.IOException,
                     javax.servlet.ServletException
Forwards the request to the specified relative URL.