aspose file tools*
The moose likes Servlets and the fly likes Servlet to Servlet communication can't get inputstream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet to Servlet communication can Watch "Servlet to Servlet communication can New topic
Author

Servlet to Servlet communication can't get inputstream

Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 396
I'm trying to communicate between servlets for the first time, using an HttpURLConnection. The code below successfully contacts the other servlet's doPost() method but chokes when trying to .getInputStream() to receive a response:



The receiving doPost() is like this:


public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

String targetId = request.getParameter("id");

response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");

response.getWriter().println("<test>" + "response from doPost()" + "</test>");

}


Correlation does not prove causality.
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

Hi,

Please post a detailed exception trace.


http://plainoldjavaobject.blogspot.in
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 396
Lalit Mehra wrote:Hi,

Please post a detailed exception trace.



I'd give you more if I had more. Let me know if you think there's a way to get more. I worked on this all day yesterday, and know that it just stops and waits when trying to get the input stream. I don't think there's anything more to the error ... it's just not able to get it .... it doesn't get one ... and in time, it times out.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3177
    
  47

OK, I am not with enough resources to give this a try, but I suspect that this might be because you closed your OutputStreamWriter before fetching out the response.

Despite the fact that it is technically possible to 'call' a servlet from another servlet though sockers, I don't understand why would someone really do that, given that inter-socket communication is not a light-weight process. Are you probably attempting to communicate with two servlets that are in two different web applications?

If sharing some business functionalities between two servlets in a single web application is all what you wanted, why don't you simply place that centric logic in a method accessible to both servlets?

Roger F. Gay wrote:I'd give you more if I had more. Let me know if you think there's a way to get more.

You should be able to see your stack trace in your container's log file(s). If you are using Tomcat, there are in 'logs' directory.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 396
Devaka Cooray wrote:OK, I am not with enough resources to give this a try, but I suspect that this might be because you closed your OutputStreamWriter before fetching out the response.

Despite the fact that it is technically possible to 'call' a servlet from another servlet though sockers, I don't understand why would someone really do that, given that inter-socket communication is not a light-weight process. Are you probably attempting to communicate with two servlets that are in two different web applications?

If sharing some business functionalities between two servlets in a single web application is all what you wanted, why don't you simply place that centric logic in a method accessible to both servlets?

Roger F. Gay wrote:I'd give you more if I had more. Let me know if you think there's a way to get more.

You should be able to see your stack trace in your container's log file(s). If you are using Tomcat, there are in 'logs' directory.


I'm using an http call in order to getremoteaddr().

I'm not getting the stack trace in the Tomcat log file.

As you can see, I'm checking for lots of errors, and even checking Exception at the end, just in case I forgot something. A timeout isn't really a syntax or other kind of error where it seems a trace might be of interest. Seems like it would indicate that it's just not getting anything back. It's servlet to servlet, so could there be some confusion about where the server thinks the response should go and how the servlet's path must be expressed?
Ayan mallick
Ranch Hand

Joined: Nov 17, 2012
Posts: 46


I have tested for the code you have posted and it's working fine. so the only reason is probably in real scenario you are going to read large chuck of data which is taking time. please check once increasing time limit of this line




Thanks

Ayan
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 396
Ayan mallick wrote:


I have tested for the code you have posted and it's working fine. so the only reason is probably in real scenario you are going to read large chuck of data which is taking time. please check once increasing time limit of this line




Thanks

Ayan


Thanks.
Guess I'll probably have a chance to work on it again this weekend. I tried infinite timeout (0) in response to your message, in its current context but trimmed down so that it didn't actually have to do anything except respond ... just like posted here, and it hung the entire website forever. Couldn't even shut Tomcat down and had to reboot. They want to open the website this week, so I'll have to focus on other things the next few days and we'll open without this particular feature. Then I'll probably take it out of the context of the webapp, see if it works for me that way, and then take it in step by step to see where it breaks.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3177
    
  47

Roger F. Gay wrote:I'm using an http call in order to getremoteaddr().

I am not sure I correctly understand what you mean. Are these two servlets belonging to the same web application, or are they in two different VMs? What are you trying to get from getRemoteAddr()? If these two servlets are deployed on same host, whatever you get with a getRemoteAddr() will be equivalent to 127.0.0.1, i.e. the localhost because the client, which itself is a servlet, is running on the same host. Do you probably want a way to get the IP address of your server or something?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5


As I recall, out,close causes the entire connection to be closed so you should not do it if you want to read the response.

Bill

Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 396
Devaka Cooray wrote:
Roger F. Gay wrote:I'm using an http call in order to getremoteaddr().

I am not sure I correctly understand what you mean. Are these two servlets belonging to the same web application, or are they in two different VMs? What are you trying to get from getRemoteAddr()? If these two servlets are deployed on same host, whatever you get with a getRemoteAddr() will be equivalent to 127.0.0.1, i.e. the localhost because the client, which itself is a servlet, is running on the same host. Do you probably want a way to get the IP address of your server or something?


Yes, I want the public IP address, and I'm getting it using request.getRemoteAddr() even though both servlets are in the same application ... using an http method to contact the other server.
The code above has worked on and off at least as far as triggering the other servlet, and that allowed me to see what it got with getRemoteAddr().
The problem has always been with getting the input stream to get a response back to the first servlet.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

If both servlets are in the same web app, why on earth would you even be using this approach?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3177
    
  47

Roger F. Gay wrote:Yes, I want the public IP address, and I'm getting it using request.getRemoteAddr() even though both servlets are in the same application ...

If getting server IP is all what you want, you can get it through request.getLocalAddr() - there is no need to have a socket approach just to get the local IP of any potential network system I can think of. A socket-based approach may technically work here, but that definitely is a really bad practice and you are going to end up with danged performance issues when you make your application go on production. As Bear and I explained, having a socket connection between two servlets in a single web application is an entirely pointless idea, unless you want to see how worse a bad practice can be.
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 396
Devaka Cooray wrote:
Roger F. Gay wrote:Yes, I want the public IP address, and I'm getting it using request.getRemoteAddr() even though both servlets are in the same application ...

If getting server IP is all what you want, you can get it through request.getLocalAddr() - there is no need to have a socket approach just to get the local IP of any potential network system I can think of. A socket-based approach may technically work here, but that definitely is a really bad practice and you are going to end up with danged performance issues when you make your application go on production. As Bear and I explained, having a socket connection between two servlets in a single web application is an entirely pointless idea, unless you want to see how worse a bad practice can be.


Seriously? I might have started out looking at some misleading examples. I thought that would just give me the local ip.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

You still haven't answered my question. I get the feeling that you are building this:
When all you really need is to walk to the window and open it.

So what is it that you are actually trying to accomplish? Now how, but what.
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 396
Bear Bibeault wrote:You still haven't answered my question. I get the feeling that you are building this:
When all you really need is to walk to the window and open it.

So what is it that you are actually trying to accomplish? Now how, but what.


We're using a dynamic IP. The IP associated with the domain name is not automatically updated. The business owner does not want to pay for dyndns.
It's normally not a serious threat. It really doesn't change very often. But when it does, I want to automatically detect it and get an email with the new IP.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

SO what does that have to do with Servlet to Servlet communications?
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 396
Bear Bibeault wrote:SO what does that have to do with Servlet to Servlet communications?


It has to do with HOW I was doing it. It looked to me like I had to do an http to get outside of the local view to fetch the public IP. And I was using a servlet method (which in itself requires ...)
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 396
I started this a while back, but since the work isn't done yet, it's still of interest. I dove into it earlier, but like I said above, it's not a big threat. The IP rarely changes, so everything else has been a higher priority. I still want to get this job done as some point however. But, it's taken a bit of brain-shaking at this point to remember the details of what I was doing / thinking at the time.

I was planning a servlet to kick off a thread to run a scheduled task to check the current ip (probably once a day ... late at night or something). A servlet because Tomcat starts automatically when the computer is started, and I would have it automatically initialize the servlet. So, if the people at this business turn the computer off for some reason, the whole thing is going to be working again if they just turn it on.

Then of course, there's the question of actually getting the IP address. For that, it appeared to me that I needed to make a fresh HTTP request, which I thought to do by setting up a servlet to check the IP, and used a servlet method to do it.

At the time I wrote the question, I was not actually using the scheduler. I had first tested the servelet method to see if it gave me the proper (public) IP address. The next step I was taking was to call the method from the other servlet.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

The heart of the problem is the user. The words "Penny-wise and Pound-foolish" come to mind.


Customer surveys are for companies who didn't pay proper attention to begin with.
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 396
Would be nice to get a solution working though. The alternative is to call an external site. There are a few around that will return your IP address.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

Roger F. Gay wrote:Would be nice to get a solution working though. The alternative is to call an external site. There are a few around that will return your IP address.


As an academic exercise, yes. When clients cheap out, however, it can be more trouble than it's worth.

The biggest problem with obtaining "your" IP address is that there are more than one of them. You will almost guaranteed have the IP address of 127.0.0.1, which is localhost. For a network-accessible server, you will also have a network IP address, which may or may not be a unique address. You may have multiple addresses, either because the machine has more than one NIC and/or because the NIC has been configured for multiple IPs.

And that's just the beginning. If your server is operating behind a proxy, its LAN IP address won't match the address that the rest of the world sees it as. This can happen in a number of configurations, but the one that would concern you when someone is operating on the cheap would be a cable modem whose external IP address might or might not be globally unique depending on the whims of the Internet Service Provider and possibly on whether or not you paid for a unique (fixed) IP address or not. Meaning, in your case, not.

So there's no one-size-fits-all solution. The most straightforward way to get "your" IP address would be to use the Runtime.exec facility to run your OS's configuration status command (for Linux, that's "ifconfig"; for Windows, it's "IPCONFIG"). Then pull apart the results in a way that matches your hardware environment. It may or may not be the public address.

In cases where your server has a registered DNS name, you'd simply do a DNS lookup and skip all of that.
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 396
I'm only looking for the size that works in the particular environment of this one website. There is only one public IP and no proxy. It's a small business configured much like your home network. One end of a cable attaches to the plug in the wall and the other to a router that finds the local machine. There is a domain name associated with it, but the association to a particular IP is done by hand; i.e. I have to go to the site where the domain was registered and enter the IP address. If the IP changes, you can't get there from here, so to speak, without being on-site. If I'm physically onsite, I can just google quickly for "What's my IP?" and do the reset. But I'm not there on weekends and Monday the business is closed (while it's peak time for the web shop business). Also, there's nothing saying I'll be there forever or never take a day off or a vacation. What I need is an automatic notification that will reach me wherever I am telling me the new IP. Then I can just log into the domain management system from wherever I am in the world and reset.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

You haven't seen my "home network"

Seriously, one of my Internet connections is a business account that runs me $60 a month and does provide a fixed IP. Maybe DNS, too, but since I run my own DNS servers, I haven't checked.

The typical home Internet service is something like a cable router that gets an address assigned by the ISP's DHCP server. Leases typically last 1-2 weeks after which a new IP address may be assigned. It's important to note that often home Internet services EULAs expressly forbid running public servers, although people often do. The problem is that when you take advantage of a loophole like, you risk having the ISP close it without notice and there's not a darned thing you can do about it. Considering the monthly rates for things like smartphone service and cable TV, I don't think $60/month is all that bad for a stable business Internet service. I pay much more than that for my primary (business-only) service and it isn't nearly as fast. The day I cannot afford that much for something as critical as my Internet service is the day I'll know that my business is going under.

Your router is typically going to be a router/switch combination with NAT on it such that the LAN IP address is significantly different from the router's address, although my wireless router is connected to an external switch, instead. You can generally configure the router to set your server's LAN IP address to a fixed IP mapped to the machine's MAC address, which will stabilize at least one part of the network. In cases where the router's external IP is fixed, you can often set the router to proxy selected ports to that fixed address, which stabilizes the whole thing.

One thing I noted was that external location services such as icanhazip.com are generally not reliable. Solutions include keeping a list of servers, or parsing the google response (which may change without notice). For amateur purposes, that's OK, but Murphy's Law says that the time you have a 10-minute delay getting that IP fixed up will be the same time that the biggest potential order of the year tries - and fails - to connect.
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 396
Service providers may do things a little differently in this country (where I am), but the technology is the same. I don't have any problems with my routers etc. and making it to the server internally. And with newer operating systems, I prefer to use computer names, which remain associated with local IPs even if they change. Everything is working as it should. Just need to do this without paying for dyndns service. That's not my choice. The business owner decided not to pay (although I'm not sure he really understands it at all yet). The owners are good people and I just want their system to work well ... not be a problem for them ... as best I can, no matter what they decide to pay for or not. bo

They're really semi-retired and this "business" is right on the border of public service, not calculated to be the most profitable thing he's done in his life. In this first year of operation (most of which had no online sales), they got an award from the local business group for best cultural addition to the community. What they're selling involves a lot of hard work coming up with items that people want or "need" to continue enjoying life ... often more work than prices warrant. So I don't bug him if he wants to keep costs down. I just say, OK I'll fix it and do a bit of extra work myself.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet to Servlet communication can't get inputstream