Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use an object created in a servlet in JSP using EL. (use object's properties)  RSS feed

 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having an issue figuring out how to use an object that I created in my servlet from data in my database by utilizing it in my JSP page. I am interested in nit picking the properties of the object to use separately throughout the JSP using EL.
I will have another post coming too about a login issue. Not sure if it makes a difference that I am letting you all know here.

For this though, I am looking for solid examples on how to use it. Lets use a PERSON object with a Name, Age, Email attributes. I want to use that PERSON object in my JSP by not printing everything but by only printing the attributes 'separately.

Thank you.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about you post what you have so far, and we'll help you along if you get stuck?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66184
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The basics:
  • Create a scoped variable for the object in request scope via the setAttribute() method.
  • Use the name of the scoped variable in the EL expression; e.g. ${person.name}
  •  
    Jeremy McNally
    Ranch Hand
    Posts: 108
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here is the code. Customer has NAME, EMAIL, USERNAME, PASSWORD. I am only interested in using the name and email. The JSP I am forwarding to is pasted below this.
    As far as ${custmr.name} goes, I tried that and get errors on it. I don't understand why?
    _________________________________________________________________________________________


    ______________________________________________________________________________________________________________

     
    Jeremy McNally
    Ranch Hand
    Posts: 108
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So what you said (2nd post on my topic). Instead of creating it in SESSION Scope I would need to setAttribute(); to the REQUEST Scope?
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66184
    146
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.

    I've gone ahead and added the code tags for you. See how much easier the code is to read?
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66184
    146
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In your code:
    Two points:
  • Use customer rather than custmr. There is no need to lop offf pefectly good characters. This is not Wheel of Fortune -- vowels do not cost money.
  • The names of scoped variables should follow the naming convention of ordinary variables; so "customer", not "Customer".

  • Following conventions makes your code surprisingly easier to read.

    Now, you named the scoped variable Customer (should be customer, see above), so...
    As far as ${custmr.name} goes, I tried that and get errors on it. I don't understand why?

    See the problem?
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66184
    146
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And yes, never use the session when request will do.

    And, why are there scriptlets in your JSP? (Just for a test, I hope.)
     
    Jeremy McNally
    Ranch Hand
    Posts: 108
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't see the problem Bear other than the use of using REQUEST version SESSION SCOPE. If that is where my issue is than I DO understand. If that isn't where my error is coming from than I don't. I understand what you are saying in terms of Java Naming Conventions. That is a good point to make and thank you BUT that wouldn't cause my error. The code is still correct.
    The Request Scope I will change and see what happens. Hopefully that is where the problem lies. Thank you Bear.
     
    Jeremy McNally
    Ranch Hand
    Posts: 108
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    OH AND BEAR: The SCRIPTLET is there to prove that the data is at least in the object I created (is for testing). That is why I want to use EL because I know it is a best practice. I just came back with an error trying to use it which is why I posted this topic. Thank you for your help. IF you see anything else you can give me feedback on I would be greatly appreciative.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66184
    146
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeremy McNally wrote:I don't see the problem Bear other than the use of using REQUEST version SESSION SCOPE.

    You name the scoped variable Customer, and then try to access it using customr. If you name your kid Fred, he ain't gonna come to dinner when you call for Barney.

    That is a good point to make and thank you BUT that wouldn't cause my error.

    In fact, it would. If you had constantly named the variable customer according to convention; not Customer, not custmr, or any other variant, there would be no issue. Conventions matter!

    The code is still correct.

    Nope. See above.
     
    Jeremy McNally
    Ranch Hand
    Posts: 108
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    OH! goodness. Okay you have a very good point. I completely missed that. Thanks for straightening me out on this. So anytime I want to access a property of an Object created (in a servlet) I would only need to do this: <b>Hello $(customer.name) </b><br> Thank you for coming back!?
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66184
    146
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Pretty much, but curly braces, not parens: ${bean.property}

    Assumes scoped variable are beans with proper accessors.

    Also, for any untrusted output that should be escaped to avoid injection attacks, be sure to use <c:out>; otherwise a customer might give themselves a name that contains a script block that contains an Ajax call to delete your database. (The "Bobby Tables" attack.)
     
    Jeremy McNally
    Ranch Hand
    Posts: 108
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    BEAR: Maybe you can help me with ONE more thing. I am having the WORST time being forwarded to my (flightsearch.jsp) IF the validation of the login is correct. IF not, I need it to loop back to (login.jsp).
    It is actually in the LoginServlet code above. I will post the classes that go with it and if you are able and willing to point me in the right direction that would be really helpful. I have been trying different things for 2 days now and nothing. No matter what I do, I either keep being looped back to the login.jsp and it does this (even if login IS valid). If it doesn't do that I am shown a NullPointerException. I simply don't understand. I just got my first job as a Java Developer today and I am trying to brush up before I start work Tuesday.
    I posted the code below for you. I will use the {code} tags you mentioned. I hope it works.

    _____________________________________________________

    You've already seen this, this is the LoginServlet. I tested the validation through another class and using the Main() and it works just fine. The Customer Object is also created as proved by my (Scriptlet) you commented on earlier.


    1.) The Servlets are packaged into the Java Resources directory.
    2.) The JSP's are located under WebContent. I do understand they should be in WEB-INF but I don't think that would cause this issue as it hasn't in another parts of the program.

    Please See Below:



    ____________________________________________________________________________

    This is the Login.jsp page. This is where the user enters his/her username/password to be validated via the LoginServlet.



    __________________________________________________________________________________

    This is from LoginDAO. Method to retrieve the data and create Customer object.

     
    Jeremy McNally
    Ranch Hand
    Posts: 108
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The flightsearch.jsp in my initial post is the page to be forwarded TO after a successful login. I just wanted to clarify that for you.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66184
    146
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Is this not the issue that's being discussed here?
     
    Jeremy McNally
    Ranch Hand
    Posts: 108
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Not my login Issue. That is actually in the other post that you have been responding too. I fixed my Final non-working issue BUT I added something that is also quite unnecessary but I had no other choice. Here check it out if you would like to. I will paste it below. I also pasted it in that other post.

     
    Paul Clapham
    Sheriff
    Posts: 22471
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You don't need the last part of that code. You could simplify the code to look like this:



    What made you think you needed the rest of what you posted?
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66184
    146
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Let's discuss it in the other post rather than in two places.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!