Receive Linked List from client using Java GET - java

I'm receiving string from client like following -
String time_S = request.getParameter(Message.KEY_TIME);
Now, If I want to receive a linked list data how should I do that? I tried to use getParameterValues but I don't think I could use it properly.
Thanks in advance.

You can't really retrieve a 'linked list' per se over HTTP - it needs to be serialized (transformed from a Java object to a string). There are plenty of ways of doing this, but you might have them send it to you as a set of comma separated values and then parse it into a linked list or java data structure of your choice.

If you have them send if via JSON, there are several libraries that can be used which will change them to Java standard objects. Such as Simple.JSON, it turns JSON Array's into Java List objects, or JSON Object's into Java Map's.

getParameterValues might be the easiest way, provided that the client can provide each element of the list as a repeated querystring parameter.
For example if the client can send this querystring:
color=red&color=white&color=blue
getParameterValues("color") will return {"red", "white", "blue"}

Depends on how you submit the data. For example, if you submitting data from a web page and you are submitting data using the same parameter name, you can use the getParameterValues method.
For example, take the following inputs:
<input type="text" name="time" />
<input type="text" name="time" />
<input type="text" name="time" />
Then you can access the parameters as follows
String[] times = request.getParameterValues("time");
And if you need that linked list, just do the following
LinkedList<String> timeList = new LinkedList<String>(Arrays.asList(times));

Related

Mule: Http->xml

Mule version: 3,5 (Actually, mule studio)
I'm new at using mule and can't seem to figure out how to do the following properly:
Want to achieve:
Send data from a html form to mule which then splits the payload and sends one part to one java program and the other to another.
Html form:
<form method="GET" action="http://localhost:8081">
<input type="text" name="name"/>
<input type="text" name="lastname"/>
<input type="submit" value="Submit" />
</form>
This points to localhost and the port which is set in MuleStudio.
Real Question:
What could I use to transform the data from the inbound http to xml?
Side Question:
I can send the whole payload to one javaprogram. (first a POJO and then to the actual program. - is this the right way or can I send it to the program and skipping the POJO?)
Since your main concern is about transformation, let's only address it.
If you're using the Enterprise Edition, you can use DataMapper to generate the XML you need. Creating a schema representing the target XML would help a lot.
Otherwise, if you're using the Community Edition, you can use different options:
A Groovy component using the excellent MarkupBuilder to generate the target XML right from the inbound message payload,
A chain of standard transformers that first transform the submitted data to a generic XML form (object-to-xml-transformer) then transform it to the desired form (xslt-transformer).
I'm sure there are other options, but these should get you going :)

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

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.

How to pass a non-string Java object to an ActionBean - Stripes

I'm using the Stripes framework. I want to pass non-string Objects to an ActionBean. Is this possible?
I am trying to do:
<s:url var="statementUrl" beanclass="sempedia.action.StatementActionBean" prependContext="false" >
<s:param name="property" value="${row.key}" />
<s:param name="values" value="${row.value}" />
<s:param name="myString" value="Why kick a moo cow" />
</s:url>
<jsp:include page="${statementUrl}"/>
Where row.key resolvs to a custom class I have defined and row.value is an ArrayList of a custom class I have defined
Nope, nothing really.
I mean, there's always a way. You could serialize the forms out to a byte array and Base64 encode in to a string and then pass that as an argument.
But then you start running in to URL limits (they can only be so long).
If practical, you could save the data in the Session and simply refer to it later. You could use Stripes FlashScope, which stuffs it in the Session but only for the next request, then it goes away.
You could encode the data in to a HTML form, but then you would need to POST that rather than use a GET for it.
You could save the data out to another store (a database, memcache, something like that), and simply return a key to it, then pass in the key.
Really depends on the lifecycle of what you're trying to do, and the nature of the data.

How do I bind HTML table data to a java object in a spring controller?

I have a spring MVC application using JSP as my view technologies with Jquery for AJAX. I have a table such as the following:
<table>
<tr>
<td>name1</td>
<td>value1</td>
<td>setting1</td>
</tr>
<tr>
<td>name2</td>
<td>value2</td>
<td>setting2</td>
</tr>
</table>
I need to serialize this table so that it can later be bound to an object in my controller. However the jquery serialize() method only works on form fields. What would be the best approach to get the table data into the HTTP request so that I can later bind it to a java object?
EDIT:
I have a java object that has a collection so
class MyOject {
private List<AnotherObject> items = new ArrayList<AnotherObject>();
// standard getters and setters
}
class AnotherObject {
private name;
private value;
private setting;
// getters and setters
}
In the screen the user is creating new items on the fly. When the user is done, they submit the form and then I need to process all the items in the list and instantiate a new collection with those items.
For display purposes I am creating a new table row when an item is created.
The <Form> tag is how you tell the browser "Put this stuff in the web request." That's how you get object binding in Spring. What is your reason for not using a Form? You don't necessarily have to put it in a form in the page, you could give your table elements IDs and fetch their contents in the javascript if you really needed to.
Edit: I think maybe it's hard to answer because it's not clear why you want the browser to give you back things that you gave it in the first place. Maybe what you really need is the #SessionAttributes() annotation on your controller so that you can preserve State of the original page shown to the user?
More Edit:
kk, see now. If what you want is Spring web data binding then create a form in parallel as you add more table rows. e.g.,
<form id="myObject" action="whateverYouNeedHere.htm" method="post">
<input type="hidden" id="items[0].name" name="items[0].name" value="foo"/>
<input type="hidden" id="items[0].value" name="items[0].value" value="bar"/>
<input type="hidden" id="items[0].setting" name="items[0].setting" value="buzz"/>
<input type="hidden" id="items[1].name" name="items[1].name" value="foo"/>
<input type="hidden" id="items[1].value" name="items[1].value" value="bar"/>
....
Then just submit that and it will bind right on for you. If you did mean to handle the content yourself, then you probably could use XHR as someone else mentioned.
Use the Spring Data Binding and Validation API to bind it into a Java object of your own design. That documentation is web-agnostic; check out the later chapter to see how the web tier leverages it.
You'll want an abstraction beyond a table, I presume.
in order to stick your table information into a java object, you will first need to send it to the server.. for that you will need to either send it via XHR or in a form.
in order to serialize the object you will need to write some javascript/jquery.
i could write it for you, but your requirements are somewhat vague when it comes to how your table will look, nor do i want to guess about what the java object you want to add your data to looks like.

Categories