Since you are using Java, you should also look into Servlets and JSPs. This will give you a more non-abstract view into the HTTP world. You will understand how it is used at an application level.
If you are into turning pages, I suggest http://www.amazon.com/Head-First-Servlets-JSP-Certified/dp/0596005407
If you want online tutorials you can find them by googling "Servlet and jsp tutorials".
An overview can be found here
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Overview.html
As to your problem, anything which you declare locally using the old fashioned scriptlets is not linked with a jsp:useBean. Also, declaring a local scriptlet variable is not visible in the included pages, you need to explicitly put them in at least the request scope. As using scriptlets is a bad practice. I recommend to forget about it at all.
In your specific case, just create a real java bean to hold the data. That is, a class with an (implicit) default constructor and private properties which are exposed by public getters/setters. Here's a basic example:
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Then you can use a servlet class to preprocess requests. You can use servlet's doGet() method for this.
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
User user = new User();
user.setName("Jitendra");
request.setAttribute("user", user); // Store in request scope.
request.getRequestDispatcher("/WEB-INF/show.jsp").forward(request, response);
}
Map this servlet in web.xml on an url-pattern of for example /show. This servlet should then be accessible by http://example.com/context/show and its doGet() would be executed immediately.
Then change/create the JSP file show.jsp which you place in /WEB-INF to prevent from direct access (so that clients cannot access it by http://example.com/context/show.jsp but are "forced" to call the servlet) with the following line:
<p>User name: ${user.name}</p>
The ${user} refers to the object which is associated with any request/session/application attribute key user. This does behind the scenes jspContext.findAttribute("user"). As the returned User instance conforms the javabean spec, the ${user.name} will invoke the getName() method on the User instance and the EL will display its outcome.
Oh, I should add, you do not need jsp:useBean for this as the servlet has already created and put the desired bean in the scope.
That said, I recommend to start with a decent JSP/Servlet tutorial/book. Examples:
Yes. It is certainly possible, I assume you have been developing most Desktop Application and native Software using Java and so would advise to take up some JSP and Servlets knowledge to dive into web development.
I would recommend this book: http://www.amazon.com/Head-First-Servlets-JSP-Certified/dp/0596005407. I found it very helpful while learning JSP.
Since you are using Java, you should also look into Servlets and JSPs. This will give you a more non-abstract view into the HTTP world. You will understand how it is used at an application level.
If you are into turning pages, I suggest http://www.amazon.com/Head-First-Servlets-JSP-Certified/dp/0596005407
If you want online tutorials you can find them by googling "Servlet and jsp tutorials".
An overview can be found here
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Overview.html
As to your problem, anything which you declare locally using the old fashioned scriptlets is not linked with a
jsp:useBean
. Also, declaring a local scriptlet variable is not visible in the included pages, you need to explicitly put them in at least the request scope. As using scriptlets is a bad practice. I recommend to forget about it at all.In your specific case, just create a real java bean to hold the data. That is, a class with an (implicit) default constructor and private properties which are exposed by public getters/setters. Here's a basic example:
Then you can use a servlet class to preprocess requests. You can use servlet's
doGet()
method for this.Map this servlet in
web.xml
on anurl-pattern
of for example/show
. This servlet should then be accessible byhttp://example.com/context/show
and itsdoGet()
would be executed immediately.Then change/create the JSP file
show.jsp
which you place in/WEB-INF
to prevent from direct access (so that clients cannot access it byhttp://example.com/context/show.jsp
but are "forced" to call the servlet) with the following line:The
${user}
refers to the object which is associated with any request/session/application attribute keyuser
. This does behind the scenesjspContext.findAttribute("user")
. As the returnedUser
instance conforms the javabean spec, the${user.name}
will invoke thegetName()
method on theUser
instance and the EL will display its outcome.Oh, I should add, you do not need
jsp:useBean
for this as the servlet has already created and put the desired bean in the scope.That said, I recommend to start with a decent JSP/Servlet tutorial/book. Examples:
Hope this helps.
Use JSTL, as @CoolBeans says. It would look something like this:
In the servlet,
Then, in the JSP,
If you're not familiar with JSTL, I'd recommend reading through the JSP section of the Java EE 5 Tutorial, or picking up a copy of Head First Servlets and JSP (it's quite good).
Yes. It is certainly possible, I assume you have been developing most Desktop Application and native Software using Java and so would advise to take up some JSP and Servlets knowledge to dive into web development.
Try to browse through web-development tags as you will find very useful suggestions there.