0

I am a beginner in JSP and need some help. I started out with this bit of code inside one of my first JSP pages

<%
    LoginService user = new LoginService();
%>

Hello <%=user.getUserID() %>!

And works just fine. I imported the LoginService class correctly and that bit of code works! But now I am learning JSTL and trying to convert the above to JSTL tags. So I got off on the right foot but need some help finishing. So far I have

<jsp:useBean id="user" class="org.test.LoginService"></jsp:useBean>

But I'm having trouble converting that last Hello line into a JSTL tag! I can't figure out how to reference the method getUserID() like I did in the JSP tag. I've come this far on my own but need some help finishing up! Any response would be greatly appreciated!

gmustudent
  • 2,229
  • 6
  • 31
  • 43
  • 1
    This approach is obsolete these days. Using of any `jsp` tags has no place. And using of the `jsp:useBean` tag creates yet another instance of the class `LoginService` in your case. Use more flexible and functional tags `c:set` and `c:out`. They along [EL](http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPIntro7.html) can completely replace the use of scriptlets and `jsp` tag library. – kapandron Aug 10 '12 at 00:23
  • You can read about why such the approach is preferable [here](http://stackoverflow.com/questions/9680248/getting-a-null-value-for-where-i-expect-a-string-set-by-the-mutator/9680335#9680335) and [here](http://stackoverflow.com/questions/5088625/how-to-get-an-object-of-java-class-from-jsp/5088714#5088714). And by the way using of the `c:out` tag makes sense. [Here](http://stackoverflow.com/questions/291031/jsp-cout-tag)'s the detailed explanation. – kapandron Aug 10 '12 at 00:29

3 Answers3

3

You can use Expression language:

<c:out value="Hello ${user.userId}!" />
kapandron
  • 3,546
  • 2
  • 25
  • 38
  • I'm very new to this (learned jsp today) and am trying to grasp everything you are throwing at me. Can you help me understand how I would create the user object the proper way? Would that be by using the c:set? Thank you – gmustudent Aug 10 '12 at 00:56
  • When I tried to enter in your code it did not work for me. It has a warning that says Unknown tag (c:out). And when I try to run it with the warning I'm thrown a 404 with an Exception thrown on the line of that code. – gmustudent Aug 10 '12 at 01:02
  • Create an user object in your servlet and set it in a http request as attribute `request.setAttribute("user", user);`. Also you can use a session scope. It depends on a context of an issue and an objective. And you need include `jstl` lib correctly. You can read [here](http://stackoverflow.com/questions/8400301/cout-unknown-tag/8400733#8400733) about fixing the problem. – kapandron Aug 10 '12 at 01:34
2

You can use:

<jsp:getProperty name="user" property="userID" /> 

but better to use:

${user.userID}
Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

That would be:

Hello ${user.userID}!
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
Skip Head
  • 7,580
  • 1
  • 30
  • 34