Quantcast
Channel: Active questions tagged servlets - Stack Overflow
Viewing all articles
Browse latest Browse all 721

JSP not showing data passed by JAVA Servlet

$
0
0

I am just a fellow newbie practicing Hibernate integration with JSP and Servlet. The problem arose when I tried to fetch data from the database.

As usual I have the entity class with all the getters and setters. Data is being input from another JSP page, posts through another servlet and gets stored into database successfully. But while retrieval into another JSP page through another servlet I don't get any output. The data is to be shown in a generic "card" block from bootstrap. Here is the JSP code, with title and content now left empty. Part of the problem being also how do I get my getters [getTitle() and getContent()] from the entity class to extract the data from the object, I am still learning so I have so many confusions.

<%@ page contentType="text/html;charset=UTF-8" language="java" %><!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>All Notes</title><%@include file="all_js_css.jsp" %></head><body><div class="container-fluid"><%@include file="navbar.jsp" %><br><h1 class="text-uppercase">All Notes:</h1><br><c:forEach items="${notes}" var="note"><div class="card" style="width: 18rem;"><div class="card-body"><h5 class="card-title"><c:out value="${note.??}" /></h5><p class="card-text"><c:out value="${note.??}" /></p><a href="#" class="btn btn-primary">Go</a></div></div></c:forEach></div></body></html>

I have tried note.title, note.gettitle, note.getTitle(). Nothing worked. It just shows blank where the text was supposed to be. The servlet code is this:

package com.servlets;import java.io.IOException;import java.util.List;import org.hibernate.Session;import org.hibernate.query.Query;import com.entities.note;import com.helper.factoryProvider;import jakarta.servlet.ServletException;import jakarta.servlet.annotation.WebServlet;import jakarta.servlet.http.HttpServlet;import jakarta.servlet.http.HttpServletRequest;import jakarta.servlet.http.HttpServletResponse;@WebServlet(urlPatterns = "/ShowNoteServlet")public class ShowNoteServlet extends HttpServlet {    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        System.out.println("hello");        try {            Session session = factoryProvider.getFactory().openSession();            Query query = session.createQuery("from notes");            List<note> list = query.list();            System.out.println(list);            req.setAttribute("notes", list);            req.getRequestDispatcher("all_notes.jsp").forward(req, resp);            session.close();        } catch (Exception e) {            e.printStackTrace();        }    }}

I placed those 2 absurd println() to check whether the servlet code is even running or not, and yes even though tomcat was running the latest built war file, I would did not get any output in the console as well.

Right now the cards look like this. There was supposed to have multiple cards based on multiple entry rows in the table. For reference, this is my entity code and my directory structure:

package com.entities;import java.util.Date;import java.util.Random;import jakarta.persistence.Entity;import jakarta.persistence.Id;import jakarta.persistence.Table;@Entity@Table(name = "notes")public class note {    @Id    private int id;    private String title;    private String content;    private Date addedDate;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getContent() {        return content;    }    public void setContent(String content) {        this.content = content;    }    public Date getAddedDate() {        return addedDate;    }    public void setAddedDate(Date addedDate) {        this.addedDate = addedDate;    }    public note(String title, String content, Date addedDate) {        super();        this.id = new Random().nextInt(100000);        this.title = title;        this.content = content;        this.addedDate = addedDate;    }    public note() {        super();    }}

I would be much happy if you could point out where I am doing wrong and how could I fix it. I saw people writing the whole query thing as scriptlet but it looks untidy and not modular. Is that the only way? Or are there any other better and clean way to do this?


Viewing all articles
Browse latest Browse all 721

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>