Solved

UnknowHostException with Servlet

Posted on 2004-09-01
11
2,952 Views
Last Modified: 2013-11-24
Consider the following code (from javaalmanac, a bit modified):

import java.io.*;
import java.net.*;

public class URLTest
{
      public static void main(String [] arguments)
      {
      
            try
            {
                  System.out.println("setting properties...");
                  System.getProperties().put("http.proxySet", "true");
            System.getProperties().put("http.proxyHost", "192.168.6.92");
            System.getProperties().put("http.proxyPort", "8080");

                  // Create a URL for the desired page
                  URL url = new URL("http://www.yahoo.com");
                  System.out.println("URL object created...");
                  // Read all the text returned by the server
                  BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                  System.out.println("input from url obtained...");
                  String str;
                  while ((str = in.readLine()) != null)
                  {
                        System.out.println("line: " + str);
                  }
                  in.close();
            }
            catch (MalformedURLException e)
            {
                  System.out.println(e);
            }
            catch (IOException e)
            {
                  System.out.println(e);
            }
      }
}

This runs fine as a standalone application, no problems at all. However when I try to run it from a servlet (I literaly copy/paste the try...catch block) I am getting an UnknownHostException in the following line:

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

obviously when it tries to open the stream.

Any thoughts on how to resolve it?

Specifications:

Linux RedHat 9.0
WebLogic Server 8.1
JDK1.4.1_05

0
Comment
Question by:girionis
  • 5
  • 3
  • 3
11 Comments
 
LVL 18

Expert Comment

by:armoghan
ID: 11951423
0
 
LVL 35

Author Comment

by:girionis
ID: 11951499
I am still getting the same error message with the code in the above link:

java.net.UnknownHostException: www.yahoo.com
        at java.net.InetAddress.getAllByName0(InetAddress.java:1004)
        at java.net.InetAddress.getAllByName0(InetAddress.java:969)
        at java.net.InetAddress.getAllByName(InetAddress.java:963)
        at weblogic.net.http.HttpClient.openServer(HttpClient.java:263)
        at weblogic.net.http.HttpClient.openServer(HttpClient.java:327)
        at weblogic.net.http.HttpClient.<init>(HttpClient.java:128)
        at weblogic.net.http.HttpURLConnection.getHttpClient(HttpURLConnection.java:127)
        at weblogic.net.http.HttpURLConnection.getInputStream(HttpURLConnection.java:329)
        at java.net.URL.openStream(URL.java:960)
        at smswebapp.SMSServlet.processRequest(SMSServlet.java:39)
        at smswebapp.SMSServlet.doGet(SMSServlet.java:19)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:971)
        at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:402)
        at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
        at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6350)
        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)
        at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:118)
        at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3635)
        at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2585)
        at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
        at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)


LIne 39 is this one:

BufferedReader in = new BufferedReader(new InputStreamReader(yahoo.openStream()));
0
 
LVL 35

Author Comment

by:girionis
ID: 11951596
It does not even run as a JSP.
0
 
LVL 18

Expert Comment

by:armoghan
ID: 11951626
i have removed these lines from my servlet and it is working fine with me

//   System.getProperties().put("http.proxySet", "true");
//        System.getProperties().put("http.proxyHost", "192.168.6.92");
//        System.getProperties().put("http.proxyPort", "8080");

0
 
LVL 18

Accepted Solution

by:
armoghan earned 400 total points
ID: 11951639
I am using Jboss with Windows Xp

Probably there is some problem your http settings
You need to check on that
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 35

Author Comment

by:girionis
ID: 11951652
I am behind a firewall, I need to have those. But I have also tried it without those, still the same problems. What app server are you using? Maybe it has got something to do with WebLogic. I am going to try Tomcat and see if it is any different.
0
 
LVL 20

Expert Comment

by:Venabili
ID: 11951815
Is the proxy host set correctly?
0
 
LVL 20

Assisted Solution

by:Venabili
Venabili earned 100 total points
ID: 11951834
Seems like it is actually but for some reason it is not recognised. Any luck with the tomcat?
0
 
LVL 20

Expert Comment

by:Venabili
ID: 11951884
0
 
LVL 35

Author Comment

by:girionis
ID: 11951923
I am not using Eclipse but I am compiling my servlet from command prompt. I think it has got something to do with WLS settings. I am trying to get Tomcat run my servlet atm so I will post any updates soon.
0
 
LVL 35

Author Comment

by:girionis
ID: 11952112
It works with Tomcat, weird. This means that there are some settings issues with WLS, maybe it does not recognize the proxy settings as Venabili said. I will ask a separate question to WebLogic category for this.

Thank you all for your help :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

930 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now