📄 controller.java
字号:
package com.forum.nokia.taskmanager.webui;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.forum.nokia.taskmanager.beans.Task;
import com.forum.nokia.taskmanager.database.DBAccess;
import com.forum.nokia.taskmanager.database.UserInfo;
/**
* This servlet acts as the controller in the system. The system implements the
* MVC (or Java Model 2) architecture, meaning that model (data storage), view
* (the look and feel of the system) and controller (decides the flow of
* operations) are separated from one another.<br>
* <br>
* This servlets responsibility is to direct the user to the correct page and to
* make queries to the database and validate the user input.
*/
public class Controller extends HttpServlet
{
/**
* Enum-type constants for the different actions offered by this servlet.
* They also work as indexes to the array of actions below.
*/
private static final int ERROR = -1;
private static final int ADD_NEW_TASK = 0;
private static final int CHANGE_PASSWORD = 1;
private static final int DELETE_TASK = 2;
private static final int LOGIN = 3;
private static final int LOGOUT = 4;
private static final int SAVE_TASK = 5;
private static final int SEND_SMS = 6;
private static final int ADD_NEW_USER = 7;
private static final int SAVE_USER = 8;
private static final int DELETE_USER = 9;
private static final int SET_TASK_DONE = 10;
private static final int GET_TASKS = 11;
private static final int SET_SERVER_INFO = 12;
private static final int GET_SERVER_INFO = 13;
private static final int EMPTY = 14;
// The separator constant.
private static final char SEPARATOR = '#';
/**
* The strings recognized as valid actions by the servlet. Note the
* correspondance between the strings and the indexes defined above.
*/
private static final String[] COMMANDS = { "AddNewTaskAction",
"ChangePasswordAction", "DeleteTaskAction", "LoginAction",
"LogoutAction", "SaveTaskAction", "SendSMSAction",
"AddNewUserAction", "SaveUserAction", "DeleteUserAction",
"SetTaskDoneAction", "GetTasksAction", "SetServerInfoAction",
"GetServerInfoAction","" };
/**
* Default serial version UID.
*/
private static final long serialVersionUID = 1L;
/**
* The database access object (DBAO). Offers services to the Controller
* servlet.
*/
private DBAccess database = null;
private boolean databaseNotInUse = false;
/**
* This method is executed when the Controller object is instantiated. It
* makes sure a DBAO can be found or that at least an error flag is raised.
*/
public void init()
{
database = (DBAccess) getServletContext().getAttribute( "database" );
if( database == null )
{ // This _should_ be true every time but we try to retrieve a
// database object from the context anyway.
try
{
database = new DBAccess();
getServletContext().setAttribute( "database", database );
}
catch( Exception e )
{ // Database connection couldn't be opened so the database is not
// in use.
System.out.println( "database not in use" );
databaseNotInUse = true;
}
}
}
/**
* This method is called when the Controller object is about to be
* destroyed. It ensures the DBAO's connection is terminated after it is no
* longer needed.
*/
public void destroy()
{
super.destroy();
if( database != null )
{ // the database connection is terminated so there's no unnecessary
// idle connections to the mysql server.
database.terminateConnection();
}
getServletContext().removeAttribute( "database" );
}
/**
* This function finds the corresponfing "enum" integer to the given action
*
* @param action
* @return
*/
private int identifyAction( String action )
{
for( int i = 0; i < COMMANDS.length; ++i )
{
if( COMMANDS[i].equals( action ) )
{
return i;
}
}
return ERROR;
}
/**
* This function is called when a GET method is received from the client. It
* merely forwards the call to doPost.
*/
protected void doGet( HttpServletRequest request,
HttpServletResponse response ) throws ServletException, IOException
{
doPost( request, response );
}
/**
* This function is called when a GET method is received from the client. It
* identifies the action request from the client and calls the function that
* realizes the action. It then dispatches the request to the correct JSP
* page.
*/
protected void doPost( HttpServletRequest request,
HttpServletResponse response ) throws ServletException, IOException
{
String nextPage = "";
int action = identifyAction( request.getParameter( "action" ) );
// We need to test if the database connection is alive.
if( !database.testConnection() )
{
try
{ // We try to reconnect to the database.
database.connect();
}
catch( SQLException e )
{ // the database couldn't be reconnected to.
request.setAttribute( "loginError",
"Error: Database connection forming failed." );
request.setAttribute( "cameFromController", "true" );
// The client is shown the correct page.
RequestDispatcher rd = getServletContext().getRequestDispatcher( "index.jsp" );
rd.forward( request, response );
}
}
if( request.getSession( false ) == null && action != LOGIN
&& action != SET_TASK_DONE && action != GET_TASKS )
{
action = ERROR;
}
switch( action )
{
case ERROR:
{
nextPage = doErrorAction( request, response );
break;
}
case ADD_NEW_TASK:
{
nextPage = doAddNewTaskAction( request, response );
break;
}
case CHANGE_PASSWORD:
{
nextPage = doChangePasswordAction( request, response );
break;
}
case DELETE_TASK:
{
nextPage = doDeleteTaskAction( request, response );
break;
}
case LOGIN:
{
nextPage = doLoginAction( request, response );
break;
}
case LOGOUT:
{
nextPage = doLogoutAction( request, response );
break;
}
case SAVE_TASK:
{
nextPage = doSaveTaskAction( request, response );
break;
}
case SEND_SMS:
{
nextPage = doSendSMSAction( request, response );
break;
}
case ADD_NEW_USER:
{
nextPage = doAddNewUserAction( request, response );
break;
}
case SAVE_USER:
{
nextPage = doSaveUserAction( request, response );
break;
}
case DELETE_USER:
{
nextPage = doDeleteUserAction( request, response );
break;
}
case SET_TASK_DONE:
{
doSetTaskDoneAction( request, response );
return;
}
case GET_TASKS:
{
doGetTasksAction( request, response );
return;
}
case SET_SERVER_INFO:
{
nextPage = doSetServerinfoAction( request, response );
break;
}
case GET_SERVER_INFO:
{
nextPage = "/main.jsp";
break;
}
case EMPTY:
{
nextPage = "/index.jsp";
break;
}
default:
{
nextPage = doErrorAction( request, response );
break;
}
}
request.setAttribute( "cameFromController", "true" );
// The client is shown the correct page.
System.out.println("nextPage: " + nextPage);
RequestDispatcher rd = getServletContext().getRequestDispatcher(nextPage );
rd.forward( request, response );
}
/**
* Implements the GetTasks action. Retrieves the users tasks from the
* database and changes them to a string.
*
* @param request
* The request object from the servlet.
* @param response
* The response object from the servlet.
*/
private void doGetTasksAction( HttpServletRequest request,
HttpServletResponse response )
{
PrintWriter out = null;
try
{
out = response.getWriter();
}
catch( IOException e )
{
}
// we do a check on the clients login info.
String username = request.getParameter( "username" );
String password = request.getParameter( "password" );
if( username == null || username.equals( "" ) || password == null
|| password.equals( "" ) )
{
out.print( SEPARATOR + "Error: username and/or password not provided." );
return;
}
UserInfo uInfo;
try
{
// the username and password are validated from the database.
uInfo = validateUser( username, password );
if( !uInfo.userValid )
{
out.print( SEPARATOR + "Error: invalid username and/or password." );
return;
}
}
catch( SQLException e )
{
out.print( SEPARATOR + "Error: server-side database error." );
return;
}
// the users tasks are retrieved.
List tasks = database.getTasks( uInfo.userId );
// The list starts with a separator sign
out.print( SEPARATOR );
if( tasks == null || tasks.size() == 0 )
{ // Empty tasklist
return;
}
Iterator iter = tasks.iterator();
String taskList = "";
while( iter.hasNext() )
{ // the tasklist is formatted into a string.
Task task = (Task) iter.next();
if( task.getStatus().equals( "NOT_DONE" ) )
{
taskList += task.getTaskId() + SEPARATOR + task.getDescription()
+ SEPARATOR;
}
}
// finally, we update the userinfo so that the users tasks are marked as
// up to date.
if( !database.markTasksUpToDate( uInfo.userId ) )
{
out.print( SEPARATOR + "Error: server-side database error." );
return;
}
out.print( taskList );
}
/**
* Implements the SetTaskDone action. Marks a specific task as done in the
* database.
*
* @param request
* The request object from the servlet.
* @param response
* The response object from the servlet.
*/
private void doSetTaskDoneAction( HttpServletRequest request,
HttpServletResponse response )
{
PrintWriter out = null;
try
{
out = response.getWriter();
}
catch( IOException e )
{
}
// we do a check on the clients login info.
String taskId = request.getParameter( "task_id" );
String username = request.getParameter( "username" );
String password = request.getParameter( "password" );
if( username == null || username.equals( "" ) || password == null
|| password.equals( "" ) )
{
out.print( SEPARATOR + "Error: username and/or password not provided." );
return;
}
if( taskId == null || taskId.equals( "" ) )
{
out.print( SEPARATOR + "Error: task id not provided." );
return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -