Results 1 to 12 of 12
  1. #1
    linking90 is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default Problem retrieving data to JSP from Bean [using Hibernate]

    Hi there, Any responses or ideas will be very helpful.

    I can create the Java object and the corresponding database table using Hibernate JPA [without Spring]. I cant seem to get, how to display the data from the bean every time the bean is refreshed into the JSP page. Say, someone updates the bean from another machine on the local network.The JSP page doesn't update when refreshed on my local machine.
    [The bean is populated from the data, from the servlet which get the data from the html page ].


    This is the html page :
    Java Code:
    <HTML>
    <BODY>
    <FORM METHOD=POST ACTION="/TestHibernateApp/Login">
    PollInterval <INPUT TYPE=TEXT NAME=PollInterval value=4 SIZE=20><BR>
    activeAcceptBtn <INPUT TYPE=TEXT NAME=activeAcceptBtn value="test1" SIZE=20><BR>
    
    activeAutoConnect <INPUT TYPE=TEXT NAME=activeAutoConnect value=1  SIZE=4><BR>
    
    activeBg <INPUT TYPE=TEXT NAME=activeBg value="test2" SIZE=4><BR>
    activeDeclineBtn <INPUT TYPE=TEXT NAME=activeDeclineBtn value="test3" SIZE=4><BR>
    activeMsg <INPUT TYPE=TEXT NAME=activeMsg value="test4" SIZE=4><BR>
    connectedEndBtn <INPUT TYPE=TEXT NAME=connectedEndBtn value="test5" SIZE=4><BR>
    connectedImStatus <INPUT TYPE=TEXT NAME=connectedImStatus value=1 value="test6" SIZE=4><BR>
    connectedMsg <INPUT TYPE=TEXT NAME=connectedMsg value="test7" SIZE=4><BR>
    connectedVoiceBg <INPUT TYPE=TEXT NAME=connectedVoiceBg value="test8" SIZE=4><BR>
    connectedMsg <INPUT TYPE=TEXT NAME=connectedMsg value="test9" SIZE=4><BR>
    standbyBg <INPUT TYPE=TEXT NAME=standbyBg  value="test10" SIZE=4><BR>
    standbyMsg <INPUT TYPE=TEXT NAME=standbyMsg value="test11" SIZE=4><BR>
    <P><INPUT TYPE=SUBMIT>
    </FORM>
    </BODY>
    </HTML>
    This is the snippet for java bean :
    Java Code:
    @Entity
    @Table(name="chatter")
    public class ChatterBean {
    	.
                 .
    	.
      }
    This is the Login servlet page

    Java Code:
    package servlets;
    
    import java.io.IOException;
    
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;
    import org.hibernate.tool.hbm2ddl.SchemaExport;
    
    
    import beans.ChatterBean;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;
    import org.hibernate.tool.hbm2ddl.SchemaExport;
    
    
    import beans.ChatterBean;
    
     public class LoginServlet extends HttpServlet implements Servlet {
    	 
       /** DoesSomething */
       static final long serialVersionUID = 1L;   
       
       /** Newly initiated ChatterBean*/
       private ChatterBean cb;
       
     
       private String a;
       
       private String b;
    
       private boolean c;
       
       private String d;
       
       private String e;
       
       private String f;
       
       private String g;
       
       private boolean h;
       
       private String i;
       
       private String j;
       
       private String k;
       
       private String l;
      
       private String m;
      
    
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		this.doPost(request, response);
    	}  	
    
    
    	
    	/** 
    	 * 
    	 * <br>3. populate bean, create session  and log user into system
    	 * <br>5. send user to appropriate page
    	 * 
    	 */
    //	@SuppressWarnings("null")
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
    
    		a =request.getParameter("PollInterval");
    		b = request.getParameter("activeAcceptBtn");
    		c = request.getParameter("activeAutoConnect") != null;
    		d = request.getParameter("activeBg");
    		e = request.getParameter("activeDeclineBtn");
    		f = request.getParameter("activeMsg");
    		g = request.getParameter("connectedEndBtn");
    		h = request.getParameter("connectedImStatus") != null;
    		i = request.getParameter("connectedMsg");
    		j = request.getParameter("connectedVoiceBg");
    		k = request.getParameter("connectedMsg");
    		l = request.getParameter("standbyBg");
    		m = request.getParameter("standbyMsg");
    		  
    		AnnotationConfiguration config = new AnnotationConfiguration();
       		config.addAnnotatedClass(ChatterBean.class);
       	    config.configure("hibernate.cfg.xml");   //reads the configuration file
    
       	   // new SchemaExport(config).create(true, true); //log,execute to database
              
       	    SessionFactory factory = config.buildSessionFactory();
       	    Session session = factory.getCurrentSession();
       	    
       	    session.beginTransaction(); 
       	         	
       		cb = new ChatterBean();
    
    
       		
       		cb.setPollInterval(a);
       		cb.setActiveAcceptBtn(b);
       		cb.setActiveAutoConnect(c);
       		cb.setActiveBg(d);
       		cb.setActiveDeclineBtn(e);
       		cb.setActiveMsg(e);
       		cb.setConnectedEndBtn(f);
       		cb.setConnectedEndBtn(g);
       		cb.setConnectedImStatus(h);
       		cb.setConnectedMsg(i);
       		cb.setConnectedVoiceBg(j);
       		cb.setConnectedMsg(k);
       		cb.setStandbyBg(l);
       		cb.setStandbyMsg(m);
       		
       		session.save(cb);
       		session.getTransaction().commit();
       		
       		/** create session  */			
    			
       		request.getSession().setAttribute("chatter", cb);
            
       		
       		
    		response.sendRedirect("/TestHibernateApp/getp.jsp");
    
    			
    			
           }
    	}
    This is the JSP display page for the bean data which is not working in case the bean is refreshed.

    Java Code:
    <%@ page
    import="beans.ChatterBean"
    %>
    <% 
    ChatterBean cb =(ChatterBean)session.getAttribute("chatter");
    %>
    <HTML>
    <HEAD>
    <meta http-equiv="refresh" content="1"></HEAD>
    <BODY>
    You entered<BR>
    
    PollInterval : <%=cb.getPollInterval() %> <BR>
    activeAcceptBtn : <%=cb.getActiveAcceptBtn() %> <BR>
    activeAutoConnect : <%=cb.getActiveAutoConnect() %> <BR>
    activeBg : <%= cb.getActiveBg() %> <BR>
    activeDeclineBtn : <%=cb.getActiveDeclineBtn() %> <BR>
    activeMsg : <%=cb.getActiveMsg() %> <BR>
    connectedEndBtn : <%=cb.getConnectedEndBtn() %>
    connectedImStatus : <%=cb.getConnectedImStatus() %> <BR>
    connectedMsg : <%=cb.getConnectedMsg() %> <BR>
    connectedVoiceBg : <%=cb.getConnectedVoiceBg() %> <BR>
    connectedMsg : <%=cb.getConnectedMsg() %> <BR>
    standbyBg : <%=cb.getStandbyBg() %> <BR>
    standbyMsg : <%=cb.getStandbyMsg() %>
    </BODY>
    </HTML>
    Thank you for your responses
    Last edited by linking90; 09-09-2010 at 08:09 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    What is it you're expecting to happen?

    At the moment the it looks like the user submits the form, which goes to the login servlet, which creates the ChatterBean, and redirects (why not forward?) to the display jsp. And that's it. There's nothing in the HTML for it to poll for updates or anything, if that's what you are expecting.

    Also, some style notes...
    1. Those are rubbish (and I mean really rubbish) variable names. "a", "b", etc...use descriptive names like "pollInterval".
    2. Do not give your servlet any attributes! There are almost no situations when you need to, and you leave yourself wide open to concurrency issues. Tomcat (or whatever servlet container you are running this on) generally uses a single instance of a servlet which is shared amongst several threads (ie sessions), so any attributes will likely be overwritten if more than one person accesses the site at the same time.
    3. Get your hibernate code out of the servlet. The servlet should be simply about getting the data from the request and passing it off to some other code to do the work on it. Then it forwards based on the result returned from that.
    4. As mentioned up top, why are you redirecting to the JSP and not fowarding?

  3. #3
    linking90 is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default

    Thanks for your response.

    There's nothing in the HTML for it to poll for updates or anything, if that's what you are expecting.
    I'm trying to access the same HTML form page on different machines and have the JSP page update according to the new HTML form data sent from the respective machine.

    Lets say: I access the HTML page on
    http://localhost:8080/TestHibernateApp/index.html submit the form and get the JSP page which refreshes every second.

    Now I go to a different machine on my local network
    http://192.168.0.7:8080/TestHibernateApp/index.html
    submit the form and I get the JSP page according to the data submitted in the form. However in my localshost the JSP page is not updated.

    So in other words, every time the bean is updated the JSP should be updated. This is the part which I cant seem to get working.

    Thanks once again !!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Ah, I missed the refresh!
    :)

    Which probably explains the redirect?

    OK, do you know if, on the second (no-local) URL, it is refreshing at all?

  5. #5
    linking90 is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default

    The display page [JSP] using the second (no-local) URL, doesn't refresh the data. It will only show the data in entered in the HTML form once.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    So the refresh isn't happening?

    If that's the case then that's a browser thing.

  7. #7
    linking90 is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default

    Oh sorry, I meant the page is refreshing every second but the data doesn't change

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    So (and sorry if I'm being dead slow on the uptake here), when run via a browser on the same machine you get the refresh and the data is updated. When the browser is on a different machine it refreshes, but the data doesn't change?

    I'm curious as to how the data changes in the Bean?

    Can you debug it at all? I suspect not since the refresh is on the JSP, and they can be a sod to debug.

  9. #9
    linking90 is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default

    No problem but thank you for your quick responses.
    when run via a browser on the same machine you get the refresh and the data is updated. When the browser is on a different machine it refreshes, but the data doesn't change?
    Correct in the sense that the JSP page wil update/refresh using the second URL but the JSP displayed using the first URL [i.e. localhost] doesn't update[The page refreshes though not the data].

    I'm curious as to how the data changes in the Bean?

    On clicking the submit button of the HTML form page, the Login servlet updates / gets the values from the form and updates the bean using the set methods [ex: cb.setPollInterval(a)...etc ]. The login servlet code is in my first message on this thread.


    Can you debug it at all?
    Correct
    Last edited by linking90; 09-09-2010 at 06:31 PM.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Hang on.

    You have two windows open...one in which you are doing the login, changing the bean values, and the other on which you have the refresh going on?

    When running locally are both windows on the same machine, and are both the same breed of browser?

    Are both windows when testing on a different machine on the same machine?

  11. #11
    linking90 is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post

    You have two windows open...one in which you are doing the login, changing the bean values, and the other on which you have the refresh going on?
    Correct

    When running locally are both windows on the same machine, and are both the same breed of browser?
    Yes

    Are both windows when testing on a different machine on the same machine?
    I'm using 2 laptops to test which are connected to the same router. So 2 jsp pages one on each laptop with 2 logins one on each laptop.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    You have 2 sessions then in the second case.

    In the first case (which works) you have one session which shares the bean.

    In the second case you have two different beans, and only the one accessed by the laptop doing the changing is, well, changing. The one accessed by the refresh JSP is not.

    Your best bet is to refresh against a servlet that rereads the bean out of the database. Or something along those lines. Off now, so hopefully that'll help you get where you need to go.

Similar Threads

  1. Replies: 3
    Last Post: 06-02-2010, 07:58 PM
  2. (Java newbie) retrieving data from a web site
    By idum in forum Networking
    Replies: 4
    Last Post: 10-12-2009, 08:37 PM
  3. Store data is not retrieving
    By JavaJunkie in forum CLDC and MIDP
    Replies: 2
    Last Post: 05-30-2009, 09:08 AM
  4. error while retrieving data from data base
    By kirtesh4u in forum New To Java
    Replies: 5
    Last Post: 11-15-2008, 05:10 PM
  5. Retrieving data from the DB
    By yuchuang in forum JDBC
    Replies: 2
    Last Post: 11-27-2007, 09:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •