📄 controller.java
字号:
}
UserInfo uInfo;
try
{
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;
}
if( !database.markTaskDone( taskId ) )
{
out.print( SEPARATOR + "Error: Task marking failed." );
}
else
{
out.print( SEPARATOR + "OK" );
}
}
/**
* Implements the DeleteUser action. Tries to remove the user from the
* database.
*
* @param request
* The request object from the servlet.
* @param response
* The response object from the servlet.
* @return The page to be shown next.
*/
private String doDeleteUserAction( HttpServletRequest request,
HttpServletResponse response )
{
String userId = (String) request.getParameter( "user_id" );
request.getSession( false ).setAttribute( "sqlError", null );
if( !database.deleteUser( userId ) )
{
request.getSession( false ).setAttribute( "sqlError",
"An SQL error occurred while modifying a user." );
}
return "/main.jsp";
}
/**
* Implements the SaveUser action. Saves modified user data into the
* database.
*
* @param request
* The request object from the servlet.
* @param response
* The response object from the servlet.
* @return The page to be shown next.
*/
private String doSaveUserAction( HttpServletRequest request,
HttpServletResponse response )
{
String login = (String) request.getParameter( "user" );
String pw = (String) request.getParameter( "pw" );
String mobile = (String) request.getParameter( "phone" );
String groupId = (String) request.getParameter( "group" );
String userId = (String) request.getParameter( "user_id" );
request.getSession( false ).setAttribute( "sqlError", null );
if( !database.changeUser( login, pw, mobile, groupId, userId ) )
{
request.getSession( false ).setAttribute( "sqlError",
"An SQL error occurred while modifying a user." );
}
return "/main.jsp";
}
/**
* Implements the AddNewUser action. Adds a new row to the user table in the
* database.
*
* @param request
* The request object from the servlet.
* @param response
* The response object from the servlet.
* @return The page to be shown next.
*/
private String doAddNewUserAction( HttpServletRequest request,
HttpServletResponse response )
{
String login = (String) request.getParameter( "user" );
String pw = (String) request.getParameter( "pw" );
String mobile = (String) request.getParameter( "phone" );
String group = (String) request.getParameter( "group" );
request.getSession( false ).setAttribute( "sqlError", null );
if( !database.addNewUser( login, pw, mobile, group ) )
{
request.getSession( false ).setAttribute( "sqlError",
"An SQL error occurred while adding a user." );
}
return "/main.jsp";
}
private String doSetServerinfoAction( HttpServletRequest request,
HttpServletResponse response )
{
String address = (String) request.getParameter( "address" );
String port = (String) request.getParameter( "port" );
request.getSession( false ).setAttribute( "sqlError", null );
if( !database.setServerInfo( address, port ) )
{
request.getSession( false ).setAttribute( "sqlError",
"An SQL error occurred while adding a server info." );
}
return "/main.jsp";
}
/**
* Implements the Error action. Sets the request attribute "loginError" so
* that an error message can be seen on the login screen.
*
* @param request
* The request object from the servlet.
* @param response
* The response object from the servlet.
* @return The page to be shown next.
*/
private String doErrorAction( HttpServletRequest request,
HttpServletResponse response )
{
request.setAttribute( "loginError", "Error: Not logged in" );
return "/index.jsp";
}
/**
* Implements the AddNewTask action. Adds a new row to the task table.
*
* @param request
* The request object from the servlet.
* @param response
* The response object from the servlet.
* @return The page to be shown next.
*/
private String doAddNewTaskAction( HttpServletRequest request,
HttpServletResponse response )
{
String oldDescription = (String) request.getParameter( "description" );
String owner_id = (String) request.getParameter( "owner" );
String state = (String) request.getParameter( "state" );
String description = null;
request.getSession( false ).setAttribute( "sqlError", null );
if( ( description = oldDescription.replace( '#', ' ' ) ) != oldDescription )
{
request.getSession( false ).setAttribute( "sqlError",
"#-characters are illegal and were removed." );
}
if( !database.addNewTask( owner_id, description, state ) )
{
request.getSession( false ).setAttribute( "sqlError",
"An SQL error occurred while adding a task." );
}
return "/main.jsp";
}
/**
* Implements the ChangePassword action. Changes the users password in the
* database.
*
* @param request
* The request object from the servlet.
* @param response
* The response object from the servlet.
* @return The page to be shown next.
*/
private String doChangePasswordAction( HttpServletRequest request,
HttpServletResponse response )
{
String currentPw = (String) request.getParameter( "currentpw" );
String newPw = (String) request.getParameter( "pw" );
String userId = (String) request.getSession( false ).getAttribute(
"userId" );
request.getSession( false ).setAttribute( "sqlError", null );
if( database.checkCurrentPassword( currentPw, userId ) )
{
if( !database.changePassword( newPw, userId ) )
{
request.getSession( false ).setAttribute( "sqlError",
"An SQL error occurred while changing password." );
}
else
{
request.getSession( false ).setAttribute( "sqlError",
"Password changed succesfully." );
}
}
else
{
request.getSession( false ).setAttribute( "sqlError",
"Current password was incorrect." );
}
return "/main.jsp";
}
/**
* Implements the DeleteTask action. Removes the specified task from the
* task table.
*
* @param request
* The request object from the servlet.
* @param response
* The response object from the servlet.
* @return The page to be shown next.
*/
private String doDeleteTaskAction( HttpServletRequest request,
HttpServletResponse response )
{
String taskId = (String) request.getParameter( "taskId" );
String ownerId = (String) request.getParameter( "owner" );
request.getSession( false ).setAttribute( "sqlError", null );
if( !database.deleteTask( taskId, ownerId ) )
{
request.getSession( false ).setAttribute( "sqlError",
"An SQL error occurred while deleting a task." );
}
return "/main.jsp";
}
/**
* This function implements the login actions. It makes sure the inserted
* username and password are of valid format and can be found from the
* database. It also sets up the necessary session attributes.
*
* @param request
* Request received from the client.
* @param response
* Response to the client.
* @return A string depicting the name of the page to be shown next.
* @throws ServletException
* @throws IOException
*/
private String doLoginAction( HttpServletRequest request,
HttpServletResponse response )
{
// Form data is extracted.
String username = request.getParameter( "username" );
String password = request.getParameter( "password" );
System.out.println("username: " +username +" password: " +password);
UserInfo uInfo;
// The field must not be empty.
if( username == null || password == null || username == ""
|| password == "" )
{
request.setAttribute( "loginError",
"Error: username and/or password not provided." );
return "/index.jsp";
}
// If true, the servlet couldn't connect to the database when
// initialized
else if( databaseNotInUse )
{
request.setAttribute( "loginError",
"Database connection couldn't be formed" );
return "/index.jsp";
}
try
{
uInfo = validateUser( username, password );
}
catch( SQLException e )
{ // Something went wrong when communicating with the database.
request.setAttribute( "loginError",
"An error occured while communicating with the database." );
return "/index.jsp";
}
if( !uInfo.userValid )
{ // The received username and password couldn't be found from the
// database.
request.setAttribute( "loginError", "Invalid username or password" );
return "/index.jsp";
}
// A new session is created.
HttpSession userSession = request.getSession( true );
userSession.setAttribute( "userId", uInfo.userId );
userSession.setAttribute( "userRole", uInfo.userRole );
userSession.setAttribute( "username", username );
// The client is redirected to the main page after a succesful login.
return "/main.jsp";
}
/**
* Implements the Logout action. Invalidates the session and returns the
* client to the login screen.
*
* @param request
* The request object from the servlet.
* @param response
* The response object from the servlet.
* @return The page to be shown next.
*/
private String doLogoutAction( HttpServletRequest request,
HttpServletResponse response )
{
request.getSession( false ).invalidate();
return "/index.jsp";
}
/**
* Implements the SaveTask action.
*
* @param request
* The request object from the servlet.
* @param response
* The response object from the servlet.
* @return The page to be shown next.
*/
private String doSaveTaskAction( HttpServletRequest request,
HttpServletResponse response )
{
String oldDescription = (String) request.getParameter( "description" );
String taskId = (String) request.getParameter( "taskId" );
String ownerId = (String) request.getParameter( "owner" );
String state = (String) request.getParameter( "state" );
String description = null;
request.getSession( false ).setAttribute( "sqlError", null );
if( ( description = oldDescription.replace( '#', ' ' ) ) != oldDescription )
{
request.getSession( false ).setAttribute( "sqlError",
"#-characters are illegal and were removed." );
}
if( !database.changeTask( ownerId, description, state, taskId ) )
{
request.getSession( false ).setAttribute( "sqlError",
"An SQL error occurred while modifying a task." );
}
return "/main.jsp";
}
/**
* Implements the SendSMS action.
*
* @param request
* The request object from the servlet.
* @param response
* The response object from the servlet.
* @return The page to be shown next.
*/
private String doSendSMSAction( HttpServletRequest request,
HttpServletResponse response )
{
request.setAttribute( "smsSent", "true" );
return "/main.jsp";
}
/**
* This function validates the username and password by accessing the
* database.
*
* @param username
* The username to be checked.
* @param password
* The password to be checked.
* @return A boolean value depending on wether the username and password
* were accepted.
* @throws SQLException
* Thrown if an error occurred while communicating with the
* database.
*/
private UserInfo validateUser( String username, String password )
throws SQLException
{
if( username.length() > 20 || password.length() > 20 )
{
return new UserInfo( false );
}
// The user login is authenticated.
UserInfo uInfo = database.validateUser( username, password );
return uInfo;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -