P
P
parkito2016-08-25 22:00:18
Java
parkito, 2016-08-25 22:00:18

How to display the contents of an object in JavaServlets?

Hello. Help, please, to solve a problem.
Have a servlet

package controllers;

import manipulating.ClientDAO;
import manipulating.TariffDAO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "tariff", urlPatterns = {"/tariff"})
public class tariff extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userPath = request.getServletPath();
        System.out.println(userPath);
        PrintWriter out = response.getWriter();
        TariffDAO tariffDAO = new TariffDAO();
        out.write("<html>");
        out.write("<body>");
        out.write("Arr");
        tariffDAO.getTariffList(out);
        out.write("Arr");
        out.write("</body>");
        out.write("</html>");
//        request.getRequestDispatcher("/WEB-INF/views" + userPath + ".jsp").forward(request, response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Post");
    }
}


it should execute the method
tariffDAO.getTariffList(out);

public void getTariffList(PrintWriter out) {
        System.out.println("here");
        String query = "SELECT * FROM Tariff";
        List tariffList = MainDAO.getEntitiesList(new Tariff(), query);
        Tariff tariff = new Tariff();
        for (Object object : tariffList) {
            tariff = (Tariff) object;
            out.write(tariff.toString());
        }
    }


However, nothing happens. When debugging, it is clear that after this method the post ends.
Outside the servlet, the code works. What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evhen, 2016-08-26
@parkito

In general, is it right to do this
and what is going on here? Are you still writing in java 1.4?) And what is the nightmarish implementation of the dao?

TariffDAO tariffDAO = new TariffDAO();

...............

 tariffDAO.getTariffList(out);

............

List tariffList = MainDAO.getEntitiesList(new Tariff(), query);
        Tariff tariff = new Tariff();
        for (Object object : tariffList) {
            tariff = (Tariff) object;
            out.write(tariff.toString());
        }

Should look something like this
TariffDAO tariffDAO = new TariffDAO();

List<Tariff> tariffList = tariffDAO.getTariffList();

for (Tariff t : tariffList) {
   out.write(t);
}

Better yet, pass it as an attribute to the request, as in the link above, and already display it in jsp.
servlet
JSP
<c:forEach items="${tariffList}" var="tariff"> 
    <p>${tariff.name}</p>
    <p>${tariff.value}</p>
    ...
</c:forEach>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question