• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Homepage contains data fetch by database

 
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try after removing spaces in "All Comments".
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Piyush, removing the spaces solve the exception, but comments are not fetched up in homepage
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Please make sure to use doGet as this is method that gets called when you directly access an URL.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kunal,

Why are you using doPost? Please use doGet.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul Clapham. Any listener helpful in this case?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I posted earlier. The problem has been solved.

Thanks Bear Bibeault, William.
 
reply
    Bookmark Topic Watch Topic
  • New Topic