0

I am creating a simple web app where my main goal is to retrieve records from a table in MySQL and display the results on the web (front-end).

I wrote a simple program in Intellij where my index.jsp looks like below:

<head>

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <style>
    body {
      font-family: Arial;
    }

    * {
      box-sizing: border-box;
    }

    form.example input[type=text] {
      padding: 10px;
      font-size: 17px;
      border: 1px solid grey;
      float: left;
      width: 80%;
      background: #f1f1f1;
    }

    form.example button {
      float: left;
      width: 20%;
      padding: 10px;
      background: #2196F3;
      color: white;
      font-size: 18px;
      border: 1px solid grey;
      border-left: none;
      cursor: pointer;
    }

    form.example button:hover {
      background: #0b7dda;
    }

    form.example::after {
      content: "";
      clear: both;
      display: table;
    }
    #result{
      padding-top: 30px;
      width:40%;
      float:left;
      position:relative;
      left:35%;

    }

    ul {
      list-style-type: none;
      margin: 0;
      padding: 0;
      overflow: hidden;
      background-color: #333;
    }

    li {
      float: left;
    }

    li a {
      display: block;
      color: white;
      text-align: center;
      padding: 14px 16px;
      text-decoration: none;
    }

    li a:hover {
      background-color: #111;
    }

    li a:active{

    }


  </style>



  <title>File Queue table</title>
  <body>
  <h1>Simple File Queue table</h1>



  </body>

<script>

  function queue_display(){




  }






</script>




</html>

MyServlet.java :

  @WebServlet(name = "MyServlet")
        public class MyServlet extends HttpServlet {
            protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



            }





            protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

           response.setContentType("text/html");
         PrintWriter out = response.getWriter();


         try{
         Class.forName("com.mysql.jdbc.Driver");
         Connection conn = DriverManager.getConnection( "jdbc:mysql://126.32.3.11:3306/test", "root", "root");
         String sql ="Select * from filequeue;";
         statement = conn.createStatement();
         ResultSet resultSet = statement.executeQuery(sql);

     while(resultSet.next()){

     // display each record
       resultSet.getString("UniqueID");
         resultSet.getString("FilePath");
         resultSet.getString("Status");
         resultSet.getString("DateTime");
         resultSet.getString("Error");
         list.add(resultSet);
     }

 resultSet.close();
     statement.close();
     conn.close();
     RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
     request.setAttribute("queryResults",list);
     dispatcher.forward(request,response);

     }catch(Exception e){

     System.out.println(e);

     }
        }

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">


    <servlet>
        <description></description>
        <display-name>Servlet</display-name>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class> MyServlet</servlet-class>

    </servlet>

    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/Servlet</url-pattern>
    </servlet-mapping>


</web-app>

I am new to web application, is there an efficient way to retrieve records from table and display on front end, without using servlet?

Daredevil
  • 1,672
  • 3
  • 18
  • 47
  • 1
    *web application...efficient way...without using servlet* Firstly `servlets` are the bread and butter of java web application programming, why would you **not** want to use them? Next, there are many ways to make your code in-efficient whether you are using `servlets` or not – Scary Wombat Dec 04 '18 at 05:26
  • I suggest that you step back for a while and re-read some tutorial stuff. This looks quite good https://o7planning.org/en/10285/create-a-simple-java-web-application-using-servlet-jsp-and-jdbc – Scary Wombat Dec 04 '18 at 05:30
  • downvoter, any comment? – Daredevil Dec 04 '18 at 06:26
  • @ScaryWombat Servlets helps the web app to get a request , is that right? – Daredevil Dec 04 '18 at 06:29
  • To over-simplify things, the servlet is the web-app, seriously suggest that you read the link I gave you, lots and lots of interesting information. – Scary Wombat Dec 04 '18 at 06:30
  • Yea i'm gonna read it right now. – Daredevil Dec 04 '18 at 06:32
  • I found another link: https://stackoverflow.com/questions/18997285/how-to-display-a-database-table-on-to-the-table-in-the-jsp-page where it is able to retrieve information without using servlet, is this a bad practice? – Daredevil Dec 04 '18 at 06:43
  • If you read the link I gave you, you would notice *The principle when programming Servlet + JSP These are the principles that you should keep in mind to be able to build a Web application using Servlet + JSP satisfying criteria: code is simple, easy to understand and easy to maintain. The principles: Never allow users to directly access to your JSP page. JSP is only considered as the place to display interface. Servlet acts as the controller of the application flows and program logical processing.* – Scary Wombat Dec 04 '18 at 06:47
  • Can you check my edited post if i am in the right direction? After following your tutorial – Daredevil Dec 04 '18 at 07:28
  • No, you want an https://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#executeQuery() which will return to you a ResultSet – Scary Wombat Dec 04 '18 at 07:46
  • something like above? – Daredevil Dec 04 '18 at 07:53
  • yeah, something like above – Scary Wombat Dec 04 '18 at 07:56
  • Is there an API to display the results in the while loop without using get? – Daredevil Dec 04 '18 at 07:57
  • probably what you what is to create your own Data Object class (DO) and have a `List` or them, that can then be added to your request (or session) and then use JSTL in the JSP to display – Scary Wombat Dec 04 '18 at 07:59
  • I think the idea is like what i wrote above? – Daredevil Dec 04 '18 at 08:09
  • No, maybe `String f1 = resultSet.getString("UniqueID");` - you can not add the `ResultSet` – Scary Wombat Dec 04 '18 at 08:12
  • Then I have to add all 5 columns of them in the list – Daredevil Dec 04 '18 at 08:16
  • As I said before *create your own Data Object class (DO)* – Scary Wombat Dec 04 '18 at 08:18
  • When you have learnt to walk, you may consider using JPA and using the reverse engineering tools available in IDEs such as Eclipse – Scary Wombat Dec 04 '18 at 08:19
  • Yea web app is still fresh to me. So in my data object class, what must be contain inside? – Daredevil Dec 04 '18 at 08:20

1 Answers1

-2

@DareDevil You can fetch the records from the datastore via ajax call. Whilst form submit is another alternative for making a backend call from jsp, do prefer ajax if you need to load the contents without a page reload. Hope this helps.

technoJ
  • 175
  • 1
  • 1
  • 16