Advertisement
Guest User

Untitled

a guest
Feb 15th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. import java.io.IOException;
  7. import java.io.PrintWriter;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.Statement;
  13. import javax.servlet.RequestDispatcher;
  14. import javax.servlet.ServletException;
  15. import javax.servlet.annotation.WebServlet;
  16. import javax.servlet.http.HttpServlet;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. /**
  20. *
  21. * @author user
  22. */
  23. @WebServlet(urlPatterns = {"/listBloggs"})
  24. public class listBloggs extends HttpServlet {
  25. Connection conn;
  26. Statement stmt;
  27. String description;
  28. String dburl = "jdbc:mysql://localhost:3306/oru?zeroDateTimeBehavior=convertToNull";
  29. String Username = "root";
  30. String PassWord = "";
  31. /**
  32. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  33. * methods.
  34. *
  35. * @param request servlet request
  36. * @param response servlet response
  37. * @throws ServletException if a servlet-specific error occurs
  38. * @throws IOException if an I/O error occurs
  39. */
  40. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  41. throws ServletException, IOException {
  42. response.setContentType("text/html;charset=UTF-8");
  43. try (PrintWriter out = response.getWriter()) {
  44. /* TODO output your page here. You may use following sample code. */
  45. RequestDispatcher rd = request.getRequestDispatcher("Blogg.jsp");
  46. rd.include(request, response);
  47. try {
  48. Class.forName("com.mysql.jdbc.Driver");
  49. conn = DriverManager.getConnection(dburl, Username, PassWord);
  50. PreparedStatement ps = conn.prepareStatement("select * from blogg order by id desc");
  51. ResultSet rs = ps.executeQuery();
  52. out.println("<h1>Inlägg</h1>");
  53.  
  54. while ( rs.next()) {
  55. out.println("<h3>" + rs.getString("description") + "</h3>");
  56. }
  57. conn.close();
  58. }
  59. catch(Exception ex) {
  60. System.out.println(ex.getMessage());
  61. }
  62. }
  63. }
  64. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  65. /**
  66. * Handles the HTTP <code>GET</code> method.
  67. *
  68. * @param request servlet request
  69. * @param response servlet response
  70. * @throws ServletException if a servlet-specific error occurs
  71. * @throws IOException if an I/O error occurs
  72. */
  73. @Override
  74. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  75. throws ServletException, IOException {
  76. processRequest(request, response);
  77. }
  78.  
  79. /**
  80. * Handles the HTTP <code>POST</code> method.
  81. *
  82. * @param request servlet request
  83. * @param response servlet response
  84. * @throws ServletException if a servlet-specific error occurs
  85. * @throws IOException if an I/O error occurs
  86. */
  87. @Override
  88. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  89. throws ServletException, IOException {
  90. processRequest(request, response);
  91. }
  92.  
  93. /**
  94. * Returns a short description of the servlet.
  95. *
  96. * @return a String containing servlet description
  97. */
  98. @Override
  99. public String getServletInfo() {
  100. return "Short description";
  101. }// </editor-fold>
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement