Does ServletRequest.setAttribute permit key names with periods? - java

I have a java webapp with a Struts 1 action that has the following code:
request.setAttribute("cat.sound", "meow");
On my jsp page, I have the following tag:
<c:out value="${cat.sound}" />
However, "meow" is never printed on the JSP page. This might work if I had an object of type "cat" to do something like:
request.setAttribute("cat", cat);
Unfortunately, this webapp does not have any object defined for the cats and the jsp pages are frozen (no changes allowed).
So is it possible to use request.setAttribute with a key name containing periods/dots? How would the JSP page need to reference the set parameter?

You can avoid having to create a Cat class if you set cat to a map with a String key "sound":
request.setAttribute("cat", Collections.singletonMap("sound", "meow"));
Collections#singletonMap() gives you a nice, succinct way to create a map with one entry.

Related

Passing an ArrayList of a class from a jsp to a servlet

I have an ArrayList of a class Room. I need to send it from a jsp to a servlet.
It seems the only way an html or a jsp can send values to a servlet is via a form, the method I tried was to pass it as a hidden parameter as follows:
<input type="hidden" name="allRooms" value="<%=request.getAttribute("allRooms") %>" />
But in the servlet to which i submit this form I get a compile error "String cannot be converted to List" for the following:
List<Room> allRooms=(List<Room>)request.getParameter("allRooms");
Just converting the parameter to an Object type first and then converting it to a List as shown below gives the same exception but this time as a Runtime Exception:
Object a=(Object)request.getParameter("allRooms");
List<Room> allRooms=(List<Room>)a;
Is there any method to pass the List to the servlet or I will have to set it as a session variable in the JSP ?
Thanks in advance.
Is there any method to pass the List to the servlet or I will have to set it as a session variable in the JSP ?
Use session.That is one best solution.
There is no way to represent an ArrayList in HTML to send via html form. Use session instead.
I think if you pass the following params, and array is formed in servlet side
param[0]=ss , param[1]=ssw
You could do this.
List<String> roomParams =(List<String>)request.getParameter("param");
But to make this.
List<Room> allRooms=(List<Room>)request.getParameter("allRooms");
That I think is not possible, so you should use Session attributes
session.setAttribute("allRooms", new ArrayList<Room>());
I believe you should not be sending the List to Servlet directly and should look out for other options.
How are you generating the ArrayList on the browser page? If it is generated from a multi-select element from UI, then you can access the request parameter values as Array in Servlet.
Shishir

read parameters passed by one jsp to another

I want to read parameter passed by one JSP to another using HTTP POST method.
Following are my two JSP files.
One.jsp
<body>
<form action="Two.jsp" method="post">
<input type="text" value="test value" name="txtOne">
<input type="submit" value="Submit">
</form>
</body>
Two.jsp
<body>
<% response.getWriter().println(request.getParameter("txtOne")); %>
</body>
I can access the parameter in Two.jsp file using scriplet.
I want to avoid scriplet, so I am looking for JavaScript or jQuery solution.
So far I have searched and found JavaScript solution which only reads parameters sent using GET method(query string only).
Any suggestion will be appreciated.
Thanks in advance.
Solution:
I was able to get the value using JSTL:
${param.txtOne}
Try expression language - ${txtOne}, of if the method in one.jsp is GET instead of POST, you would be able to read URL in javascript and extract parameter value from there.
Yes you can get the value by use of JSTL
${param.txtOne}
Update
From EL info page
In EL there are several implicit objects available.
EL Scriptlet (out.print and null checks omitted!)
---------------------------------- ---------------------------------------------
${param.foo} request.getParameter("foo");
${paramValues.foo} request.getParameterValues("foo");
${header['user-agent']} request.getHeader("user-agent");
${pageContext.request.contextPath} request.getContextPath();
${cookie.somename} Too verbose (start with request.getCookies())
Implicit Objects
The JSP expression language defines a set of implicit objects:
pageContext: The context for the JSP page. Provides access to various objects including:
servletContext: The context for the JSP page’s servlet and any web components contained in the same application. See Accessing the Web Context.
session: The session object for the client. See Maintaining Client State.
request: The request triggering the execution of the JSP page. See Getting Information from Requests.
response: The response returned by the JSP page. See Constructing Responses.
In addition, several implicit objects are available that allow easy access to the following objects:
param: Maps a request parameter name to a single value
paramValues: Maps a request parameter name to an array of values
header: Maps a request header name to a single value
headerValues: Maps a request header name to an array of values
cookie: Maps a cookie name to a single cookie
initParam: Maps a context initialization parameter name to a single value
Finally, there are objects that allow access to the various scoped variables described in Using Scope Objects.
pageScope: Maps page-scoped variable names to their values
requestScope: Maps request-scoped variable names to their values
sessionScope: Maps session-scoped variable names to their values
applicationScope: Maps application-scoped variable names to their values
I want to avoid scriplet, so I am looking for JavaScript or jquery solution.
Simply you cannot.
request object can only accessible on server side, i.e in your JSP. You cannot access request or response object in client side that i.e javascript/jquery/whatever.
If you want access jsp value in javascript, try something like
var news=<%= request.getParameter("txtOne")) %>;
As a side note: Avoid scriplets and go for Expression language.
It is time for you to move to some MVC framework like struts than plain JSP. Then you can fill an ActionForm from parameters and use them on Two.jsp.

traversing arraylist using EL using foreach

I have a class language having features ID and Name.
I have another class Language_List that has ArrayList<language> lang as a member variable.
In my JSP page I want to access the Name variable of ArrayList<language> lang using EL and For each Loop.
<c:forEach var="language" items="${languages.lang}">
${language}<br>
</c:forEach>
However, it doesn't show ant result and intellisense doesn't work too. Anyone who can help me with this
PS: languages is a Bean variable contain list of languages from DB
I tried this and got this
<b>${languages.lang}</b>
HTML
[sakila.language#f1541c, sakila.language#63c8fabf, sakila.language#1fc644c7, sakila.language#11cd751d, sakila.language#47c3cc0c, sakila.language#7894ca3, sakila.language#47066532, sakila.language#74ddda0b, sakila.language#1116441e, sakila.language#4cd21655, sakila.language#74b84dd9, sakila.language#6fff1d6c, sakila.language#55e4d6e5, sakila.language#22d88071, sakila.language#33d88c96, sakila.language#4df5e671, sakila.language#4aec2cb3, sakila.language#576ac232, sakila.language#76a6dbd7, sakila.language#44ab3d1c, sakila.language#46391c7c, sakila.language#4f7d34e8, sakila.language#251c941d, sakila.language#77400ef3]
The EL doesn't access fields of your objects. It accesses bean properties of your objects. This means that ${languages.lang} is translated to a call to languages.getLang().
If you don't have such a getter, you'll get an exception though. If it just doesn't display anything, it's probably because languages is null, or because its lang list is null or empty. To confirm or infirm those guesses, we need to see the code where you create and populate the bean and its list of languages, and where you store it somewhere to make it accessible from the JSP.
Another possibility is that you forgot to declare the core taglib at the beginning of the JSP. To confirm or infirm that, paste the code of the JSP, and the HTML code generated by the JSP (using View page source in the browser)

Websphere Commerce-TypedProperty

Can anyone help me to understand the usage of TypedProperty in websphere commerce?
ie,How to pass values from one jsp to other using TypedProperty without a command class.I would prefer to handle it in my client side itself without invoking Command class..can anyone help me to sort out it?
Typed property is usually used to pass values from controller commands to JSPs. If you just want to pass values from one JSP to another, create a form in your first JSP and submit it to the second.
If this is a form submit, set the values you need to pass in element. In the results jsp you can get those values using ${WCParam.xxx} .
FYI - To list out all the values in WCParam object try to print the below in JSP :
${WCParamValues}
We use typedProperty when we need to send anything from the command. For example, you give an order ID from the first JSP and want to get the final amount to be passed the result JSP. Here in the command we use the orderID from the request object -> Then we use the OrderAccessBean to get the OrderTotal -> then we set this to a TypedProperty object -> we then set this TypedProperty object to request properties using setRequestProperties() OOB method in a controller command.
Hope this makes it clear !
TypedProperty is a class in Java which can be compared to Hashmap in Java for better understanding. It is a name value pair combination.
I just wanted to understand the problem before answering further.
Why do you want to use TypedProperty in Jsp to pass the value from one jsp to another?
Are you importing the second jsp or including the second jsp to which you have to pass the values to?
If you are importing, you can use c:param tag to pass the values to the second jsp.
For included jsps, the values are already available in the second JSP.
Please include code snippets to explain your problem so that it can be answered clearly.
You can pass parameters from one jsp to another by using the following code snippet:
<c:import url="child.jsp">
<c:param name="name1" value="value1" />
<c:param name="name2" value="value2" />
<c:param name="name3" value="value3" />
</c:import>
Within the child.jsp you can read the parameters by using:
<c:out value="${param.name1}" />
<c:out value="${param.name2}" />
<c:out value="${param.name3}" />
A TypedProperty is nothing but a Wrapper to HashMap. So that's nothing to do here with passing values from one JSP to another JSP. Without invoking a command, you can't pass a Java object to another JSP.
And that is the very basic of Command Framework. I would prefer to go with the first answer.

Dynamic delete on jsp file

i want to remove some table lines dynamicaly using struts2 and ajax.
i got an object "object1" which contains a list of "object2"
public class Object1 {
[...]
private int id;
[...]
private List<Object2> objs2;
[...]
}
in a jsp file a display informations about object1 in a form to be able to modify some input.
To display the list i use an iterator.
<s:iterator value="object1.objs2" status="objsStatus">
this work fine, and i want to be able to delete one of these object2. so i put a link after each object2 which call a "delete" action with struts2-jquery.
<s:url id="delObject" value="/deleteObject2.action"/>
<sj:a id="delObj%{#objsStatus.index}" href="%{delObj}?object2.name=%{name}" targets="result">delete</sj:a>
my struts.xml
<action name="deleteObject2" class="Object2Action" method="deleteObj">
<result></result>
</action>
this also works fine BUT (there always a but...) i have to "refresh" manually the page to see the "new" list of object2. is it possible to call a javascript function on result success to remove the appropriate table line, using for example:
$(this).closest('tr:not(:only-child)').remove();
You can try Reversen AJAX in jQuery. It will push data when it changed on server.
http://blog.jamieisaacs.com/archives/404
http://plugins.jquery.com/plugin-tags/reverse-ajax

Categories