@Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {

    // create the workbook, its worksheet, and its title row
    Workbook workbook = new HSSFWorkbook();
    Sheet sheet = workbook.createSheet("User table");
    Row row = sheet.createRow(0);
    row.createCell(0).setCellValue("The User table");

    // create the header row
    row = sheet.createRow(2);
    row.createCell(0).setCellValue("UserID");
    row.createCell(1).setCellValue("LastName");
    row.createCell(2).setCellValue("FirstName");
    row.createCell(3).setCellValue("Email");

    try {
      // read database rows
      ConnectionPool pool = ConnectionPool.getInstance();
      Connection connection = pool.getConnection();
      Statement statement = connection.createStatement();
      String query = "SELECT * FROM User ORDER BY UserID";
      ResultSet results = statement.executeQuery(query);

      // create spreadsheet rows
      int i = 3;
      while (results.next()) {
        row = sheet.createRow(i);
        row.createCell(0).setCellValue(results.getInt("UserID"));
        row.createCell(1).setCellValue(results.getString("LastName"));
        row.createCell(2).setCellValue(results.getString("FirstName"));
        row.createCell(3).setCellValue(results.getString("Email"));
        i++;
      }
      results.close();
      statement.close();
      connection.close();
    } catch (SQLException e) {
      this.log(e.toString());
    }

    // set response object headers
    response.setHeader("content-disposition", "attachment; filename=users.xls");
    response.setHeader("cache-control", "no-cache");

    // get the output stream
    String encodingString = request.getHeader("accept-encoding");
    OutputStream out;
    if (encodingString != null && encodingString.contains("gzip")) {
      out = new GZIPOutputStream(response.getOutputStream());
      response.setHeader("content-encoding", "gzip");
      // System.out.println("User table encoded with gzip");
    } else {
      out = response.getOutputStream();
      // System.out.println("User table not encoded with gzip");
    }

    // send the workbook to the browser
    workbook.write(out);
    out.close();
  }
Beispiel #2
0
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String userAgent = request.getHeader("user-agent");
    if (userAgent.matches(".*Android.*")) {
      int flag = 0;
      try {
        Connection con = DBConnectionAdmin.getConnection();
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT email,password FROM admin");

        String emailmember = request.getParameter("mail");
        String passwordmember = request.getParameter("pass");
        while (rs.next()) {
          String email = rs.getString("email");
          String password = rs.getString("password");
          if (email.equals(emailmember) && password.equals(passwordmember)) {
            flag = 1; // success in login
            break;
          }
        }

        /*if(request.getParameter("remember")!=null ){
        	if( request.getParameter("remember").equals("yes") ){
        					Cookie c1 = new Cookie("email", emailmember);
        					c1.setMaxAge(60*60*24*365);
        					response.addCookie(c1);
        					Cookie c2 = new Cookie("password", passwordmember);
        					c2.setMaxAge(60*60*24*365);
        					response.addCookie(c2);

        	}
        }
        else{
        	Cookie[] cookies = request.getCookies();
        	if (cookies != null) {
        		for(Cookie cookie: cookies) {
        			if(cookie.getName().equals("email")){
        				cookie.setMaxAge(0);
        				response.addCookie(cookie);
        			}
        			else if(cookie.getName().equals("password")){
        				cookie.setMaxAge(0);
        				response.addCookie(cookie);
        			}
        		}
        	}

        }*/

      } catch (SQLException | ClassNotFoundException e1) {
        e1.printStackTrace();
      }
      if (flag == 1) {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\">\n";
        String title = "You Signed In Successfully";
        out.println(
            docType
                + "<HTML>\n"
                + "<HEAD>"
                + "<script type='text/javascript'>"
                + "function changeweb() {"
                + "   AndroidFunction.Second();"
                + "}"
                + "</script>"
                + "<TITLE>"
                + title
                + "</TITLE></HEAD>\n"
                + "<BODY BGCOLOR=\"#FDF5E6\" ><H1 ALIGN=CENTER>"
                + title
                + "</H1>\n"
                + "<button type='button' onclick=\"changeweb()\">Click Me!</button>"
                + "</BODY></HTML>");
        HttpSession session = request.getSession(true);
        String email = request.getParameter("mail");
        session.setAttribute("email", email);

      } else {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\">\n";
        String title = "Wrong password or E-mail.Please try again.";
        out.println(
            docType
                + "<HTML>\n"
                + "<HEAD><TITLE>"
                + title
                + "</TITLE></HEAD>\n"
                + "<BODY BGCOLOR=\"#FDF5E6\" ><H1 ALIGN=CENTER>"
                + title
                + "</H1>\n</BODY></HTML>");
      }
    } else {
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\">\n";
      String title = "Main";
      out.println(
          docType
              + "<HTML>\n"
              + "<HEAD><TITLE>"
              + title
              + "</TITLE>"
              + "<style>"
              + "ul"
              + "{"
              + "list-style-type:none;"
              + "margin:0;"
              + "padding:0;"
              + "overflow:hidden;"
              + "}"
              + "li"
              + "{"
              + "float:left;"
              + "}"
              + "a:link,a:visited"
              + "{"
              + "display:block;"
              + "font-weight:bold;"
              + "font-size:200%;"
              + "color:#FFFFFF;"
              + "background-color:#98bf21;"
              + "text-align:center;"
              + "padding:4px;"
              + "text-decoration:none;"
              + "text-transform:uppercase;"
              + "border: 1px solid black;"
              + "padding:25px"
              + "}"
              + "a:hover,a:active"
              + "{"
              + "background-color:#7A991A;"
              + "}"
              + "</style>"
              + "</HEAD>\n"
              + "<body style='background-color:black' >\n");

      out.println("<ul>");
      out.println("<li><a href='http://localhost:8083/TestProject/main.html'>New Event!!</a></li>");
      out.println("<li><a href='post.Eventlist'>Events</a></li>");
      out.println("<li><a href='post.Myeventlist'>My Events</a></li>");
      out.println("<li><a href='post.Memberslist'>Members</a></li>");
      out.println("<li><a href='post.Profile'>Profile</a></li>");
      out.println("<li><a href='post.Help'>Help</a></li>");
      out.println("</ul>");
      out.println("</BODY></HTML>");
    }
  }