Solved

Applet/Servlet example program problem!

Posted on 2001-06-15
36
342 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
[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
  • 19
  • 10
  • 4
  • +1
36 Comments
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 6195759
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
ID: 6195898
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
ID: 6195938
Could you post your servlet code?

Sasha Maryanovsky.
0
Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

 
LVL 1

Author Comment

by:david_d
ID: 6195983
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
ID: 6195984
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
ID: 6195985
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
ID: 6195986
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
ID: 6195987
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
ID: 6195988
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
ID: 6195989
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
ID: 6195990
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
ID: 6195991
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
ID: 6195993
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
ID: 6196028
Ahhh - stop refreshing!!

Sasha Maryanovsky.
0
 
LVL 1

Author Comment

by:david_d
ID: 6196041
No!  I had a problrm submitting the data!
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 6196061
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
ID: 6196324
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
ID: 6196502
call out.flush() after out.println() in your code....that seems to be the problem......
0
 
LVL 2

Expert Comment

by:iDeb
ID: 6196511
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
ID: 6196519
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
ID: 6197690
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
ID: 6197704
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
ID: 6200873
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
ID: 6201153
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
ID: 6201161
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
ID: 6204243
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
ID: 6222514
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
ID: 6229179
I think i know what the problem is.....oversight....
do a con.connect(); after creating con....
0
 
LVL 1

Author Comment

by:david_d
ID: 6230723
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
ID: 6232366
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
ID: 6232370
use con.connect()
instead of connection.connect();
0
 
LVL 1

Author Comment

by:david_d
ID: 6233441
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
ID: 6234832
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
ID: 6236279
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
ID: 6239437
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
ID: 6239442
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

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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 …
Suggested Courses

617 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