Advertisement
Guest User

Untitled

a guest
May 16th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.56 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package nl.fontys.pro.web;
  7.  
  8. import java.io.IOException;
  9. import java.io.PrintWriter;
  10. import javax.servlet.ServletException;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14.  
  15.  
  16. /**
  17.  *
  18.  * @author ÄktschnDan
  19.  */
  20. public class ProcessLogin extends HttpServlet {
  21.    
  22.     /**
  23.      * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
  24.      * @param request servlet request
  25.      * @param response servlet response
  26.      * @throws ServletException if a servlet-specific error occurs
  27.      * @throws IOException if an I/O error occurs
  28.      */
  29.  
  30.     boolean regflag=false;
  31.  
  32.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  33.     throws ServletException, IOException {
  34.         response.setContentType("text/html;charset=UTF-8");
  35.         PrintWriter out = response.getWriter();
  36.  
  37.         try{
  38.              if(request.getParameter("register").equals("1")){
  39.                 regflag=true;
  40.              }
  41.             }
  42.              catch(Exception e){
  43.              regflag=false;
  44.              }
  45.  
  46.  
  47.         try {
  48.  
  49.  
  50.             LoginData database= LoginData.getInstance();
  51.  
  52.             //
  53.             //database.addAccount("testuser","test");
  54.             //
  55.            
  56.             String user=request.getParameter("username");
  57.             String password=request.getParameter("password");
  58.  
  59.                 if(regflag){
  60.  
  61.                 database.addAccount(user, password);
  62.                 //out.println("<h1>thanks, your account has been activated<h1>");
  63.                 //out.println("<a href=\"login.html\"> back to login page</a>");
  64.  
  65.                 printLoginPage(out,"<h1>thanks, your account has been activated</h1>",user,password);
  66.             }
  67.                 else{
  68.  
  69.                 switch(database.checkLogin(user, password)){
  70.                 case PASSED : //out.println("<h1> hello Mr. "+ user +", your password "+ password +" will now be broadcasted :-)");
  71.                               //out.println("<br> <a href=\"login.html\"> your application</a>");
  72.                               //out.println("<br> <a href=\"login.html\"> back to login page</a>");break;
  73.                                 printLoginPage(out,"<h1> hello Mr. "+ user +", your password "+ password +" will now be broadcasted :-)</h1>",user,password);break;
  74.                 case WRONGPWD : //out.println("<h1> hello Mr. "+ user +", your password is incorrect. Try again.</h1>");break;
  75.                                 printLoginPage(out,"<h1> hello Mr. "+ user +", your password is incorrect. Try again.</h1>",user,password);break;
  76.                 case NOACCOUNT : //out.println("<h1> there is no such account!</h1>");break;
  77.                                 printLoginPage(out,"<h1> there is no such account!</h1>",user,password);break;
  78.                 case EMPTY : //out.println("<h1>please fill in the blanks</h1>");break;
  79.                                 printLoginPage(out,"<h1>please fill in the blanks</h1>",user,password);break;
  80.             }
  81.                 }
  82.  
  83.  
  84.            
  85.  
  86.                
  87.  
  88.         } finally {
  89.             out.close();
  90.         }
  91.     }
  92.  
  93.     private void printLoginPage(PrintWriter out,String message, String user, String pwd){
  94.         out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"><html><head><title></title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></head><body><table border=\"0\" width=\"1024\"><tr><td><img src=\"img/fontys_logo.jpg\" align=\"top\" alt=\"Fontys,junge\"></td><td align=\"center\"><h1>The Fontys Portal, boy </h1> </td> </tr></table><hr><table border=\"0\" width=\"1024\"><tr><td>&nbsp</td></tr><tr><td><form action=\"ProcessLogin\">"+ message+"<br> Login: <input name=\"username\" type=\"text\"  maxlength=\"30\" value=\""+ user +"\"> <br>Password: <input name=\"password\" type=\"text\"  maxlength=\"30\" value=\""+pwd+"\"> <br>&nbsp <br><input type=\"submit\" name=\"submitButton\" value=\"login\"></form></td></tr></table>&nbsp <br>new to Fontys Portal? &nbsp;&nbsp <a href=\"register.html\"> >> get yourself an Account</a><hr>&nbsp <br>Design by Me</body></html>");
  95.     }
  96.  
  97.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  98.     /**
  99.      * Handles the HTTP <code>GET</code> method.
  100.      * @param request servlet request
  101.      * @param response servlet response
  102.      * @throws ServletException if a servlet-specific error occurs
  103.      * @throws IOException if an I/O error occurs
  104.      */
  105.     @Override
  106.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  107.     throws ServletException, IOException {
  108.         processRequest(request, response);
  109.     }
  110.  
  111.     /**
  112.      * Handles the HTTP <code>POST</code> method.
  113.      * @param request servlet request
  114.      * @param response servlet response
  115.      * @throws ServletException if a servlet-specific error occurs
  116.      * @throws IOException if an I/O error occurs
  117.      */
  118.     @Override
  119.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  120.     throws ServletException, IOException {
  121.         processRequest(request, response);
  122.     }
  123.  
  124.     /**
  125.      * Returns a short description of the servlet.
  126.      * @return a String containing servlet description
  127.      */
  128.     @Override
  129.     public String getServletInfo() {
  130.         return "Short description";
  131.     }// </editor-fold>
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement