Muni123 0 Newbie Poster

I am trying to add some text in to a page by using a textarea where text is entered the text gets saved in to a database after clicking on a add button on a page. However, I am unable to achieve this after trying. I have two tables matchreportcomments and fixtures
matchreportcomments is where the match reports are supposed to be saved and it has fields id fixtureId as foreign key fixtureId and report where match reports are saved and fixtures table has primary key id . A list of all the classes that are relavent to this are listed. And sorry for the long code.

Here's the class that maps to the matchreportscomments table

package com.webapp.entity;

import java.io.Serializable;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name = "matchreportcomment")
public class MacthReportCommentsEntity  implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
     @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id ;

    @Column(name = "report")
    private String report;

    @Column(name = "fixtureId")
    private Long fixtureId;


    @JoinColumn(name = "fixtureId", insertable=false,updatable=false)
    @ManyToOne
    private FixturesEntity fixturesEntity;


    public Long getId() {
        return id;
    }


    public void setId(Long id) {
        this.id = id;
    }


    public String getReports() {
        return report;
    }


    public void setReports(String report) {
        this.report = report;
    }


    public Long getFixtureId() {
        return fixtureId;
    }


    public void setFixtureId(Long fixtureId) {
        this.fixtureId = fixtureId;
    }


    public FixturesEntity getFixturesEntity() {
        return fixturesEntity;
    }


    public void setFixturesEntity(FixturesEntity fixturesEntity) {
        this.fixturesEntity = fixturesEntity;
    }


}

Here's is the servlet which I use to save the texts using hibernate

    package com.webapp.servlets;

    import java.io.IOException;
    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 org.hibernate.Session;
    import com.webapp.entity.MacthReportCommentsEntity;
    import com.webapp.util.HibernateUtil;

    /**
     * Servlet implementation class MatchReportsServlet
     */
    @WebServlet("/MatchReportsServlet")
    public class MatchReportsServlet extends HttpServlet 
    {
        private static final long serialVersionUID = 1L;

        /**
         * @see HttpServlet#HttpServlet()
         */
        public MatchReportsServlet() {
            super();
            // TODO Auto-generated constructor stub
        }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doProcess(request, response);
        }

        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doProcess(request, response);
        }
        private void doProcess(HttpServletRequest request, HttpServletResponse response) 
        {


            String Match = request.getParameter("matchreportcomment");
            Long fixtureId = Long.parseLong(request.getParameter("fixtureId"));

            String type = request.getParameter("type");

            MacthReportCommentsEntity webEntity = new MacthReportCommentsEntity();

            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();

            if(type.equalsIgnoreCase("add")) 
            {


                webEntity.setReports(Match);
                webEntity.setFixtureId(fixtureId);

                session.save(webEntity);
            }
            session.getTransaction().commit();
            try 
            {
                response.sendRedirect("jsp/displayMatchReportsComments.jsp?fixtureId="+fixtureId);
            } 
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

        }

And here's the jsp page part where the displaying of the report takes place

<%@page import="com.webapp.entity.MacthReportCommentsEntity"%>
<%@page import="com.webapp.entity.FixturesCommentsEntity"%>
<%@page import="com.webapp.entity.WebResourcesEntity"%>
<%@page import="com.webapp.entity.ScoresEntity"%>
<%@page import="com.webapp.entity.FixturesEntity"%>
<%@page import="com.webapp.util.HibernateUtil"%>
<%@page import="com.webapp.beans.UserBean"%>

<%
    UserBean userBean = (UserBean) session.getAttribute("userSession");
    if (userBean == null) {
%>
<jsp:forward page="login.jsp"/>
<%            }
%>

<%@page import="java.util.Collections"%>
<%@page import="java.util.List"%>
<%@page import="org.hibernate.Session"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<script type="text/javascript" src="/WebApp/js/jquery-1.9.1.js"></script>
<jsp:include page="Header.jsp"></jsp:include>
    <html xmlns="http://www.w3.org/1999/xhtml">
        <!-- InstanceBegin template="/Templates/main.dwt" codeOutsideHTMLIsLocked="false" -->
        <head>
            <meta http-equiv="Content-Type"
                  content="text/html; charset=iso-8859-1" />
            <!-- InstanceBeginEditable name="doctitle" -->
            <title> display fixtures</title>
            <!-- InstanceEndEditable -->
            <link href="/WebApp/style/middle.css" rel="stylesheet" type="text/css" />
            <link href="/WebApp/style/menushowhide1.css" rel="stylesheet" type="text/css" />
            <link rel="stylesheet" type="text/css" href="/WebApp/style/boxes.css"
                  media="all" />
            <link rel="stylesheet" type="text/css" href="/WebApp/style/menu-topbar.css" />
            <link rel="stylesheet" type="text/css" href="/WebApp/style/menu-base.css" />

            <script type="text/javascript" src="/WebApp/js/selectAll.js"></script>
            <script>
  $(function() {

     /*  $('.displaytagtd a').addClass('buttonash'); */
  });


</script>


<script type="text/javascript">
            function vis(id) {
                document.getElementById(id).style.display = 'inline';
            }
            function invis(id) {
                document.getElementById(id).style.display = 'none';
            }
            function submit() {
                if(validateForm()) {
                document.addCustomer.submit();
                }
            }
            function resetForm() {
                document.addCustomer.reset();
            }

            function validateForm()
            {
            var x=document.forms["addCustomer"]["fixtureComments"].value;
            if (x==null || x=="")
              {
              alert("Comments must be filled out");
              return false;
              }


            return true;
            }


        </script>
        </head>
        <body>


            <br />
            <br />
            <table width="" border="0" align="center" cellpadding="0"
                   cellspacing="0" class="maintable">
                <tr>
                    <td>
                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                <td
                                    style="background: url(/WebApp/images/leftbg.gif) repeat-y; width: 8px;"></td>
                                <!-- InstanceBeginEditable name="EditRegion2" -->
                                <td class="middlesection">
                                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                        <tr>
                                            <td align="left" valign="middle">

                                            </td>
                                        </tr>
                                        <tr>
                                            <td class="p10">
                                                <table width="100%" border="0" cellspacing="0"
                                                       cellpadding="0">
                                                    <tr>
                                                        <td>
                                                            <table width="100%" border="0" cellspacing="0"
                                                                   cellpadding="0">
                                                                <tr>
                                                                    <td align="left" valign="middle">
                                                                        <h1>
                                                                            Fixtures
                                                                        </h1>
                                                                    </td>
                                                                    <td align="right" valign="bottom">
                                                                    <table width="0%" border="0" cellspacing="0"
                                                                   cellpadding="0">
                                                                <tr>
                                                                    <td align="center" style="color: #00FF00">
                                                                        &nbsp;
                                                                    </td>
                                                                    <td>

                                                                    </td>
                                                                    <td width="5">
                                                                        <a href="#" class="buttonash" onclick="submit()"><span>
                                                                                Add </span> </a>
                                                                    </td>

                                                                </tr>
                                                            </table>

                                                                    </td>
                                                                </tr>
                                                            </table>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td class="bottomboder">
                                                            <img src="/WebApp/images/ashish.gif" height="1" />
                                                        </td>
                                                    </tr>
                                                </table>
                                            </td>
                                        </tr>

                                        <tr>
                                            <td class="p10">
                                                <table width="100%" border="0" cellspacing="0"
                                                       cellpadding="0">
                                                    <tr>
                                                        <td>
                                                            <table width="100%" border="0" cellspacing="0"
                                                                   cellpadding="0">
                                                                <tr>
                                                                    <td class="switcher">
                                                                        <label for="fixture_switcher"></label>

                                                                        <br></br>
                                                                    </td>
                                                                </tr>
                                                            </table>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            <table width="100%" border="0" cellspacing="0"
                                                                   cellpadding="0">
                                                                <tr>
                                                                    <td valign="top">

                                                                    </td>
                                                                    <td style="background: url(images/bghd.gif) repeat-x;">
                                                                        <table class="actions" cellspacing="0">
                                                                            <tbody>
                                                                                <tr>
                                                                                    <td>

                                                                                    </td>
                                                                                    <td align="right">
                                                                                        <table width="0" border="0" cellspacing="0"
                                                                                               cellpadding="2">
                                                                                        </table>
                                                                                    </td>
                                                                                </tr>
                                                                            </tbody>
                                                                        </table>
                                                                    </td>
                                                                    <td valign="top">

                                                                    </td>
                                                                </tr>
                                                            </table>
                                                        </td>
                                                    </tr>
                                                </table>
                                            </td>
                                        </tr>

                                        <tr>
                                            <td class="p10">
                                                <div style="width: 960px; overflow: auto;">
                                                    <center>

                                                        <div id="productGrid">
<%


    String fixtureId = request.getParameter("fixtureId");
    Session sessionHB = HibernateUtil.getSessionFactory().getCurrentSession();
    sessionHB.beginTransaction();

    List<MacthReportCommentsEntity> fixtures = sessionHB.createQuery("SELECT a FROM MacthReportCommentsEntity a where a.fixtureId="+fixtureId).list();

    request.setAttribute("fixtures", fixtures);
    sessionHB.getTransaction().commit();

%>

                                                        </div>

                                                        <div class="grid">



                                                            <display:table name="${fixtures}" cellpadding="4px" id="itemName">
                                                            <display:column property="report" title="report"/>                    

                                                            </display:table>
                                                           <br/>
                                                           <br/>
                                                           <form method="get" id="addCustomer" name="addCustomer" action="/WebApp/MatchReportsServlet" >
                                                           <table>
                                                            <tr>
                                                                <td><label>report</label><br/></td>
                                                                <td>
                                                         <textarea rows="10" cols="65" name="matchreportcomment"></textarea>
                                                         <input type="hidden" name="type" value="add"/> 
                                                         <input type="hidden" name="fixtureId" value="<%=fixtureId%>"/>
                                                                </td>
                                                            </tr>
                                                           </table>
                                                           </form>

                                                    </div>

                                                </center>
                                            </div>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                            <!-- InstanceEndEditable -->
                            <td
                                style="background: url(/WebApp/images/rightbg.gif) repeat-y; width: 8px;"></td>
                        </tr>
                    </table>

                </td>
            </tr>
        </table>
    </body>
    <%@include file="footer.jsp" %>
</html>

Dani AI

Generated

Two quick, high‑value things to check first — they’re the most common reasons a saved comment never appears on the page.

  • Bean / display binding mismatch: the view (display tag) is asking for a property name that doesn’t match the JavaBean getter. JavaBeans introspection expects getX() for property x. If the JSP asks for report but the entity’s getter is named for a different property (for example getReports()), the display layer will see a blank value. Fix by making the bean and the display property names match (rename the getter to getReport() or change the display column to the bean’s actual property name).

  • Client-side submission blocked by JS: the form’s client validation is referencing a non‑existent field name. That throws a JavaScript error and prevents the form from being submitted at all. Check the browser console for errors and the Network tab to confirm whether the browser actually sends the request. Also use POST for textarea content (GET can truncate long bodies).

Practical fixes to try immediately

  • Correct the JavaBean getter/property mismatch (make the getter name match the property the display tag expects).
  • Fix the validation function to reference the textarea’s real name, or remove/replace it with server validation.
  • Change the form method to post and confirm the servlet reads request.getParameter(...) safely (null/number parsing checks).
  • Watch server logs during submission for exceptions (NumberFormatException, transaction errors).
  • Verify the DB directly (or enable Hibernate SQL logging) to confirm whether the insert happened.

Other notes
Avoid opening Hibernate sessions inside JSPs — fetch data in the servlet/DAO, set request attributes, then forward to the JSP. That keeps transaction/exception handling in one place and makes debugging far easier.

— start by checking the browser console and then the bean/getter name; fixing those two things usually resolves this exact symptom.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.