How can I pass variable from servlet to jsp?
setAttribute
and getAttribute
didn't work for me :-(
相关问题
- #{facesContext} EL expression not resolved at runt
- jsp caching tag library
- how to create files under /WEB-INF/
- tomcat websocket servlet listening port
- JSP template implementation (Composite View Patter
相关文章
- jsp里面的画布功能,为什么会出错?求大佬找下问题
- JSP String formatting Truncate
- java.lang.NoClassDefFoundError: javax/servlet/http
- Forward request from servlet to jsp
- Intercept @RequestHeader exception for missing hea
- Comparing string and boolean in Expression languag
- Integrating Jetty with RESTEasy
- How to abort Tomcat startup upon exception in Serv
If you are using Action, Actionforward way to process business logic and next page to show, check out if redirect is called. As many others pointed out, redirecting doesn't keep your original request since it is basically forcing you to make a new request to designated path. So the value set in original request will be vanished if you use redirection instead of requestdispatch.
Besides using an attribute to pass information from a servlet to a JSP page, one can also pass a parameter. That is done simply by redirecting to a URL that specifies the JSP page in question, and adding the normal parameter-passing-through-the-URL mechanism.
An example. The relevant part of the servlet code:
And the relevant part of JSP page
example.jsp
:This is an servlet code which contain a string variable a. the value for a is getting from an html page with form. then set the variable into the request object. then pass it to jsp using
forward
andrequestdispatcher
methods.in jsp follow these steps shown below in the program
You could set all the values into the response object before forwaring the request to the jsp. Or you can put your values into a session bean and access it in the jsp.
It will fail to work when:
You are redirecting the response to a new request by
response.sendRedirect("page.jsp")
. The newly created request object will of course not contain the attributes anymore and they will not be accessible in the redirected JSP. You need to forward rather than redirect. E.g.You are accessing it the wrong way or using the wrong name. Assuming that you have set it using the name
"name"
, then you should be able to access it in the forwarded JSP page as follows:Simple way which i found is,
In servlet:
You can set the value and forward it to JSP like below
In Welcome.jsp you can get the values by
(or) directly u can call