Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package servlet_package;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.io.IOException;
- import java.io.PrintWriter;
- import static java.lang.System.out;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- *
- * @author MuhammedMustafa
- */
- @WebServlet(urlPatterns = {"/first"})
- public class first extends HttpServlet {
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
- * methods.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- public void doGet(HttpServletRequest req, HttpServletResponse res)
- throws ServletException, IOException {
- try {
- res.setContentType("text/html");
- PrintWriter pw = res.getWriter();
- String url = "jdbc:postgresql://localhost:5432/qupass";
- String uname = "qupass";
- String pass = "pass";
- Class.forName("org.postgresql.Driver");
- Connection conn = DriverManager.getConnection(url, uname, pass);
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("Select * from corporate");
- pw.print("<html>");
- pw.print("<head>");
- pw.print("</head>");
- pw.print("<body>");
- pw.print("<table border='2'");
- pw.print("<tr>"
- + "<th>id</th>"
- + "<th>version</th>"
- + "<th>corporate_brand</th>"
- + "<th>name</th>"
- + "<th>invoice_info</th>"
- + "<th>tax_office</th>"
- + "</tr>");
- while (rs.next()) {
- pw.print("<tr>");
- pw.print("<td>" + rs.getString(1) + "</td>");
- pw.print("<td>" + rs.getString(2) + "</td>");
- pw.print("<td>" + rs.getString(3) + "</td>");
- pw.print("<td>" + rs.getString(4) + "</td>");
- pw.print("<td>" + rs.getString(6) + "</td>");
- pw.print("<td>" + rs.getString(8) + "</td>");
- pw.print("</tr>");
- }
- pw.print("</table>");
- pw.print("</body></html>");
- conn.close();
- rs.close();
- pw.close();
- stmt.close();
- } catch (Exception e) {
- out.println(e.getMessage());
- }
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Returns a short description of the servlet.
- *
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement