Solved

UnknowHostException with Servlet

Posted on 2004-09-01
11
2,957 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses

630 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