• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

can i send the java value object from url to one url

can I send the java value object from url to one url.can any body some links or examples?

i need to send this object from one context to another context that are deployed in different servers.
0
chaitu chaitu
Asked:
chaitu chaitu
  • 10
  • 6
  • 5
  • +1
1 Solution
 
CEHJCommented:
0
 
chaitu chaituAuthor Commented:
// Construct data
    String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(roger, "UTF-8");
    data += "&" + URLEncoder.encode("dob", "UTF-8") + "=" + URLEncoder.encode(05/20/11, "UTF-8");

do we need to send like key-value pairs like above..
0
 
objectsCommented:
serialize the object somehow and POST it to the other server
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
objectsCommented:
> do we need to send like key-value pairs like above..

thats a GET request and you can do that also, sending the properties required to recreate object at other end

0
 
CEHJCommented:
>>do we need to send like key-value pairs like above..

You don't need to, but you could, e.g.
data += "&" + URLEncoder.encode("object", "UTF-8") + "=" + URLEncoder.encode(base64EncodedObjectString, "UTF-8");

Open in new window

0
 
chaitu chaituAuthor Commented:
// Construct data using post
    String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
    data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");


// Construct data using get
String queryString = "param1=" +
 URLEncoder.encode(param1Value, "UTF-8");
queryString += "&param2=" +
URLEncoder.encode(param2Value, "UTF-8");

Difference i found between get and post is in post also u use URLEncoder.encode for key value.am i right?

objects,
few days back i asked you about sending object from context(http://localhost:8080/dept) to another context(http://localhost:8080/emp) if both are deployed in the same server.
at that time you said you cannot send object if both are deployed in the same server,need to send only request parameters like apending parameters to query string.

for that requirement also can't we use this solution?
0
 
chaitu chaituAuthor Commented:
"data += "&" + URLEncoder.encode("object", "UTF-8") + "=" + URLEncoder.encode(base64EncodedObjectString, "UTF-8"); "


how this base64EncodedObjectString will be contructed??
0
 
objectsCommented:
> for that requirement also can't we use this solution?

yes, thats what I was suggesting in the other q :)

> how this base64EncodedObjectString will be contructed??

not a good idea, better to serialize your object (eg. as xml, or json) and use a POST request
0
 
CEHJCommented:
You can also use sun.misc.BASE64Encoder in the JDK, but it's deprecated
0
 
chaitu chaituAuthor Commented:
object,

just for my confirmation  we can use this solution for that requirement as well.
0
 
CEHJCommented:
The easiest way is to use a servlet at the receiving end - then you can use ObjectInputStream directly
0
 
objectsCommented:
> just for my confirmation  we can use this solution for that requirement as well.

yes
0
 
chaitu chaituAuthor Commented:
let me try with XML and use a POST request ..
0
 
jinujineshCommented:
You can bundle the object and send it as a JSON object too
0
 
chaitu chaituAuthor Commented:
jinujinesh:

can you post some pseudo code on  send it as a JSON object too ??
0
 
objectsCommented:
As i mentioned earlier its the same as snding xml. You just need to serialize your object (as json) and pass the json data in your POST request
0
 
chaitu chaituAuthor Commented:
in this case also do i need to use URLConnection ??

can you provide on how serialize your object (as json) ??
0
 
objectsCommented:
you can use URLConnection to send POST request

have a look at flexjon for serialization
http://flexjson.sourceforge.net/
0
 
chaitu chaituAuthor Commented:
i have found good link .by looking at this i think that without using 'use URLConnection to send POST request' we can post the data to other server.


http://www.ibm.com/developerworks/xml/library/wa-ajaxintro11/index.html
0
 
chaitu chaituAuthor Commented:
I have written ClientServlet to send the data from client to server.

Whenever it connects this url(http://localhost:8080/DEPT/server) i have called one filter at the server side where it will read the response and put it in an
object and set in request scope.object is printing in fine in filter.

apart from pushing the data need to redirect to the application page  by calling  spring action class.so i mentioned that url in ClientServlet.
but  in the spring action class object is coming as null from request object.


ClientServlet
*************
public void doPost(HttpServletRequest request, HttpServletResponse response)
			 { 
		HttpCon con=null;
		String xmldata=null;
		try {
			System.out.println(".......ClientLoginAction..in AppContext.........");
			String strURL = "http://localhost:8080/DEPT/server";			
			String paramName[]={"userName","password"};
			String paramVal[]={"admin","admin"};			
			String datas=HttpCon.httpPost(strURL, paramName, paramVal);
			System.out.println("datas...."+datas);
			System.out.println("strURL..."+strURL);
			response.sendRedirect("http://localhost:8080/DEPT/app?_flowId=app-flow").forward(request, response);

		} catch (Exception e) {
			System.out.println("Getting Exception" + e);

		}



Filter
**************

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException, NoSuchFlowExecutionException {

  
        HttpServletRequest request = (HttpServletRequest) servletRequest;

		Bean bean new Bean();

	//reading response that client sent
	Enumeration enumeration = request.getParameterNames();

		while (enumeration.hasMoreElements()) {
			String parameterName = (String) enumeration.nextElement();
			 if(parameterName.equals("firstname")
			  bean.setFirstName(request.getParameter(parameterName));
  			 if(parameterName.equals("LastName")
    	                  bean.setLastName(request.getParameter(parameterName));
		}
      

	//put the object in request scope
		
		System.out.println(bean+"******************request**** server context*********"+request);
        request.setAttribute("bean", bean);
      

    }

//Spring web flow action class
*********************************

public class DeptAction
{

   public Event  initialize(RequestContext context) {

    if (context != null) {
    	    	ServletExternalContext servletContext = (ServletExternalContext) context.getExternalContext();
                if (servletContext != null) {
                    request = servletContext.getRequest();
                    response = servletContext.getResponse();
                   }
            }

    	 	Bean bean = (Bean)request.getAttribute("bean");
		
		System.out.println(bean+"******************request**** server context*********"+request);

}
	
}

Open in new window

0
 
chaitu chaituAuthor Commented:
this solution looks easy compared to the solutions offered by experts.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 10
  • 6
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now