📄 lecturerservlet.java.svn-base
字号:
package com.sixtwenty.servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.ArrayList;
import java.util.Calendar;
import com.sixtwenty.*;
import com.sixtwenty.Submit;
import com.sixtwenty.SubmitServiceLocator;
/**
* This servlet is accessed when the lecture login to the system
* @author grp-e
*
*/
public class LecturerServlet extends HttpServlet
{
/**
* Handles the get request
* @param request
* @param response
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
Submit submit = null;
try {
//Instantiate the SubmitServiceLocator
SubmitServiceLocator ssl = new SubmitServiceLocator();
submit = ssl.getsubmit();
} catch(Exception e) {
throw new ServletException(e.toString());
}
// Get the type of action to do
String action = new String(request.getParameter("action"));
// To get the staff identifier
String staffId = (String)request.getSession().getAttribute("username"); //The staffId
// If we don't know the staff's identifier => access error
if (staffId == null || staffId.equals("")) {
//Forwarding to next jsp
RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/jsp/errorpage.jsp");
requestDispatcher.forward(request, response);
return;
}
//If the action is listproject
if (action.equalsIgnoreCase("listproject"))
{
Projects[] listOfProject = null; //The list of Projects[]
//call to remote method getProjectStaff to get the list of project
listOfProject = submit.getProjectStaff(staffId);
//set the list of projects in session
request.getSession().setAttribute("listOfProject", listOfProject);
//Forwarding to next jsp
RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/jsp/listprojectstaff.jsp");
requestDispatcher.forward(request, response);
}
//if action is submissions
if (action.equalsIgnoreCase("submissions"))
{
String projId = new String(request.getParameter("projId")); //The project id
Submissions[] listOfSubmissions = null;
//call to remote method getProjectSubmissions to get the list of submissions
listOfSubmissions = submit.getProjectSubmissions(projId);
//set the list of submissions in session
request.getSession().setAttribute("listOfSubmission", listOfSubmissions);
RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/jsp/submissionsStaff.jsp");
requestDispatcher.forward(request, response);
}
//if action is download
if (action.equalsIgnoreCase("download"))
{
String subId = new String(request.getParameter("subId")); //The subject Id
String filename = new String(request.getParameter("filename")); // The filename
//call to remote method getSubmission
Submissions subm = submit.getSubmission(subId);
response.setContentType("applicatioin/bin");
// Set the file name
response.setHeader("Content-disposition", "attachment; filename=" + filename);
// Get the output stream handler to stream bytes to the browser
ServletOutputStream os = response.getOutputStream();
byte[] fileContents; // The file contents to stream
int i = 0;
boolean find = subm.getSub_files()[0].getFilename().equals(filename);
while (i < subm.getSub_files().length && !find) {
i++;
find = subm.getSub_files()[i].getFilename().equals(filename);
}
if (!find)
throw new ServletException("fn: " + filename + " subId= " + subId + " i: " + (i));
fileContents = subm.getSub_files()[i].getFileContents();
//write the content
os.write(fileContents, 0, fileContents.length);
}
//if the action is openandclose
if (action.equalsIgnoreCase("openandclose"))
{
String projectid = request.getParameter("projId"); //The project id
String status = submit.getProjectStatus(projectid); //The status
//Set the status in session
request.getSession().setAttribute("status", status);
Calendar openDate = submit.getProjectOpendate(projectid); //The open date
//set the open date in session
request.getSession().setAttribute("openDate", openDate);
Calendar closeDate = submit.getProjectClosedate(projectid); //The close date
//set the close date in session
request.getSession().setAttribute("closeDate", closeDate);
//call to remote method getProjectByID
Projects project = submit.getProjectByID(projectid);
//set the Project in session
request.getSession().setAttribute("project", project);
//Forwarding to next jsp
RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/jsp/openandcloseproject.jsp");
requestDispatcher.forward(request, response);
}
//if the action is setopenandclose
if (action.equalsIgnoreCase("setopenandclose"))
{
String projectid = request.getParameter("projId"); //The project id
String opentime = request.getParameter("opentime"); //The open time
String closetime = request.getParameter("closetime"); //The close time
String status = request.getParameter("status"); //The status
boolean asOpendate = false; //The flag for open date
boolean asClosedate = false; //The flag for close date
String time = opentime; //The time
//validation on open date
if (opentime != null && !opentime.equals("") && !opentime.equals("None") && !opentime.equals("\\s")) {
String[] result = time.split("\\s");
String[] date = result[0].split("-");
//Instantiate the calendar
Calendar projTimeOpen = Calendar.getInstance();
//set open date in calendar object
projTimeOpen.set( projTimeOpen.YEAR, Integer.parseInt(date[0]));
projTimeOpen.set( projTimeOpen.MONTH, Integer.parseInt(date[1])-1);
projTimeOpen.set( projTimeOpen.DATE, Integer.parseInt(date[2]));
//call to remote method setProjectOpendate
submit.setProjectOpendate(projectid, projTimeOpen);
asOpendate = true;
} else {
//call to remote method setProjectOpendate
submit.setProjectOpendate(projectid, null);
}
time = closetime;
//validation on close date
if (closetime != null && !closetime.equals("") && !closetime.equals("None") && !closetime.equals("\\s")) {
String[] result = time.split("\\s");
String[] date = result[0].split("-");
//Instantiate Calendar
Calendar projTimeClose = Calendar.getInstance();
projTimeClose.set( projTimeClose.YEAR, Integer.parseInt(date[0]));
projTimeClose.set( projTimeClose.MONTH, Integer.parseInt(date[1])-1);
projTimeClose.set( projTimeClose.DATE, Integer.parseInt(date[2]));
//call to remote method setProjectClosedate
submit.setProjectClosedate(projectid, projTimeClose);
asClosedate = true;
} else {
//call to remote method setProjectClosedate
submit.setProjectClosedate(projectid, null);
}
//call to remote method setProjectStatus
submit.setProjectStatus(projectid, status);
Projects[] listOfProject = null; //The list of projects
//call to remote method setProjectStatus
listOfProject = submit.getProjectList();
//set the list of projects in session
request.getSession().setAttribute("listOfProject", listOfProject);
//Forwarding to next jsp
RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/jsp/listprojectstaff.jsp");
requestDispatcher.forward(request, response);
}
//if action is createproject
if (action.equalsIgnoreCase("createproject"))
{
Subjects[] listOfSubjects = null; //the list of subjects
//call to remote method getSubjectList
listOfSubjects = submit.getSubjectList();
//set the list of subjects in session
request.getSession().setAttribute("listsubjects", listOfSubjects);
//Forwarding to next jsp
RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/jsp/createproject.jsp");
requestDispatcher.forward(request, response);
}
//if action is addproject
if (action.equalsIgnoreCase("addproject"))
{
String subjCode = new String(request.getParameter("subjCode")); //The subject code
String year = new String(request.getParameter("year")); //The year
String semester = new String(request.getParameter("semester")); //The semester
String projName = request.getParameter("projName"); //The project name
String projDesc = new String(request.getParameter("projDesc")); //The project description
String projURL = new String(request.getParameter("projURL")); //The project url
//call to remote method addProject
submit.addProject(subjCode, year, semester, projName, projDesc, projURL);
Projects[] listOfProject = null; //The list of project
//Call to remote method getProjectStaff
listOfProject = submit.getProjectStaff(staffId);
//set the list of projects in session
request.getSession().setAttribute("listOfProject", listOfProject);
//Forwarding to next jsp
RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/jsp/listprojectstaff.jsp");
requestDispatcher.forward(request, response);
}
}
/**
* Handles the post request
* @param request
* @param response
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -