Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package servlets;
- 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;
- import beans.TextLog;
- import dao.ApplicationDao;
- import inmemory.ApplicationInMemory;
- public class LogsServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- public LogsServlet() {
- super();
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String title = request.getParameter("post_title");
- String content = request.getParameter("post_content");
- String toDelete = request.getParameter("to_delete");
- String htmlResponse = "<html><head><title>Logs</title></head><body><h4>Here are your logs:</h4></body></html>";
- PrintWriter writer = response.getWriter();
- // ApplicationInMemory.allLogs.forEach(action);
- if ((title != null) && (content != null)) {
- TextLog test = new TextLog(title, content);
- ApplicationInMemory.allLogs.add(test);
- writer.write("TextLog succesfully created!");
- try {
- ApplicationDao appDao = new ApplicationDao();
- if (appDao.getConnection() != null) {
- writer.write("<p>Connection made</p>");
- }
- if (appDao.insertNewLog(test.getId().toString(), test.getTitle(), test.getContent(),
- test.getCreateTimestamp().toString()) != 0) {
- writer.write("Log added to database!");
- } else {
- writer.write("Not added to database :(");
- }
- } catch (Exception e) {
- e.printStackTrace();
- writer.write("Database connection not working :(");
- }
- }
- writer.write("<h3>Assignment 3</h3><form method=\"get\">\r\n" + " <table>\r\n" + "<tr>\r\n"
- + " <td>\r\n" + "<label for=\"post_title\">Post Title: </label>\r\n"
- + " <input type=\"text\" id=\"post_title\" name=\"post_title\" required=\"required\" />\r\n"
- + " </td>\r\n" + "</tr>\r\n" + "<tr>\r\n" + "<td>\r\n"
- + " <label for=\"post_content\">Post Content: </label>\r\n"
- + " <input type=\"text\" id=\"post_content\" name=\"post_content\" required=\"required\" />\r\n"
- + " </td>\r\n" + " </tr>\r\n" + "<tr>\r\n" + "<td>\r\n"
- + " <a href=\"/logsServlet\"></a><input type=\"submit\" value=\"Submit\" /></a>\r\n"
- + " </td>\r\n" + "</tr>\r\n" + "</table>\r\n" + "</form>");
- writer.write(htmlResponse);
- for (TextLog l : ApplicationInMemory.allLogs) {
- writer.write("<p> </p>" + l.toHTMLString());
- //writer.write("<input type=\"submit\">);
- }
- }
- }
Add Comment
Please, Sign In to add comment