alwaysAnewbie
Programmer
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:
Servlet code:
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);
}
}