Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

UnknowHostException with Servlet

Posted on 2004-09-01
11
Medium Priority
?
2,958 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
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 …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

688 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