Friday, December 28, 2012

Java Servlet with RequestDispatcher


import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/Display")
public class Display extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String studentId = request.getParameter("studentid");
String address = "jsp/StudentJSP.jsp";
StudentBean studentBean = null;

if(studentId != null){
studentBean = StudentDAO.GetStudentSurveyData(studentId);
}

HttpSession session = request.getSession();

if(studentBean == null){
address = "jsp/NoSuchStudentJSP.jsp";
session.setAttribute("badId", studentId);
}
else{
session.setAttribute("student", studentBean);
}

RequestDispatcher dispatcher = request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String studentId = request.getParameter("studentId");
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String street = request.getParameter("street");
String zip = request.getParameter("zip");
String city = request.getParameter("city");
String state = request.getParameter("state");
String telephone = request.getParameter("telephone");
String email = request.getParameter("email");
String url = request.getParameter("url");
String dateOfSurvey = request.getParameter("dateOfSurvey");
String graduationMonth = request.getParameter("graduationMonth");
String graduationYear = request.getParameter("graduationYear");
String[] likes = request.getParameterValues("likes");
String interests = request.getParameter("interests");
String recommendation  = request.getParameter("recommendation");
String comments = request.getParameter("comments");
String data = request.getParameter("data");

String likesString = "";
for(int i=0; i< likes.length; i++){
likesString += likes[i] + ", ";
}
likesString = likesString.substring(0, likesString.length() - 2);

StudentBean studentBean = new StudentBean(studentId, firstName, lastName, street, zip, city, state,
telephone, email, url, dateOfSurvey, graduationMonth,
graduationYear, likesString, interests, recommendation, comments);

StudentDAO.SaveStudentSurveyFormData(studentBean);

DataBean dataBean = DataProcessor.ComputeMeanAndStandardDeviation(data);

List<String> surveyIdsBean = StudentDAO.GetSurveyIds();

HttpSession session = request.getSession();

session.setAttribute("student", studentBean);
session.setAttribute("data", dataBean);
session.setAttribute("surveys", surveyIdsBean);

String address = "jsp/SimpleAcknowledgementJSP.jsp";
if(dataBean.getMean() > 90){
address = "jsp/WinnerAcknowledgementJSP.jsp";
}
RequestDispatcher dispatcher = request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}
}

No comments: