java - communication between remote servlets -


I have two web applications, which say App1 and App2. I want to call a service which is in App2 from servlet in App2. I am using the URL connection for this, I am able to pass the parameter for the servlet in App2 and I get feedback from the servlet as a straight Able to do But I want to send java objects from servlet in App2 and want to get them in the App1 servlet. How to get it?

Depending on


If those webpages are physically located on the same webserver in the same serviced container, then set it only as a request attribute and forward the request for another reference:

  request.setAttribute ("name", object); ServletContext app2 = getServletContext (). GetContext ("app2"); App2.getRequestDispacher ("servletUrl"). Forwarded (request, response);  

Other context objects will be able to get:

  object object = request.getAttribute ("name");  

This requires only a server setting, which can be accessible by each other. How to do this depends on the servletcontainer For example, you can see the & lt; Context & gt; The element needs to set the true to the crossContext attribute.

  & lt; Reference crossContext = "true" & gt;  

Then it will be available for other references. For other servers, consult your document.


If those webpages run physically on different webserver, then there are several options:

  1. Convert the string to and send it as a parameter On recovery, change back from the string. JSON is a good format for this. Provides the possibility of converting full-width Java objects and JSN and vice versa. If you are using GET and the request URI is longer than 2KB, consider using POST instead of GET, otherwise the URI can be shortened by the server. Pros: Better reusable service cons: Difficult to send binary data.

    Also see: .

  2. Send a Multipart / Form-Data using URL Connection HTTP Post Request or on the other side and its Using the process. Pros: Standard specification, possible to send binary data Cons: More code.

    Also see: .

  3. Java objects, using it using the URLConnection #getOutputStream () and get it raw from HttpServletRequest #getInputStream () Make and unserialize it by using Pros: Easy Cons: Re-usable, tight-coupled.

    Also see: More.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -