Answer the question
In order to leave comments, you need to log in
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");
}
}
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());
}
}
Answer the question
In order to leave comments, you need to log in
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());
}
TariffDAO tariffDAO = new TariffDAO();
List<Tariff> tariffList = tariffDAO.getTariffList();
for (Tariff t : tariffList) {
out.write(t);
}
<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 questionAsk a Question
731 491 924 answers to any question