How can i pass parameters in pageContext include - java

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"%><%
}
%>

Related

How to use jsp value in servlets?

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

How to pass variable from a servlet to a jsp page?

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" %>

How do I pass information from a servlet to a JSP page

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

How Do I make dynamic-attributes Work in JSP Tag Files?

So according to my JSP reference book, as well as every other reference I can find on the web, I'm supposed to be able to do something like:
<%# tag dynamic-attributes="dynamicAttributesVar" %>
and then when someone uses an attribute that I didn't define in an attribute directive, I should be able to access that attribute from the "dynamicAttributesVar" map:
<%= dynamicAttributesVar.get("someUnexpectedAttribute") %>
However, that doesn't work, at all; I just get a "dynamicAttributesVar cannot be resolved" error when I try.
Now, I did discover (by looking at the generated Java class for the tag) that I can "hack" a working dynamic attributes variable by doing:
<% Map dynamicAttributesVar = _jspx_dynamic_attrs; %>
Now, that hack doesn't work unless I also use the dynamic-attributes parameter on my tag directive, so it seems that the parameter is doing something.
But what I want to know is, how can I make it do what it does for every other JSP user out there?
Just trying to get a badge for answering a four year old question.
I have this problem as well and came across some help at O'Reilly to use JSTL instead of scriptlets.
The original poster could have used this code to get all keys/values:
<c:forEach items="${dynamicAttributesVar}" var="a">
${a.key}="${a.value}"
</c:forEach>
This would get a specific value:
<c:out value="${dynamicAttributesVar['someUnexpectedAttribute']}"/>
Isn't "dynamicAttributesVar" the name of the key in the page context that the dynamic attributes are put into? So you could do
<c:out value="${dynamicAttributesVar.someUnexpectedAttributes}"/>
or if you must use scriptlets:
Map dynamicAttributes = (Map) pageContext.getAttribute("dynamicAttributesVar")
(Disclaimer: I haven't tried it, I've just used dynamic attributes in tags with direct Java implementations... but it seems reasonable)

How do set request parameter value while calling java method?

I need to call a method of java class from jsp page.. While calling that in jsp page, need to set some request parameters also. How do i do that in jsp page?
Ex :
Java class :
public void execute() {
string msg = request.getParameter("text");
}
JSP file :
I need to call the method here and also need to set parameter values (eg : &text=hello)
Please help me...
Just embed your Java code inside the JSP. The object "request" is available to get parameters (parameters can't be set). Unless you plan to forward to another JSP in another context with an attribute should be enough.
To refer to your own class do not forget to add the import statement at the beginning of the JSP (similiar to an import in a Java class).
<!-- JSP starts here -->
<%#page import="test.MyClass" %>
<%
MyClass myClass = new MyClass();
String text=myClass.execute();
// This is not neccessary for this JSP since text variable is already available locally.
// Use it to forward to another page.
request.setAttribute("text");
%>
<p>
This is the <%=text%>
</p>
You cannot set request parameters. I suggest you try using the attributes as suggested in the previous answers.
You can't set request parameters - they're meant to have come from the client.
If you need to give the method some information, you should either use normal method parameters, or some other state. The request parameters are not appropriate for this, IMO.

Categories

Resources