Solved

UnknowHostException with Servlet

Posted on 2004-09-01
11
2,955 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

830 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