2

I am using the code below to invoke a servlet from my Java class :

  URL url = new URL( "http://localhost:7001/Socket-war/Servlet" );
  new BufferedReader(new InputStreamReader(url.openStream()));  

Getting an Exception:

java.io.FileNotFoundException: Response: '404: Not Found' for url: 'http://localhost:7001/Socket-war/Servlet'  

Everything is fine; I have used it before as well in my other Program; but now raising exception...

Somebody tell me WHY ??? Any advice or suggestion would be highly appreciable.

Thank you!!!

Jenz
  • 8,280
  • 7
  • 44
  • 77
Developer
  • 161
  • 2
  • 3
  • 15
  • Can you reach the servlet from your browser? – Hirak May 06 '14 at 05:02
  • Yes, using Browser I can. – Developer May 06 '14 at 05:05
  • can you try 127.0.0.1 instead of localhost? – Hirak May 06 '14 at 05:06
  • Also, are there any proxy setup in your network? – Hirak May 06 '14 at 05:07
  • Getting Exception again: java.io.FileNotFoundException: Response: '404: Not Found' for url: 'http://127.0.0.1:7001/Socket-war/Servlet' at weblogic.net.http.HttpURLConnection.getInputStream(HttpURLConnection.java:568) at weblogic.net.http.HttpURLConnection.getInputStream(HttpURLConnection.java:568) at weblogic.net.http.SOAPHttpURLConnection.getInputStream(SOAPHttpURLConnection.java:37) at java.net.URL.openStream(URL.java:1037) – Developer May 06 '14 at 05:10
  • you didnot put http:// before 127... ? – Hirak May 06 '14 at 05:13
  • Yes there is Proxy setup – Developer May 06 '14 at 05:15
  • Exception: java.io.FileNotFoundException: Response: '404: Not Found' for url: 'http://127.0.0.1:7001/Socket-war/Servlet' at weblogic.net.http.HttpURLConnection.getInputStream(HttpURLConnection.java:568) at weblogic.net.http.HttpURLConnection.getInputStream(HttpURLConnection.java:568) at weblogic.net.http.SOAPHttpURLConnection.getInputStream(SOAPHttpURLConnection.java:37) at java.net.URL.openStream(URL.java:1037) – Developer May 06 '14 at 05:17
  • Proxy shouldn't affect the localhost servlet connection. Can you provide a sample servlet and how you are connecting it? We would like to run that in our machine – Hirak May 06 '14 at 05:18
  • Its a simple Servlet Sir with Hello World Code; I need to invoke it as a part of a complete Processing cycle of my Project; can't provide the whole code but it is as simple as it sounds... – Developer May 06 '14 at 05:26
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/52071/discussion-between-hirak-and-user3462765) – Hirak May 06 '14 at 05:28
  • But, yes My Project cycle starts with a Java class which is serving as a JMS Message Producer in which I set some Initial Context Parameters by using code: Hashtable ht = new Hashtable(); ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory"); ht.put(Context.PROVIDER_URL, "t3://localhost:7001"); ctx = new InitialContext(ht); ::::::::::: May be this has got some issue ??? – Developer May 06 '14 at 05:30

1 Answers1

0

This answer is in response to the chat happened.

Following is my servlet code

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          response.getWriter().println("Hello World");
            response.getWriter().flush();
            response.getWriter().close();
    }

Following is my connection code, and it works.

public static void main(String[] args) throws IOException {
    System.setProperty("http.proxyHost", "proxy.***.com");
    System.setProperty("http.proxyPort", "####");
     Authenticator authenticator = new Authenticator() {
                    public PasswordAuthentication getPasswordAuthentication() {
                        return (new PasswordAuthentication("user",
                                "pwd".toCharArray()));
                    }
                };
    Authenticator.setDefault(authenticator);
     URL url = new URL( "http://localhost:8080/ImageCanvas/Servlet2" );
     BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
     String line;
     while((line=br.readLine())!= null){
         System.out.println(line);
         System.out.flush();
     }
     br.close();

}
Hirak
  • 3,601
  • 1
  • 22
  • 33
  • Yes I am trying to do the same but, yes My Project cycle starts with a Java class which is serving as a JMS Message Producer in which I set some Initial Context Parameters by using code: Hashtable ht = new Hashtable(); ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory"); ht.put(Context.PROVIDER_URL, "t3://localhost:7001"); ctx = new InitialContext(ht); ::::::::::: May be this has got some issue ??? – Developer May 06 '14 at 05:41