Advertisement
Guest User

servlet

a guest
Aug 21st, 2017
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 KB | None | 0 0
  1. package servlet_package;
  2.  
  3. /*
  4.  * To change this license header, choose License Headers in Project Properties.
  5.  * To change this template file, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8. import java.io.IOException;
  9. import java.io.PrintWriter;
  10. import static java.lang.System.out;
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.ResultSet;
  14. import java.sql.Statement;
  15. import javax.servlet.ServletException;
  16. import javax.servlet.annotation.WebServlet;
  17. import javax.servlet.http.HttpServlet;
  18. import javax.servlet.http.HttpServletRequest;
  19. import javax.servlet.http.HttpServletResponse;
  20.  
  21. /**
  22.  *
  23.  * @author MuhammedMustafa
  24.  */
  25. @WebServlet(urlPatterns = {"/first"})
  26. public class first extends HttpServlet {
  27.  
  28.     /**
  29.      * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  30.      * methods.
  31.      *
  32.      * @param request servlet request
  33.      * @param response servlet response
  34.      * @throws ServletException if a servlet-specific error occurs
  35.      * @throws IOException if an I/O error occurs
  36.      */
  37.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  38.             throws ServletException, IOException {
  39.         response.setContentType("text/html;charset=UTF-8");
  40.     }
  41.  
  42.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  43.     /**
  44.      * Handles the HTTP <code>GET</code> method.
  45.      *
  46.      * @param request servlet request
  47.      * @param response servlet response
  48.      * @throws ServletException if a servlet-specific error occurs
  49.      * @throws IOException if an I/O error occurs
  50.      */
  51.     @Override
  52.     public void doGet(HttpServletRequest req, HttpServletResponse res)
  53.             throws ServletException, IOException {
  54.         try {
  55.             res.setContentType("text/html");
  56.             PrintWriter pw = res.getWriter();
  57.  
  58.             String url = "jdbc:postgresql://localhost:5432/qupass";
  59.             String uname = "qupass";
  60.             String pass = "pass";
  61.             Class.forName("org.postgresql.Driver");
  62.  
  63.             Connection conn = DriverManager.getConnection(url, uname, pass);
  64.             Statement stmt = conn.createStatement();
  65.  
  66.             ResultSet rs = stmt.executeQuery("Select * from corporate");
  67.  
  68.             pw.print("<html>");
  69.             pw.print("<head>");
  70.             pw.print("</head>");
  71.             pw.print("<body>");
  72.  
  73.             pw.print("<table border='2'");
  74.  
  75.             pw.print("<tr>"
  76.                     + "<th>id</th>"
  77.                     + "<th>version</th>"
  78.                     + "<th>corporate_brand</th>"
  79.                     + "<th>name</th>"
  80.                     + "<th>invoice_info</th>"
  81.                     + "<th>tax_office</th>"
  82.                     + "</tr>");
  83.  
  84.             while (rs.next()) {
  85.  
  86.                 pw.print("<tr>");
  87.                 pw.print("<td>" + rs.getString(1) + "</td>");
  88.                 pw.print("<td>" + rs.getString(2) + "</td>");
  89.                 pw.print("<td>" + rs.getString(3) + "</td>");
  90.                 pw.print("<td>" + rs.getString(4) + "</td>");
  91.                 pw.print("<td>" + rs.getString(6) + "</td>");
  92.                 pw.print("<td>" + rs.getString(8) + "</td>");
  93.                 pw.print("</tr>");
  94.  
  95.             }
  96.  
  97.             pw.print("</table>");
  98.             pw.print("</body></html>");
  99.  
  100.             conn.close();
  101.             rs.close();
  102.             pw.close();
  103.             stmt.close();
  104.  
  105.         } catch (Exception e) {
  106.             out.println(e.getMessage());
  107.         }
  108.  
  109.     }
  110.  
  111.     /**
  112.      * Handles the HTTP <code>POST</code> method.
  113.      *
  114.      * @param request servlet request
  115.      * @param response servlet response
  116.      * @throws ServletException if a servlet-specific error occurs
  117.      * @throws IOException if an I/O error occurs
  118.      */
  119.     @Override
  120.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  121.             throws ServletException, IOException {
  122.         processRequest(request, response);
  123.     }
  124.  
  125.     /**
  126.      * Returns a short description of the servlet.
  127.      *
  128.      * @return a String containing servlet description
  129.      */
  130.     @Override
  131.     public String getServletInfo() {
  132.         return "Short description";
  133.     }// </editor-fold>
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement