File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Homepage contains data fetch by database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Homepage contains data fetch by database" Watch "Homepage contains data fetch by database" New topic
Author

Homepage contains data fetch by database

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
As soon as my homepage(1st page)opens,it should contain some data fetch by database. How should i do it? Should i go for any listener? I am using hibernate for the db connection.


kunal
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

What is your home page? .jsp,.html,.xhtml,.jsf etc?

If jsp then apply a standard MVC pattern, have the home page invoke a servlet, load up the model
and forward to the view to render the data.

WP
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Its a .jsp file.

To should should i forward the request?

Here is what i did.

Web.xml



HomePage.jsp




CommentsServlet.java



CommentsDao.java






Error---

Apr 12, 2012 9:56:17 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: /HomePage.jsp(26,2) "${All Comments}" contains invalid expression(s): javax.el.ELException: Error Parsing: ${All Comments}
at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:198)
at org.apache.jasper.compiler.Validator$ValidateVisitor.checkXmlAttributes(Validator.java:1149)
at org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:819)
at org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1507)
at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2336)
at org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2386)
at org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2392)
at org.apache.jasper.compiler.Node$Root.accept(Node.java:489)
at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2336)
at org.apache.jasper.compiler.Validator.validate(Validator.java:1737)
at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:178)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:306)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

Since you are not passing or require any parameters, jsp:useBean would solve your problem.

http://www.exampledepot.com/egs/javax.servlet.jsp/usebean.jsp.html

Create a bean say, CommentData.java and when it instantiates, load up the comments for rendering in the jsp.


Please let us know if this works for you ...

WP
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
Try after removing spaces in "All Comments".
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Piyush, removing the spaces solve the exception, but comments are not fetched up in homepage
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
Hi, Did you also remove the space in your servlet code as well?

req.setAttribute("All Comments", com);


Please also make sure that has some data in it.

Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196


I can see you have started the transaction but where are you doing a commit?

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
William, that doesn't solves the problem. I need some code that runs even before the homepage runs. Something as similar to ServletContextListeners which runs before the servlet can service the client
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60739
    
  65

Your welcome file should not be the JSP. It should be the URL of the servlet controller for the JSP.

I do not recommend following the deprecated Model 1 approach with useBean.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
If you want to show comment on your HomePage.jsp, then you should call a servlet, get data from Model , set data in request attribute , dispatch the request to HomePage.jsp which will then be able to show comment.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60739
    
  65

Exactly, as I said. No JSP should be able to be directly addressed without going through its controller -- including any "home page".

In fact, I recommend placing all your JSPs in a folder such as WEB-INF/views so that they cannot be mistakably served up without a controller.
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196


Please make sure to use doGet as this is method that gets called when you directly access an URL.
Vanessa Danin
Ranch Hand

Joined: Oct 04, 2004
Posts: 34
Kunal, if your homepage should contain the comments as soon as it loads, you should
change your web.xml to
<servlet>
<servlet-name>Comm</servlet-name>
<servlet-class>org.nit.servlet.CommentsServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

and possibly place the call to your commentsList method in the Comm servlet's init method.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
I applied the following changes

Web.xml


CommentServlet


Removed <from> tag from HomePage.jsp

But still not getting any Comment.

It seems control doesn't goes to CommentServlet, as the println() line doesn't runs
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
Vanessa Danin wrote:Kunal, if your homepage should contain the comments as soon as it loads, you should
change your web.xml to
<servlet>
<servlet-name>Comm</servlet-name>
<servlet-class>org.nit.servlet.CommentsServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

and possibly place the call to your commentsList method in the Comm servlet's init method.



It will not work if you directly access HomePage.jsp. You will have to go through servlet to show comments on HomePage.jsp.
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
Hi Kunal,

Why are you using doPost? Please use doGet.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Piyush, its works. Great.
Vanessa Danin, load on startup doesn't works. Just wonder, why?
Thanks Bear Bibeault, Piyush, Vanessa Danin,William P O'Sullivan.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Kunal Lakhani wrote:Vanessa Danin, load on startup doesn't works. Just wonder, why?


That's because "load on startup" does just what it says... it loads the servlet when the application starts. It makes no difference to your code whether the servlet is loaded at the time of the request or whether it is loaded 30 minutes earlier, so configuring it to load on startup won't make any difference to your code.

The only reason to use the load-on-startup feature is if the servlet is doing something which causes it to take a long time to initialize, such as setting up large quantities of data in a cache. And servlets shouldn't be doing that sort of thing any more, it should be done by listeners instead. So not only is load-on-startup unhelpful for your problem, it has been obsolete since listeners were brought into the servlet spec. Which was a very long time ago.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks Paul Clapham. Any listener helpful in this case?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60739
    
  65

Kunal Lakhani wrote:Thanks Paul Clapham. Any listener helpful in this case?

No. You've already been give then answer, why aren't you busy acting on it?
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

Because I can ... ;)

JSP:


Add, run this and you will see that the constructor is called each time.
This approach would allow your comments to change dynamically as well.

May not be the most efficient but a very easy and straightforward way to load startup pages etc..

WP
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
I posted earlier. The problem has been solved.

Thanks Bear Bibeault, William.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Homepage contains data fetch by database
 
Similar Threads
StaleConnection Exception Websphere 4.0
Hibernate with RSS Feeds.
How to assign js variable to jsp variable
how to refresh data after delete in jsp
Populating Drop Down From Database