By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,247 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,247 IT Pros & Developers. It's quick & easy.

Applet Not Loaded

P: 1
WHEN I RUN THIS WEB APPLICATION ON Tomcat5.5.9 SERVER
MY HttpChatApplet sccessfully Loaded from ChatDispatch but

running on some another PC HttpChatApplet not loaded


my Coad

ChatDispatch.java

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

public class ChatDispatch extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
res.setContentType("text/html");

if (!req.getParameterNames().hasMoreElements()) {
// There were no request parameters. Print a welcome page.
printWelcomePage(req, res);
}
else {
// There was at least one request parameter.
// Print a page containing the applet.
printAppletPage(req, res);
}
}

// The welcome page greets the reader and has a form where the user
// can choose an applet-servlet communication method.
private void printWelcomePage(HttpServletRequest req,HttpServletResponse res)throws IOException {
PrintWriter out = res.getWriter();
String me = req.getServletPath();
String user=req.getParameter("USER_NAME");
String user_type=req.getParameter("USER_TYPE");

out.println("<HTML>");
out.println("<HEAD><TITLE>");
out.println("ChatServer1.2");
out.println("</TITLE></HEAD>");
out.println();
out.println("<BODY>");
out.println("<H1>ChatServer</H1>");
out.println();


out.println("<UL>");

out.println(" <LI><A HREF=\"" + me + "?method=http\">http</A>");
out.println(" <LI><A HREF=\"" + me + "?method=socket\">socket</A>");
out.println(" <LI><A HREF=\"" + me + "?method=rmi\">rmi</A>");


out.println("</UL>");



out.println("</BODY></HTML>");
}

// The applet page displays the chat applet.
private void printAppletPage(HttpServletRequest req,HttpServletResponse res) throws IOException {
PrintWriter out = res.getWriter();

out.println("<HTML>");
out.println("<HEAD><TITLE>Join Chatserver 1.2</TITLE></HEAD>");
out.println("<BODY>");
out.println("<H1>ChatServer</H1>");

String method = req.getParameter("method");
//String user = req.getRemoteUser();
String user = "anonynous";
String applet = null;

if ("http".equals(method)) {
applet = "HttpChatApplet";
}
else if ("socket".equals(method)) {
applet = "SocketChatApplet";
}
else if ("rmi".equals(method)) {
applet = "RMIChatApplet";
}
else {
// No method given, or an invalid method given.
// Explain to the user what we expect.
out.println("Sorry, this servlet requires a <TT>method</TT> " +
"parameter with one of these values: " +
"http, socket, rmi");
return;
}

// Print the HTML code to generate the applet.
// Choose the applet code based on the method parameter.
// Provide a user parameter if we know the remote user.
out.println("<APPLET CODE=" + applet + " CODEBASE=/classes " +"WIDTH=500 HEIGHT=170>");

//if (user != null)
out.println("<PARAM NAME=user VALUE=\"" + user + "\">");
out.println("</APPLET>");

out.println("</BODY></HTML>");
}
}


HttpChatApplet.java


import java.applet.*;

import java.awt.*;
import java.awt.Component;
import java.awt.TextComponent;
import java.awt.TextArea;
import java.awt.Label;
import java.awt.TextField;

import java.io.*;
import java.net.*;
import java.util.*;

import com.oreilly.servlet.HttpMessage;

public class HttpChatApplet extends Applet implements Runnable {

TextArea text;
//Label label;
TextField input;
/*
Thread thread;
*/

private volatile Thread thread;


String user;

public void init() {
// Check if this applet was loaded directly from the filesystem.
// If so, explain to the user that this applet needs to be loaded
// from a server in order to communicate with that server's servlets.
//URL codebase = getCodeBase();
/* if (!"http".equals(codebase.getProtocol())) {
System.out.println();
System.out.println("*** Whoops! ***");
System.out.println("This applet must be loaded from a web server.");
System.out.println("Please try again, this time fetching the HTML");
System.out.println("file containing this servlet as");
System.out.println("\"http://server:port/file.html\".");
System.out.println();
System.exit(1); // Works only from appletviewer
// Browsers throw an exception and muddle on
}*/

// Get this user's name from an applet parameter set by the servlet
// We could just ask the user, but this demonstrates a
// form of servlet->applet communication.
user = getParameter("user");
if (user == null) user = "anonymous";

// Set up the user interface...
// On top, a large TextArea showing what everyone's saying.
// Underneath, a labeled TextField to accept this user's input.

Label label = new Label("Say something:");
label.getText();
label.setText("Say something:");

input = new TextField(17);
input.setEditable(true);



// text = new TextArea();
// text.setEditable(false);



setLayout(new BorderLayout());
Panel panel = new Panel();
panel.setLayout(new BorderLayout());

add("Center", text);
add("South", panel);

panel.add("West", label);
panel.add("Center", input);
}

public void start() {
thread = new Thread(this);
thread.start();
}

String getNextMessage() {
String nextMessage = null;
while (nextMessage == null) {
try {
// URL url = new URL(getCodeBase(), "/servlet/ChatServlet");
URL url = new URL("http://localhost:8080/servlet/ChatServlet");
HttpMessage msg = new HttpMessage(url);
InputStream in = msg.sendGetMessage();
// DataInputStream data = new DataInputStream(new BufferedInputStream(in));
BufferedReader data= new BufferedReader(new InputStreamReader(in));
nextMessage = data.readLine();
}
catch (SocketException e) {
// Can't connect to host, report it and wait before trying again
System.out.println("Can't connect to host: " + e.getMessage());
try { Thread.sleep(5000); } catch (InterruptedException ignored) { }
}
catch (FileNotFoundException e) {
// Servlet doesn't exist, report it and wait before trying again
System.out.println("Resource not found: " + e.getMessage());
try { Thread.sleep(5000); } catch (InterruptedException ignored) { }
}
catch (Exception e) {
// Some other problem, report it and wait before trying again
System.out.println("General exception: " +
e.getClass().getName() + ": " + e.getMessage());
try { Thread.sleep(1000); } catch (InterruptedException ignored) { }
}
}
return nextMessage + "\n";
}
/*
public void run() {
while (true) {
text.appendText(getNextMessage());
}
}

public void stop() {
thread.stop();
thread = null;
}
*/
public void run() {
Thread thisThread = Thread.currentThread();
while (thread == thisThread) {
try {
thisThread.sleep(1000);
} catch (InterruptedException e){
}
repaint();
}
}

public void stop() {
thread = null;
}




void broadcastMessage(String message) {
message = user + ": " + message; // Pre-pend the speaker's name
try {
//URL url = new URL(getCodeBase(), "/servlet/ChatServlet");
URL url = new URL("http://localhost:8080/servlet/ChatServlet");

HttpMessage msg = new HttpMessage(url);
Properties props = new Properties();
props.put("message", message);
msg.sendPostMessage(props);
}
catch (SocketException e) {
// Can't connect to host, report it and abandon the broadcast
System.out.println("Can't connect to host: " + e.getMessage());
}
catch (FileNotFoundException e) {
// Servlet doesn't exist, report it and abandon the broadcast
System.out.println("Resource not found: " + e.getMessage());
}
catch (Exception e) {
// Some other problem, report it and abandon the broadcast
System.out.println("General exception: " +
e.getClass().getName() + ": " + e.getMessage());
}
}

public boolean handleEvent(Event event) {
switch (event.id) {
case Event.ACTION_EVENT:
if (event.target == input) {
broadcastMessage(input.getText());
input.setText("");
return true;
}
}
return false;
}
}


HELP PLZ

ANKUR RAHVANSHI
Mar 21 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.