Scope of jsp:useBean
home.jsp
<jsp:useBean id="username" class="java.lang.String" sco开发者_StackOverflow中文版pe="application"/>
<%
username="Jitendra";
%>
<jsp:include page="include.jsp"/>
include.jsp
<%=username%>
This gives an error saying “username” is undefined in include.jsp, even though the scope of Bean is application…
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:
- Sun Java EE tutorial part II chapters 1-8.
- Coreservlets.com JSP/Servlet tutorials.
- Head First Servlets & JSP book.
Hope this helps.
Your code is "evil" because a java.lang.String
is not really a bean. Significantly, it doesn't have a "set" method to change its text (that's intentional, it's meant to be immutable).
The way you access beans is to declare one, and then use its properties (i.e. the names behind the get() and set() methods) to change it. You cannot directly change the actual bean instance, only its values.
On Tomcat 6, home.jsp
is translated to the Servlet code:
java.lang.String username = null;
synchronized (application) {
username = (java.lang.String) _jspx_page_context.getAttribute("username",
PageContext.APPLICATION_SCOPE);
if (username == null){
username = new java.lang.String();
_jspx_page_context.setAttribute("username", username,
PageContext.APPLICATION_SCOPE);
}
}
username="Jitendra";
org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response,
"include.jsp", out, false);
Although the behaviour is the same, the exact code generated depends on the application server implementation.
The scope of the local username
variable does not extend into the Servlet that will be generated from include.jsp
. You are not setting the value "Jitendra" into the application scope, only setting the value of the local variable.
As others have pointed out, an immutable String does not make a very good bean.
精彩评论