• Post Reply Bookmark Topic Watch Topic
  • New Topic

Catching IOException for Servlet Call from java.net.URL.openStream

 
giridhar challa
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am making a URL call to a Servlet. I am throwing an IOException from the Servlet. But the client code never getting into the exception block. I printed the response got from Servlet which is 500 error. Even I am explicitly throwing IOException from servelt class why it is not falling into client side exception block?

The client code:


String strUrl = http://localhost:7001/testapp/test1servlet;

try
{


URL link = new URL(strUrl);

long start = System.currentTimeMillis();
BufferedReader in = new BufferedReader( new InputStreamReader(link.openStream()));

while (in.readLine()!=null)
logger.info("in.readLine()-->"+in.readLine());

long respTime = System.currentTimeMillis() - start;
}
catch(IOException ioe)
{
logger.error("Exception occured while Accessing the link:"+strUrl);
}

The servlet Code


public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
handleRequest(request, response);
}

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


private void handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
long start = System.currentTimeMillis();

if(true)

throw new IOException("tag");
catch(IOException ex)
{
logger.error("IOException - ContentMappingServlet handleRequest(): " + ex.getMessage(), ex);

if(ex.getMessage().equalsIgnoreCase("tag"))
throw new IOException("tag");
}

catch(Exception ex)
{
logger.error("Exception - ContentMappingServlet handleRequest(): " + ex.getMessage(), ex);
if(ex.getMessage().equalsIgnoreCase("tag"))
throw new IOException("tag");

}
logger.info("Exiting handleRequest() in " + respTime);
}


The client side logic is not falling in IOException block.

the response i printed is showing like this...

####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()--><HTML>>
####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()--><TITLE>Error 500--Internal Server Error</TITLE>>
####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()--></HEAD>>
####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()-->
>
####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()--><H2>Error 500--Internal Server Error</H2>>
####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()--></table>>
####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()--> at com.nscorp.uportal.nscorp.servlet.ContentMappingServlet.handleRequest(ContentMappingServlet.java:221)>
####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()--> at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)>
####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()--> at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)>
####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()--> at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:283)>
####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()--> at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)>
####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()--> at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)>
####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()--> at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)>
####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()--> at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)>
####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()--> at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)>
####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()--> at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2002)>
####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()--> at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1362)>
####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()--> at weblogic.work.ExecuteThread.run(ExecuteThread.java:181)>
####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()--></table>>
####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()--></BODY>>
####<Aug 31, 2010 1:10:55 PM EDT> <in.readLine()-->null>


Any idea about the handling of IOException from a URL openStream() for a servlet call?

Please help me to understand the concept. thank you.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information.

Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.

Also, what is a "client side exception block"?
 
giridhar challa
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thanks very much Bear Bibeault. I will follow your comments while adding code. The above block is the client side exception handling block. Even we are explicitly throwing IOException from servelt the control never falling inside client side IOException block.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An exception thrown on the server must be handled in the JVM on the server, you cant just toss it to the client.

Normal practice is to try to create a meaningful error message to be logged and also sent to the client.

Consider using HTTP status codes if your client code is creating a HttpURLConnection to make a request.

Bill
 
giridhar challa
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you William Brogden. I will try with HttpUrlConnection.

My client code and server code both pointing to the same JVM. That's the reason I am expecting the exception to be caught by the client code once it is thrown from servlet. Is this not the correct approach? I would like to understand the basic concepts. Thank you.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
giridhar challa wrote:My client code and server code both pointing to the same JVM. That's the reason I am expecting the exception to be caught by the client code once it is thrown from servlet. Is this not the correct approach? I would like to understand the basic concepts. Thank you.

That is not the correct approach. It wouldn't make any sense to expect an arbitrary client to be able to see what's happening on the server side. And just because they're both "pointing at" the same JVM wouldn't mean it's the same *instance* of the JVM, or that there would be any direct connection between the different threads of execution.
 
giridhar challa
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David Newton. I mentioned that both server and client are pointing to the same JVM. I did like this. I created a servlet for the serverside code. I created another servlet where i am instantiating the client side logic class and invoking using the url. So there are 2 servlets. Both Servlets are deployed in same web application so I thought both are running in the same JVM. If that is the case the client code can be capable of handling the exception thrown by server side servlet right? Am I thinking wrong here? Please help me to understand the concepts.

Anyway i coded using HttpURLConnection to complete the logic. But I would like to understand the concepts. Thanks ALL for your valuable explanation.
 
giridhar challa
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I noticed the container is swallowing the exception we are throwing back(from the server side servlet). It is never reaching the client as exception so client is reading like a normal response. Does this the way containers implement HTTP objects? If we use HttpURLConnection with HTTP codes everything works as per the logic. thank you.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like I said, you're thinking about it wrong. Clients don't get exceptions thrown by the server. And again, just because it's the same executable doesn't mean it's (a) the same instance, and even if it was, (b) that information like that is shared across threads.
 
giridhar challa
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David Newton. The reason could be point (b) mentioned by you in this scenario.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it's most likely point (a). Unless you're starting up the client from the server, it's going to spin up a new Java.
 
giridhar challa
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I am starting the client also from the same server by using another servlet.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's... weird. In any case, you're just thinking about exceptions all wrong.
 
giridhar challa
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just testing the logic from another servlet instead of trying with new app and ran into this issue. any way thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!