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>
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.