jolenic

LogsServlet

Oct 12th, 2020
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. package servlets;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9.  
  10. import beans.TextLog;
  11. import dao.ApplicationDao;
  12. import inmemory.ApplicationInMemory;
  13.  
  14. public class LogsServlet extends HttpServlet {
  15.     private static final long serialVersionUID = 1L;
  16.  
  17.     public LogsServlet() {
  18.         super();
  19.     }
  20.  
  21.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  22.             throws ServletException, IOException {
  23.         String title = request.getParameter("post_title");
  24.         String content = request.getParameter("post_content");
  25.         String toDelete = request.getParameter("to_delete");
  26.  
  27.         String htmlResponse = "<html><head><title>Logs</title></head><body><h4>Here are your logs:</h4></body></html>";
  28.         PrintWriter writer = response.getWriter();
  29.         // ApplicationInMemory.allLogs.forEach(action);
  30.  
  31.         if ((title != null) && (content != null)) {
  32.             TextLog test = new TextLog(title, content);
  33.             ApplicationInMemory.allLogs.add(test);
  34.             writer.write("TextLog succesfully created!");
  35.             try {
  36.                 ApplicationDao appDao = new ApplicationDao();
  37.                 if (appDao.getConnection() != null) {
  38.                     writer.write("<p>Connection made</p>");
  39.                 }
  40.                 if (appDao.insertNewLog(test.getId().toString(), test.getTitle(), test.getContent(),
  41.                         test.getCreateTimestamp().toString()) != 0) {
  42.                     writer.write("Log added to database!");
  43.                 } else {
  44.                     writer.write("Not added to database :(");
  45.                 }
  46.             } catch (Exception e) {
  47.                 e.printStackTrace();
  48.                 writer.write("Database connection not working :(");
  49.             }
  50.  
  51.         }
  52.        
  53.         writer.write("<h3>Assignment 3</h3><form method=\"get\">\r\n" + "   <table>\r\n" + "<tr>\r\n"
  54.                 + "         <td>\r\n" + "<label for=\"post_title\">Post Title: </label>\r\n"
  55.                 + "             <input type=\"text\" id=\"post_title\" name=\"post_title\" required=\"required\" />\r\n"
  56.                 + "         </td>\r\n" + "</tr>\r\n" + "<tr>\r\n" + "<td>\r\n"
  57.                 + "             <label for=\"post_content\">Post Content: </label>\r\n"
  58.                 + "             <input type=\"text\" id=\"post_content\" name=\"post_content\" required=\"required\" />\r\n"
  59.                 + "         </td>\r\n" + "      </tr>\r\n" + "<tr>\r\n" + "<td>\r\n"
  60.                 + "             <a href=\"/logsServlet\"></a><input type=\"submit\" value=\"Submit\" /></a>\r\n"
  61.                 + "         </td>\r\n" + "</tr>\r\n" + "</table>\r\n" + "</form>");
  62.        
  63.         writer.write(htmlResponse);
  64.         for (TextLog l : ApplicationInMemory.allLogs) {
  65.             writer.write("<p> </p>" + l.toHTMLString());
  66.             //writer.write("<input type=\"submit\">);
  67.         }
  68.     }
  69. }
  70.  
  71.  
Add Comment
Please, Sign In to add comment