I have a servlet (front-controller), which analyse the request, prepare some necessary data (model) and then should pass it to the jsp to be rendered.
How should I pass the data from servlet to jsp? (I hoped that it was possible to add new parameters to parameters map in the request object, but that map is unmodifiable).
I can add attributes to the request but I don't know how to retrieve them from the jsp.
All the data should be in the request scope. What's the correct way?
I can add attributes to the request but I don't know how to retrieve them from the jsp.
You don't need to specifically 'retrieve' them, just referring them works
request.setAttribute("titleAttribute", "kittens are fuzzy");
and then
Title here: ${titleAttribute}
You can use the request or the session scope for this. Apart from the answer by Nikita Rybak, you can use
request.getSession().setAttribute("key","value");
And then use it in JSP using scriplet.
<%=session.getAttribute("key")%>
Note that in the example given by Nikita, Expression Language(EL) has been used(I am not sure if it's JSTL tags). You need to explicitly state that EL is not to be ignored by using the isELIgnored property.
<%# page isELIgnored="false" %>
Related
I want to display something from one jsp page in another jsp page by clicking a button. I did it using request.setAttribute request.getAttribute but it doesn't work for me, for some reason the variable I send is null or the page is blank.
From your original question : When you are doing setAttribute(), its scope is limited to the request when the main page is loading and hence will not be available on the next page as it will be a new request.
<%Object product=ptp;
request.setAttribute("purchase", ptp.getId());
%>
What you can do is, submit this value in URL param as GET or in a form (get/ post) to fetch it on next JSP using request.getParameter().
Or you can use session scope by session.setAttribute()
Hope it helps
you can pass the variables through request scope or session scope.
request.setAttribute("variable name","value of its");
session.setAttribute("variable name","value");
Here a detailed exmple
http://www.jsptut.com/sessions.jsp
In my application I am trying to do a pageContext include in a custom tag java file. I am going to include a jsp file.
Can I pass any parameters to the jsp file which would be scoped only to the file(like params passed in jsp:include)?
I am aware that I can set it as an attribute in the request and use it in the jsp, however I don't think this is the best solution as I do not want it to persist in the request. Besides, the jsp file may in turn use the same tag again, which would override this attribute, and the rest of the jsp would have the wrong attribute.
Here is the code for that:
request.setAttribute("myVariable", this.viewletId);
pageContext.include(viewletLayout);
Is there a way to pass it to just the jsp in its scope(Like jsp:include)?
Look here to get the solution that hinted me to what worked for me:
<%
if ("layout1".equalsIgnoreCase(viewletLayout)){
%><%#include file="layout1.jsp"%><%
} else if ("layout2".equalsIgnoreCase(viewletLayout)){
%><%#include file="layout2.jsp"%><%
}
%>
I have a Servlet, which sends values to a JSP1. I am setting the values in the servlet as
request.setAttribute();
and i am using
dispatcher.forward(request, response);
and i am able to access these values in the JSP1 using expression language.
Now, I also need to use this in another JSP2.
These two JSP's are clubbed together to display in a single page. So, I need to access the values of the servlet in the second jsp as well. How can i do that?
As you say you have both the jsp clubbed in a single page , you can simply get the attribute as,
${attributeName}
it refers to the object set in the HttpServletRequest scope . I suggest to take the tutorial for Expression language
It is quite simple. If you are setting an attribute "attr1" in a servlet and then forwarding to a JSP (file1.jsp):
request.setAttribute("attr1", "first attr");
request.getRequestDispatcher("file1.jsp").forward(request, response);
And suppose in the first JSP (file1.jsp), you are including another JSP (say file2.jsp):
<body>
.....
<jsp:include page="file2.jsp"></jsp:include>
.....
</body>
Then in file2.jsp, you can access attr1 by calling the request object's getAttribute method:
<body>
....
${attr1}
....
</body>
In case of jsp:include action, when file2.jsp is included in file1.jsp, both request and response objects are passed to file2.jsp as parameters. So file2.jsp can access the attributes of file1.jsp's request object.
i want to use JSP values in servlet. iam fetching some parameters from the url and i need to pass those parameters in my servlet and also the servlet result something by using those parameters and i want to use the result again in the same jsp and in other jsp.
i get those parameters first time when my home jsp is called.
i am getting url parameter by using jstl core tags:
<c:set var="data" scope="session" value="${param.urldata}" />
How do i do this using JSP tags???
Thanks in advance
As i understand somewhat , you are trying to send the values from JSP page to Servlet..
For this we have lot of solution
Pass the value through method
Set the value at session and get it wherever you want in the whole application
I prefer second point, because which hold the value for the entire application
Check Pass variables from servlet to jsp
Above question pass the value from servlet to JSP,Your question is opposite of the answer that's it..!
Set value at JSP : session.setAttribute("key","value");
Get value at Servlet: session.getAttribute("name");
Also refer : Sharing values between servet and JSP
Is it possible to have a servlet that contains an object (an ArrayList in this case) that then does the equivalent of displaying a jsp page and passing that object to the jsp page. In this case the ArrayList contains database results an I want to iterate through and display the results on the JSP page.
I am not using any MVC framework, is it possible to do this with the basic Servlet/JSP architecture.
Yes.
in the servlet call request.setAttribute("result", yourArrayList);
then forward to the jsp:
getServletContext().getRequestDispatcher("your.jsp")
.forward(request, response);
using JSTL, in the jsp:
<c:forEach items="${result}" var="item">
...
</c:forEach>
If you don't want to use JSTL (but I recommend using it), then you can get the value using request.getAttribute("result") in the JSP as well.
Alternatively, but not recommended, you can use request.getSession().setAttribute(..) instead, if you want to redirect() rather than forward().
You can pass objects to jsp's by embedding them within the Request.
request.setAttribute("object", object);
and within the jsp:
request.getAttribute("object");
You can put it using request.setAttribute("myobj", myObj); see javadoc
If you are trying to make some kind of "component" then it's better to convert the JSP page into a custom tag. Here is excellent article about that: http://onjava.com/pub/a/onjava/2004/05/12/jsp2part4.html