File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes JSP Bean,not able to work my first bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP Bean,not able to work my first bean" Watch "JSP Bean,not able to work my first bean" New topic
Author

JSP Bean,not able to work my first bean

Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Posts: 87
I have created a webproject in which i have one jsp firstView.jsp captures details about the person and relays the information to the second jsp viewInformation.jsp.
The source code is as below:
FirstView.jsp




viewInformation.jsp





The bean i have used is under package viewBean, which I have kept under src/viewBean folder. The output folder for the class files is under the lib folder.
The bean class is as under




However whenever i hit the firstview.jsp i get an exception:

org.apache.jasper.JasperException: /pages/firstView.jsp(5,0) The value for the useBean class attribute viewBean.Person is invalid.
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:148)
at org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1272)
at org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1178)
at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
at org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2411)
at org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2417)
at org.apache.jasper.compiler.Node$Root.accept(Node.java:495)
at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
at org.apache.jasper.compiler.Generator.generate(Generator.java:3426)
at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:216)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:332)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:312)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:299)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
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:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)



Please help. I am stuck.
is there some place else where i need to keep the bean class file. or have i used the bean correctly.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

Most likely cause: class file is in the wrong place.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

Other comments:
  • What is the purpose of the useBean in the first JSP?
  • Never make your bean instance variables public.
  • Always follow conventions. Package names should be lowercase.
  • As you are not using scriptlets (good!), the import is unnecessary.
  • Why getProperty in place of the EL?
  • Stanley Walker
    Ranch Hand

    Joined: Sep 23, 2009
    Posts: 87
    Hi Bear,

    could you please tell me where i should put the bean class file. I have put it under the /WEB-INF/lib folder as well as WEB-INF/classes folder.

    1.I didnt know that import was unneccessary for beans. Thanks for letting me know.
    2. I wanted to use EL but not before i get a hang of beans.
    3. This is kind of important. May be i am getting the concept and usage of beans wrong. My firstView.jsp will have viewInformation.jsp as its target. I was under the impression that the viewBean.Person class fields will be instantiated with the values from the firstView.jsp(hence the setProperty) and then will be used to show it in viewInformation.jsp.
    I have used the scope of request, hence.
    Is that possible?
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61658
        
      67

    WEB-INF/lib is for jars, not classes.

    The class file should be at: WEB-INF/classes/viewBean/Person.class

    (As mentioned, you should fix the package name.)
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61658
        
      67

    Stanley Walker wrote:1.I didnt know that import was unneccessary for beans. Thanks for letting me know.

    imports are only necessary for scriptlets. Don't even go there.

    2. I wanted to use EL but not before i get a hang of beans.

    EL is part of using beans. getProperty and setProperty, while not officially deprecated, are dinosaurs.

    3. This is kind of important. May be i am getting the concept and usage of beans wrong.

    Actually, it's really the concept and usage of JSP you're getting wrong. With very few exceptions, a JSP should be never be the target of a submit. Submits should be handled by servlets.

    In this case, where you are just displaying results, it's not too big a deal. but as soon as any type of real processing enters the picture, it becomes a very very big deal.

    My firstView.jsp will have viewInformation.jsp as its target. I was under the impression that the viewBean.Person class fields will be instantiated with the values from the firstView.jsp(hence the setProperty) and then will be used to show it in viewInformation.jsp.

    No. The bean in the first JSP is created for no reason. The bean in the second JSP is what should capture the submission. (Assumes antiquated constructs -- as I said, submitting to a JSP is no longer considered a best practice.)
    Stanley Walker
    Ranch Hand

    Joined: Sep 23, 2009
    Posts: 87
    Thanks for the reply Bear.

    So i will submit my page to a servlet. Instantiate the Person bean with fields entered in the firstView.jsp using request.getParameter. Then i will place the person object in request. in the second jsp, the values will be populated hence.

    Am i right in my assessment above??

    Also, please do tell me where i should place the class file for the bean.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61658
        
      67

    Stanley Walker wrote:So i will submit my page to a servlet. Instantiate the Person bean with fields entered in the firstView.jsp using request.getParameter. Then i will place the person object in request. in the second jsp, the values will be populated hence.
    Yes -- assumes a forward (not redirect) from the servlet to the JSP. You might want to read this article.

    You might also want to check out the Jakarta project BeanUtil.populate() method to help populate the bean easily.

    Also, please do tell me where i should place the class file for the bean.
    Already did. See above.
    Stanley Walker
    Ranch Hand

    Joined: Sep 23, 2009
    Posts: 87
    Thank you bear, i have finally made my java beans work.

    I have one final question. i have a jsp, secondView.jsp which sets properties of the bean and then submits it to beanServlet. if a bean is set in request scope , then why do i get null when i do request,getAttribute("beanId") in the servlet??

    my source code is as below.


    secondView.jsp




    and the beanServlet is as under


    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61658
        
      67

    Stanley Walker wrote:I have one final question. i have a jsp, secondView.jsp which sets properties of the bean and then submits it to beanServlet.
    That sounds odd. What do you mean by this. A JSP shouldn't be sending a bean to a servlet, it should be the other way around.

    if a bean is set in request scope , then why do i get null when i do request,getAttribute("beanId") in the servlet??
    Most likely because they are operating in different request scopes.

    What's the sequence of events that occurs. It should be something like:
  • The user fills in a form.
  • The form is submitted to a servlet (task controller).
  • The servlet does whatever its supposed to do with the submitted data.
  • The servlet redirects to the page controller (another servlet) for the page to be displayed.
  • The page controller places whatever data the JSP will need in request scope and forwards to the JSP.


  • This implements the PRG pattern which is considered web app best practice. Does this match what you are doing?

    This article describes the reasons behind all this.
    Stanley Walker
    Ranch Hand

    Joined: Sep 23, 2009
    Posts: 87
    yes i did do that and it worked fine, thanks to you.

    i was trying a different approach as i mentioned in the previous post.
    what i do not understand is why would you have a setProperty tag. my understanding was you have a setProperty tag was present so that the bean can be instantiated from the jsp itself ( kind of like jsf and backing bean).

    my understanding(for the second approach was)

    1. user fills in a form.
    2. using setProperty i instantiate the bean.
    3. control flows to the task controller.
    4. in the task controller i extract the bean from the request scope, may be do some processing to display my final output , the final jsp i forward the request to.

    Apparently i am wrong in my above assessment.
    Please help. I am almost sure i got beans figured out except for the setProperty tag.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61658
        
      67

    what i do not understand is why would you have a setProperty tag.
    setProperty is rather antiquated. Firstly, it was more important in the days when people used JSPs for processing. Secondly, it's been side-lined by the JSTL's <c:set>.

    Remember, just because you can do something, doesn't mean that you should.

    my understanding was you have a setProperty tag was present so that the bean can be instantiated from the jsp itself ( kind of like jsf and backing bean).
    That's a remnant from the old Model 1 days. Today, most beans should be instantiated and filled in by the page controller. I still instantiate beans in some JSPs -- but not for data processing. They're usually what I call "display support beans" whose job is merely to help the JSP manage its display.

    my understanding(for the second approach was)
    Again, that's Model 1. Deprecated. Old-fashioned. Discredited.

    Apparently i am wrong in my above assessment.
    I wouldn't say "wrong", I'd say "heading down the garden path".

    Please help. I am almost sure i got beans figured out except for the setProperty tag.
    I cannot remember the last time I used setProperty. It's good to understand what it does in case you come across a usage. But it's not a big player anymore in the face of <c:set> and Model 2.
    Stanley Walker
    Ranch Hand

    Joined: Sep 23, 2009
    Posts: 87
    Thanks Bear, for volunteering your time.
    Tough love works for me!

    Anyways, i'll remmebr what you said.
    I'll be trying to pick up EL and JSTLs now. Dont be bored if i post some more of my questions.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61658
        
      67

    Stanley Walker wrote:Thanks Bear, for volunteering your time.
    Tough love works for me!

    I actually removed the "tough love" part of the message because upon re-reading came across harsher than I intended.

    I'll be trying to pick up EL and JSTLs now. Dont be bored if i post some more of my questions.
    Of course not! That's what we're here for!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: JSP Bean,not able to work my first bean