Retrieve values from JDBC and use JSTL tags to call the methods

前端 未结 3 921
既然无缘
既然无缘 2020-12-15 14:27

Below is the code which i have written to retrieve values from the database (I have added the whole code so it will be easier for you to understand what i am trying to say h

3条回答
  •  心在旅途
    2020-12-15 15:13

    Since it looks you're new to Java Web Programming, you can implement a full MVC scenario using JSP (View), Servlet (Controller) and Entities, DAOs and Service layer (Model). This is what you have:

    Model:

    • DBConnection as a database access class (Data access layer)
    • AddRecords as your DAO class (Data access layer)
    • No class as Service class (Business logic layer). For this example, let's have a RecordService class for this:

      public class RecordService {
          public RecordService() {
          }
          //since it has no real business logic, it will serve as facade
          public String[] getRecords() {
              AddRecords addRecords = new AddRecords();
              return addRecords.populateSelect();
          }
      }
      

    Controller:

    • No class for Controller yet. Since I assume you're using plain Java EE, you would use a Servlet (implementation based on Servlets StackOverflow wiki):

      @WebServlet("/RecordServlet")
      public class RecordsServlet extends HttpServlet {
          @Override
          protected void doGet(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
              //it will fire on a GET request (like accessing directly to the URL
              //from the browser)
              //here you should load the data for the View (JSP)
              loadData(request);
              //forward to show the view
              request.getRequestDispatcher("hello.jsp").forward(request, response);
          }
      
          @Override
          protected void doPost(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
              //it will fire on a POST request (like submitting the form using POST method)
              String selectedRecord = request.getParameter("selectedRecord");
              System.out.println(selectedRecord);
              request.setAttribute("selectedRecord", selectedRecord);
              loadData(request);
              //forward to show the view
              request.getRequestDispatcher("hello.jsp").forward(request, response);
          }
      
          //method to load the data available to select
          private void loadData(HttpServletRequest request) {
              RecordService recordService = new RecordService();
              String[] records = recordService.getRecords();
              //set the data as attribute on the request to be available on the View
              request.setAttribute("records", records);
          }
      }
      

    View:

    • You already have a JSP file. Since you haven't posted the name, let's call it hello.jsp. I will just adap your actual JSP code using JSTL:

      
      <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      
      
      Please select an element:

      You've selected ${selectedRecord}!

    Now, to run the example, build the project and access to it using http://localhost:8080/YourWebProjectName/RecordServlet. Also, you can set in the web.xml file:

    
        RecordServlet
    
    

    And just run the web project.

    Some notes:

    • Use relevant names for the class/methods, I used RecordXXX since you had this AddRecord class (it should be RecordDAO or something more useful to code readers like UserDAO or YourEntityDAO).
    • Distribute your classes through different packages. Every package should contain only relevant classes to its scope i.e. edu.home.dao for DAO classes, edu.home.service for service/business logic classes, and on...

提交回复
热议问题