daily pastebin goal
47%
SHARE
TWEET

Untitled

a guest Jul 12th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package Servlets;
  2.  
  3. import java.io.IOException;
  4. import java.sql.SQLException;
  5. import java.util.Date;
  6.  
  7. import javax.servlet.RequestDispatcher;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.annotation.WebServlet;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13.  
  14. import Achievement.Achievement;
  15. import DAO.AchievementDao;
  16. import DAO.AnswerDao;
  17. import DAO.QuestionDao;
  18. import DAO.QuizDao;
  19. import DAO.UserDao;
  20. import Questions.Question;
  21. import Quiz.DoneQuiz;
  22. import User.User;
  23.  
  24. /**
  25.  * Servlet implementation class answerQuestionServlet
  26.  */
  27. @WebServlet("/answerQuestionServlet")
  28. public class answerQuestionServlet extends HttpServlet {
  29.     private static final long serialVersionUID = 1L;
  30.        
  31.     /**
  32.      * @see HttpServlet#HttpServlet()
  33.      */
  34.     public answerQuestionServlet() {
  35.         super();
  36.         // TODO Auto-generated constructor stub
  37.     }
  38.  
  39.     /**
  40.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  41.      */
  42.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  43.         // TODO Auto-generated method stub
  44.         response.getWriter().append("Served at: ").append(request.getContextPath());
  45.     }
  46.  
  47.     /**
  48.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  49.      */
  50.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  51.         String ans = " ";
  52.         ans = request.getParameter("response");
  53.         if (ans == null) ans = " ";
  54.         int quizid = Integer.parseInt(request.getParameter("quizId"));
  55.         int questionNum = Integer.parseInt(request.getParameter("question"));
  56.         Integer c;
  57.         if ((Integer) request.getSession().getAttribute("score") == null) c = 0;
  58.         else c = (Integer) request.getSession().getAttribute("score") ;
  59.         Integer score =c;
  60.         UserDao us = new UserDao();
  61.         AnswerDao an = new AnswerDao();
  62.         QuestionDao qp = new QuestionDao(an);
  63.         QuizDao complete = new QuizDao(qp, us);
  64.         Question quest = qp.getAllQuestions(quizid).get(questionNum-1);
  65.         String correctAns = (quest.getCorrectAnswer().getPossibleAnswer());
  66.         String answered;
  67.         String ll = "quiz"+quizid+"question"+questionNum;
  68.         if  (request.getSession().getAttribute(ll) != null)  answered = "answered";
  69.         else answered = null;
  70.         if (ans.equals(correctAns) && answered ==null) score++;
  71.         System.out.println(score);
  72.         request.getSession().setAttribute("score", score);
  73.         RequestDispatcher disp ;
  74.         request.getSession().setAttribute(ll, "alreadyAnswered");
  75.         if (questionNum == qp.getAllQuestions(quizid).size()) {
  76.             AchievementDao ach = new AchievementDao();
  77.             Date startdate = (Date) request.getSession().getAttribute("startdate"+quizid);
  78.             User usr = (User) request.getSession().getAttribute("user");
  79.             Date dat =  new Date();
  80.             DoneQuiz don = new DoneQuiz(usr.getId(), quizid, score, dat.toString(), (int)((dat.getTime() - startdate.getTime())/1000) );
  81.             complete.insertCompletedQuiz(don);
  82.             if (complete.getCompleteQuizes(usr.getId()).size() >=2) {
  83.                 usr.addAchievement(new Achievement("Quiz Machine", usr.getId(), 4, dat.toString()));
  84.             }
  85.             disp = request.getRequestDispatcher("doneQuiz.jsp?"+quizid);
  86.             disp.forward(request, response);
  87.         }else {
  88.             String link = "writeQuiz.jsp?quizId=" + complete.getQuiz(quizid).getQuizId() + "&question=" + (questionNum);
  89.             disp = request.getRequestDispatcher(link);
  90.             disp.forward(request, response);
  91.         }
  92.         doGet(request, response);
  93.     }
  94.  
  95. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top