Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie applet/servlet exception problem

Status
Not open for further replies.

alwaysAnewbie

Programmer
Sep 12, 2002
23
US
Background info. WinXP, Java sdk 1.4.2, Eclipse IDE

I have an applet that I am trying to communicate with a servlet. I have compiled both the applet and the servlets without error. When I run the applet I return an exception in the text area defined for the data returned from the servlet. I can't figure out why.

Note: I have pared the servlet code down to what you see to try to figure out the problem.

Applet code:
Code:
/*
 * Created on Jan 14, 2005
 */

/**
 * @author Brian
 */
package Draft;
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class DraftApp extends Applet implements Runnable{
    // Define your thread.
    Thread draftThread;
    // This textfield will show the time.
    TextField clockField;
    TextField getField;
    // Date will give us the current hours, minutes and seconds
    Date date;
    // This variable will remain true for as long
    // we want the thread to run.
    boolean timeleft = true;
    int hours = 0;
    int minutes = 0;
    int seconds = 0;
    String time = "";

    public void init()
    {
       // a standard layout to place just one textfield
       System.out.println("in init");
       setLayout(new BorderLayout());
       Panel west = new Panel();
       west.setLayout(new GridLayout(2,1));
       west.add(new Label("Time Left: ", Label.RIGHT));
       west.add(new Label("Get info: ", Label.RIGHT));
       add("West",west);
       Panel center = new Panel();
       center.setLayout(new GridLayout(2,1));
       clockField = new TextField();
       center.add(clockField);
       getField = new TextField();
       center.add(getField);
       add("Center", center);
       // Create the thread.
       // Check to see if thread still active
       getField.setText(getDraftStatus());
       if (draftThread == null)
       {
           draftThread= new Thread(this);
           // and let it start running
           draftThread.start();
       }
    }

    // Very important. You do not want your thread to keep running when
    // the applet is deactivated (eg. user left page)
    public void destroy()
    {
         // will cause thread to stop looping
         //draftThread.stop();
         timeleft = false;
         // destroy it.
         draftThread = null;
    }


    // The method that will be called when you have a thread.
   // You always need this when you implement Runnable (use a thread)
   public void run()
   {
       // loop until told to stop
       minutes = 2;
       hours = minutes/60;
       time="";
       while (timeleft = true)
       {
           // Construct the current date.
//          date = new Date();
           // Get the hours, minutes and hours
           if (hours<10) time="0"+hours;
            else time=""+hours;
           if (minutes<10) time=time+":0"+minutes;
            else time=time+":"+minutes;
           if (seconds<10) time=time+":0"+seconds;
            else time=time+":"+seconds;
//          String time = hours+":"+minutes+":"+seconds;
           // Put that result in the textfield
           clockField.setText(time);
           //Now the reason for threads
           try
           {
               // Wait 500milliseconds before continuing
               Thread.sleep(1000);
               if (seconds >=1) seconds--;
               else if (minutes>=1) { minutes--; seconds=59; }
                    else if (hours>=1) { hours--; minutes=59;seconds=59; }
                         else timeleft=false; 
               
           }
           catch (InterruptedException e)
           {
               stop();
               System.out.println(e);
           }
          // he has wait and will now restart his actions.
       }
   }
   private String getDraftStatus() 
   {
   	InputStream inputStreamFromServlet = null;
   	try 
       {
           // Construct a URL referring to the servlet
       	   String league_id = "3";
       	   String draftInfo = "";
           URL DraftServlet = new URL(getCodeBase(), "Draft/DraftStatus.class?LeagueID=" + league_id);
//           URL url = new URL("DraftStatus");
           Properties props = new Properties();

           props.setProperty("LeagueID",league_id);

           URLConnection servletConnection = DraftServlet.openConnection(); 
           inputStreamFromServlet = servletConnection.getInputStream();
           
           BufferedReader result  = new BufferedReader(new InputStreamReader(inputStreamFromServlet));
           // Read the first line of the response, which should be
           // a string representation of the current time
           while (result.readLine()!= null)
		   {
           	    System.out.println(result.readLine());
           		draftInfo += result.readLine();
		   }
//           String draftInfo = url;

           // Close the InputStream
           inputStreamFromServlet.close();

           // Return the retrieved time
           return draftInfo;
       }
       catch (Exception e) 
       {
           // If there was a problem, print to System.out
           // (typically the Java console) and return null
           e.printStackTrace();
           return null;
       }
   }

}

Servlet code:
Code:
package Draft;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class DraftStatus extends HttpServlet {
    public void doGet(HttpServletRequest req,HttpServletResponse res)
    throws ServletException, IOException {
    	PrintWriter out = res.getWriter();
    	out.println("Hello World");
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    	throws ServletException, IOException 
	{
    	doGet(request, response);
	}
}
 
I will warn you now, you'd better post the exception you're getting too ... Few are going to want to plough through your code without an exception to guide them.

Tim
 
Darn it. I ploughed through, and there is no exception posted !

--------------------------------------------------
Free Database Connection Pooling Software
 
Sorry guys and thanks for the patience. I thought I put it in the original post.

The only thing that shows in the textarea is the word Exception with a small box after it. I assume that this is a non-printable character. When I debug I get a few bits of human readable code in the draftinfo field in the getDraftStatus method(?) surrounded by boxes and various punctuation marks. It looks like the source of the servlet.

I have tried and tried to get the exception text somehow but because of the non-printable stuff I can't seem to get it out.
 
Is the stack trace not printed out in the Java Console (if in IE, open Tools->Sun Java Console, or something similar).Without knowing the actual exception, it will be hard to solve your problem.

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top