Solved

Applet/Servlet example program problem!

Posted on 2001-06-15
36
304 Views
Last Modified: 2013-11-24
Hi,

I am trying to create an example applet/servlet communication - using an example from a book!  However, the code that I have used keeps throwing a "java.io.FileNotFoundException http://172.16.156.151/servlet/ServerServlet".  Could you please help.

Here is the code I have used and where the line where the exception is thrown:

  private String getDateUsingHttpText()
  {
    try
    {
      URL url = new URL(getCodeBase(), "servlet/ServerServlet");
      URLConnection con = url.openConnection();
      con.setUseCaches(false);
      //Exception thrown on next line!
      InputStream in = con.getInputStream();
      DataInputStream result = new DataInputStream(new BufferedInputStream(in));
      String date = result.readLine();
      in.close();
      return date;
    }
    catch(Exception ee)
    {
      displayMessage(ee.toString());
      return "unavailable";
    }
  }

If I type in:

http://172.16.156.151/servlet/ServerServlet

I get the response from the servlet - the date as a String.

Please help!

Thanks in advance!

David.
0
Comment
Question by:david_d
  • 19
  • 10
  • 4
  • +1
36 Comments
 
LVL 7

Expert Comment

by:Sasha_Mapa
Comment Utility
Well, I tried accessing your servlet and I can't connect...

The code you posted seems to be ok.

Sasha Maryanovsky.
0
 
LVL 1

Author Comment

by:david_d
Comment Utility
Sasha,

Thanks for your quick reply, but you cannot access it I am behind a firewall so only I and the PC's around me can access it!

Do you know of any examples that I could try?  and where I could get them from?

Many Thanks

David.
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
Comment Utility
Could you post your servlet code?

Sasha Maryanovsky.
0
 
LVL 1

Author Comment

by:david_d
Comment Utility
Sasha,

Here is the code, I have just typed it in by hand!

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServerServlet extends HttpServlet
{
  publiic Date getDate()
  {
    return new Date();
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();
    out.println(getDate().toString());
  }

  public void doPost(HttpServletRequest req, HttpServlerResponse res)
    throws ServlerException, IOException
  {
    doGet(req, res);
  }
}

I should be just obtaining the time from the server!  Why would a file not found exception be thrown in such circumstances?

Thanks so far.

David.

0
 
LVL 1

Author Comment

by:david_d
Comment Utility
Sasha,

Here is the code, I have just typed it in by hand!

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServerServlet extends HttpServlet
{
  publiic Date getDate()
  {
    return new Date();
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();
    out.println(getDate().toString());
  }

  public void doPost(HttpServletRequest req, HttpServlerResponse res)
    throws ServlerException, IOException
  {
    doGet(req, res);
  }
}

I should be just obtaining the time from the server!  Why would a file not found exception be thrown in such circumstances?

Thanks so far.

David.

0
 
LVL 1

Author Comment

by:david_d
Comment Utility
Sasha,

Here is the code, I have just typed it in by hand!

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServerServlet extends HttpServlet
{
  publiic Date getDate()
  {
    return new Date();
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();
    out.println(getDate().toString());
  }

  public void doPost(HttpServletRequest req, HttpServlerResponse res)
    throws ServlerException, IOException
  {
    doGet(req, res);
  }
}

I should be just obtaining the time from the server!  Why would a file not found exception be thrown in such circumstances?

Thanks so far.

David.

0
 
LVL 1

Author Comment

by:david_d
Comment Utility
Sasha,

Here is the code, I have just typed it in by hand!

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServerServlet extends HttpServlet
{
  publiic Date getDate()
  {
    return new Date();
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();
    out.println(getDate().toString());
  }

  public void doPost(HttpServletRequest req, HttpServlerResponse res)
    throws ServlerException, IOException
  {
    doGet(req, res);
  }
}

I should be just obtaining the time from the server!  Why would a file not found exception be thrown in such circumstances?

Thanks so far.

David.

0
 
LVL 1

Author Comment

by:david_d
Comment Utility
Sasha,

Here is the code, I have just typed it in by hand!

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServerServlet extends HttpServlet
{
  publiic Date getDate()
  {
    return new Date();
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();
    out.println(getDate().toString());
  }

  public void doPost(HttpServletRequest req, HttpServlerResponse res)
    throws ServlerException, IOException
  {
    doGet(req, res);
  }
}

I should be just obtaining the time from the server!  Why would a file not found exception be thrown in such circumstances?

Thanks so far.

David.

0
 
LVL 1

Author Comment

by:david_d
Comment Utility
Sasha,

Here is the code, I have just typed it in by hand!

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServerServlet extends HttpServlet
{
  publiic Date getDate()
  {
    return new Date();
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();
    out.println(getDate().toString());
  }

  public void doPost(HttpServletRequest req, HttpServlerResponse res)
    throws ServlerException, IOException
  {
    doGet(req, res);
  }
}

I should be just obtaining the time from the server!  Why would a file not found exception be thrown in such circumstances?

Thanks so far.

David.

0
 
LVL 1

Author Comment

by:david_d
Comment Utility
Sasha,

Here is the code, I have just typed it in by hand!

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServerServlet extends HttpServlet
{
  publiic Date getDate()
  {
    return new Date();
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();
    out.println(getDate().toString());
  }

  public void doPost(HttpServletRequest req, HttpServlerResponse res)
    throws ServlerException, IOException
  {
    doGet(req, res);
  }
}

I should be just obtaining the time from the server!  Why would a file not found exception be thrown in such circumstances?

Thanks so far.

David.

0
 
LVL 1

Author Comment

by:david_d
Comment Utility
Sasha,

Here is the code, I have just typed it in by hand!

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServerServlet extends HttpServlet
{
  publiic Date getDate()
  {
    return new Date();
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();
    out.println(getDate().toString());
  }

  public void doPost(HttpServletRequest req, HttpServlerResponse res)
    throws ServlerException, IOException
  {
    doGet(req, res);
  }
}

I should be just obtaining the time from the server!  Why would a file not found exception be thrown in such circumstances?

Thanks so far.

David.

0
 
LVL 1

Author Comment

by:david_d
Comment Utility
Sasha,

Here is the code, I have just typed it in by hand!

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServerServlet extends HttpServlet
{
  publiic Date getDate()
  {
    return new Date();
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();
    out.println(getDate().toString());
  }

  public void doPost(HttpServletRequest req, HttpServlerResponse res)
    throws ServlerException, IOException
  {
    doGet(req, res);
  }
}

I should be just obtaining the time from the server!  Why would a file not found exception be thrown in such circumstances?

Thanks so far.

David.

0
 
LVL 1

Author Comment

by:david_d
Comment Utility
Sasha,

Here is the code, I have just typed it in by hand!

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServerServlet extends HttpServlet
{
  publiic Date getDate()
  {
    return new Date();
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();
    out.println(getDate().toString());
  }

  public void doPost(HttpServletRequest req, HttpServlerResponse res)
    throws ServlerException, IOException
  {
    doGet(req, res);
  }
}

I should be just obtaining the time from the server!  Why would a file not found exception be thrown in such circumstances?

Thanks so far.

David.

0
 
LVL 7

Expert Comment

by:Sasha_Mapa
Comment Utility
Ahhh - stop refreshing!!

Sasha Maryanovsky.
0
 
LVL 1

Author Comment

by:david_d
Comment Utility
No!  I had a problrm submitting the data!
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
Comment Utility
Anyway, I'm not really a servlet expert, so I probably can't help you - the only thing I can think of is that you are connected to the web through a proxy, which doesn't have access through your firewall... This would prevent you from accessing your servlet through the proxy.

Sasha Maryanovsky.
0
 
LVL 1

Author Comment

by:david_d
Comment Utility
I have the web servre on the same machine as my PC!  I am using the apache web server!

David.
0
 
LVL 2

Expert Comment

by:iDeb
Comment Utility
call out.flush() after out.println() in your code....that seems to be the problem......
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:iDeb
Comment Utility
the other possibility is your servlet engine and apache is not configured correctly.....that is probably why you get a file not found exception.....in that case when you type your URL in a browser, you also get a not found page.....which servlet engine are you using?
0
 
LVL 2

Expert Comment

by:iDeb
Comment Utility
if you are using tomcat try http://172.16.156.151:8080/servlet/ServerServlet
instead of
http://172.16.156.151/servlet/ServerServlet
.....in the situation your servlet engine is not configured with apache correctly, and tomcat is running then the former should work
0
 
LVL 1

Author Comment

by:david_d
Comment Utility
Hi,

The out.flush() is placed in the Servlet class still causes the error!  I am using JServ as the servlet engine.  I think this is configured correctly because if I type from the web browser:

http://172.16.156.151/servlet/ServerServlet

I get a web page displaying the correct time!!

Do you have any other ideas? or possibilities?

David.
0
 
LVL 1

Author Comment

by:david_d
Comment Utility
I think it is the client problem because the applet is not even finding the servlet!  I have placed log messages in the servlet and they are not being displayed therefore they are not being called!  I have also tried calling the file ServerServlet.class from the client and again no luck!

I looked at the Java documentation for what is the FileNotFoundException and it is thrown by the FileInputStream, FileOutputStream and RandomAccessFile constructors when a file with a specified pathname does not exist! Or it may not be accessible.  But it is accessible when typed:

http://172.16.156.151/servlet/ServerServlet

and the current date is displayed in the web browser!

If it is a JServ problem what would you recommend?

Many thanks

David
0
 

Expert Comment

by:sontnvn
Comment Utility
Dir david,
i think your URL were wrong.
instead :
URL url = new URL(getCodeBase),"servlet/ServerServlet");
you shoud try:
URL url = new
URL("http://172.16.156.151/servlet/ServerServlet");
PS : you shoud System.out this string to see the URL
System.out.println(getCodeBase).toString()+ "servlet/ServerServlet")
Bye good luck
0
 

Expert Comment

by:sontnvn
Comment Utility
Dir david,
i think your URL were wrong.
instead :
URL url = new URL(getCodeBase),"servlet/ServerServlet");
you shoud try:
URL url = new
URL("http://172.16.156.151/servlet/ServerServlet");

Or
URL url =
new (getCodeBase().getHost() +
     getCodeBase().getPort() + "servlet/ServerServlet")

PS : you shoud System.out this string to see the URL
System.out.println(getCodeBase).toString()+ "servlet/ServerServlet")

Bye good luck
0
 

Expert Comment

by:sontnvn
Comment Utility
Dir david,

Note : /servlet/ServerServlet

i think your URL were wrong.
instead :
URL url = new URL(getCodeBase),"servlet/ServerServlet");
you shoud try:
URL url = new
URL("http://172.16.156.151/servlet/ServerServlet");

Or
URL url =
new (getCodeBase().getHost() +
    getCodeBase().getPort() + "/servlet/ServerServlet");

PS : you shoud System.out this string to see the URL
System.out.println(getCodeBase).toString()+ "servlet/ServerServlet")

Bye good luck
0
 
LVL 2

Expert Comment

by:iDeb
Comment Utility
Do you have authenticated access to the Servlet, does the browser prompt for authentication.....if so it is possible you need to include username:password in the URL.....i use tomcat and haven't ever used JRun...
0
 
LVL 1

Author Comment

by:david_d
Comment Utility
Hi,

Sorry for not getting back to you sooner, but I have been away!

sontnvn, I have tried what you have proposed but the problem still remains!  Whether I try using the codebase or without the problem still exists.

iDeb, my server does not require any kind of authentication.

The problem is that the servlet is throwing the java.io.filenotfoundexception

http://172.16.156.151/servlet/ServerServlet

This could be because the client is looking for a a file called ServerServlet.class?

What do you think?

Thanks for your comments so far!

David.
0
 
LVL 2

Expert Comment

by:iDeb
Comment Utility
I think i know what the problem is.....oversight....
do a con.connect(); after creating con....
0
 
LVL 1

Author Comment

by:david_d
Comment Utility
Hi,

iDeb, your comment now produces no error message but the program still does not work!!

When I press the button to obtain the time, it does nothing - what I mean by this is that if I press and leave the cursor over the button the button colour is dark grey (it is usually light grey!)  If I move the mouse away the button colour goes back to light grey!

Thanks so far!

David.
0
 
LVL 2

Accepted Solution

by:
iDeb earned 60 total points
Comment Utility
try this.....

          try {
                       URL url = new URL(getCodeBase(), "servlet/ServerServlet");
                       URLConnection con = url.openConnection();
                        con.setUseCaches(false);
               connection.connect();

               BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                        String date = in.readLine();
          }
          catch (IOException e) {
               System.out.println("Error:" + e);
          }
0
 
LVL 2

Expert Comment

by:iDeb
Comment Utility
use con.connect()
instead of connection.connect();
0
 
LVL 1

Author Comment

by:david_d
Comment Utility
I have been looking at other people's questions and URLConnection has been placed with HttpURLConnection - would this make any difference as I am using the apache?

David.
0
 
LVL 2

Expert Comment

by:iDeb
Comment Utility
i don't think that makes any difference......could you try the code segment i sent in the earlier response?
0
 
LVL 1

Author Comment

by:david_d
Comment Utility
iDeb,

Hi, the code solution that you gave me works!  What is the difference between the code that I have and the code that you have?  It looks uncanny!

Thank you very much!

David.
0
 
LVL 2

Expert Comment

by:iDeb
Comment Utility
The difference on the outset seems to be the use of a Reader class to read data from an input stream....i think a good place to understand it would be http://java.sun.com/docs/books/tutorial/essential/io/overview.html

good luck
0
 
LVL 2

Expert Comment

by:iDeb
Comment Utility
just an extract from the above url

Understanding the I/O Superclasses
Reader and InputStream define similar APIs but for different data types. For example, Reader contains these methods for reading characters and arrays of characters:
int read()
int read(char cbuf[])
int read(char cbuf[], int offset, int length)

InputStream defines the same methods but for reading bytes and arrays of bytes:
int read()
int read(byte cbuf[])
int read(byte cbuf[], int offset, int length)

Also, both Reader and InputStream provide methods for marking a location in the stream, skipping input, and resetting the current position.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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.
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

743 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

16 Experts available now in Live!

Get 1:1 Help Now