Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2011-05-10
22
Medium Priority
?
405 Views
Last Modified: 2012-05-11
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
Comment
Question by:chaitu chaitu
  • 10
  • 6
  • 5
  • +1
22 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35726753
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35726760
// 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
 
LVL 92

Expert Comment

by:objects
ID: 35726761
serialize the object somehow and POST it to the other server
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 92

Expert Comment

by:objects
ID: 35726764
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35726790
>>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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35726800
// 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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35726805
"data += "&" + URLEncoder.encode("object", "UTF-8") + "=" + URLEncoder.encode(base64EncodedObjectString, "UTF-8"); "


how this base64EncodedObjectString will be contructed??
0
 
LVL 92

Expert Comment

by:objects
ID: 35726815
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35726824
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35726846
You can also use sun.misc.BASE64Encoder in the JDK, but it's deprecated
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35726923
object,

just for my confirmation  we can use this solution for that requirement as well.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35727063
The easiest way is to use a servlet at the receiving end - then you can use ObjectInputStream directly
0
 
LVL 92

Expert Comment

by:objects
ID: 35727105
> just for my confirmation  we can use this solution for that requirement as well.

yes
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35727181
let me try with XML and use a POST request ..
0
 
LVL 1

Expert Comment

by:jinujinesh
ID: 35775141
You can bundle the object and send it as a JSON object too
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35782675
jinujinesh:

can you post some pseudo code on  send it as a JSON object too ??
0
 
LVL 92

Expert Comment

by:objects
ID: 35782681
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35782879
in this case also do i need to use URLConnection ??

can you provide on how serialize your object (as json) ??
0
 
LVL 92

Expert Comment

by:objects
ID: 35782902
you can use URLConnection to send POST request

have a look at flexjon for serialization
http://flexjson.sourceforge.net/
0
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 0 total points
ID: 35782936
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35808255
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
 
LVL 20

Author Closing Comment

by:chaitu chaitu
ID: 35997889
this solution looks easy compared to the solutions offered by experts.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question