Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package nl.fontys.pro.web;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- *
- * @author ÄktschnDan
- */
- public class ProcessLogin extends HttpServlet {
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- boolean regflag=false;
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- try{
- if(request.getParameter("register").equals("1")){
- regflag=true;
- }
- }
- catch(Exception e){
- regflag=false;
- }
- try {
- LoginData database= LoginData.getInstance();
- //
- //database.addAccount("testuser","test");
- //
- String user=request.getParameter("username");
- String password=request.getParameter("password");
- if(regflag){
- database.addAccount(user, password);
- //out.println("<h1>thanks, your account has been activated<h1>");
- //out.println("<a href=\"login.html\"> back to login page</a>");
- printLoginPage(out,"<h1>thanks, your account has been activated</h1>",user,password);
- }
- else{
- switch(database.checkLogin(user, password)){
- case PASSED : //out.println("<h1> hello Mr. "+ user +", your password "+ password +" will now be broadcasted :-)");
- //out.println("<br> <a href=\"login.html\"> your application</a>");
- //out.println("<br> <a href=\"login.html\"> back to login page</a>");break;
- printLoginPage(out,"<h1> hello Mr. "+ user +", your password "+ password +" will now be broadcasted :-)</h1>",user,password);break;
- case WRONGPWD : //out.println("<h1> hello Mr. "+ user +", your password is incorrect. Try again.</h1>");break;
- printLoginPage(out,"<h1> hello Mr. "+ user +", your password is incorrect. Try again.</h1>",user,password);break;
- case NOACCOUNT : //out.println("<h1> there is no such account!</h1>");break;
- printLoginPage(out,"<h1> there is no such account!</h1>",user,password);break;
- case EMPTY : //out.println("<h1>please fill in the blanks</h1>");break;
- printLoginPage(out,"<h1>please fill in the blanks</h1>",user,password);break;
- }
- }
- } finally {
- out.close();
- }
- }
- private void printLoginPage(PrintWriter out,String message, String user, String pwd){
- 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> </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>  <br><input type=\"submit\" name=\"submitButton\" value=\"login\"></form></td></tr></table>  <br>new to Fontys Portal?   <a href=\"register.html\"> >> get yourself an Account</a><hr>  <br>Design by Me</body></html>");
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Returns a short description of the servlet.
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement