Link to home
Start Free TrialLog in
Avatar of chaitu chaitu
chaitu chaituFlag for India

asked on

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.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Avatar of chaitu chaitu

ASKER

// 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..
serialize the object somehow and POST it to the other server
> 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

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

// 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?
"data += "&" + URLEncoder.encode("object", "UTF-8") + "=" + URLEncoder.encode(base64EncodedObjectString, "UTF-8"); "


how this base64EncodedObjectString will be contructed??
> 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
You can also use sun.misc.BASE64Encoder in the JDK, but it's deprecated
object,

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

yes
let me try with XML and use a POST request ..
Avatar of jinujinesh
jinujinesh

You can bundle the object and send it as a JSON object too
jinujinesh:

can you post some pseudo code on  send it as a JSON object too ??
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
in this case also do i need to use URLConnection ??

can you provide on how serialize your object (as json) ??
ASKER CERTIFIED SOLUTION
Avatar of chaitu chaitu
chaitu chaitu
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

this solution looks easy compared to the solutions offered by experts.