Brian K Swingle

Ranch Hand
+ Follow
since Jun 20, 2003
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Brian K Swingle

Hello Bear,
Thanks for answering. What i'm doing is using a signed applet along with javawebstart and jni to load a native .dll onto the users system read some system level specs. Then contact a servlet that will make the database connection for me and return the results for comparison with the users specs. There would be about 20 fields from a single database record to be compared. So you think maybe an array of Strings would be the best way to go?

Thanks,
Brian
19 years ago
Hello All,
I using the jr.jar package. From my signed applet i pass objects to the servlet and then back again with no problem. This includes an array of objects. But when i try to pass a result set i get

java.lang.ClassCastException

using the following code.

--Applet Code--

ResultSet rs = (ResultSet) HTTP.send( ConnServlet , ConnArray );

--Servlet Code--

ResultSet rs = stmt.executeQuery(dbquery);
Servlets.sendObjectToClient( resp , rs );

---end----


From my reading i understand that there is a better way to do this with Vectors? Could some one explain? or a Code example would be great.

Thanks All,
Brian
19 years ago
Hello everyone i'm working on getting this ChatApplet to work. It's an example from Oreilly Java Servlet Programming on creating a chat system using applet servlet communication. The problem i'm having is the applet returns this exception.

Java(TM) Plug-in: Version 1.4.1_06
Using JRE version 1.4.1_06 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\Brian Swingle
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
p: reload proxy configuration
q: hide console
r: reload policy configuration
s: dump system properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------
java.lang.NoClassDefFoundError: com/oreilly/servlet/HttpMessage

at HttpChatApplet.getNextMessage(HttpChatApplet.java:70)

at HttpChatApplet.run(HttpChatApplet.java:98)

at java.lang.Thread.run(Unknown Source)

java.lang.NoClassDefFoundError: com/oreilly/servlet/HttpMessage

at HttpChatApplet.broadcastMessage(HttpChatApplet.java:111)

at HttpChatApplet.handleEvent(HttpChatApplet.java:135)

at java.awt.Component.postEvent(Unknown Source)

at java.awt.Component.postEvent(Unknown Source)

at java.awt.Component.postEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

But the Applet was compiled with this package on my PC. The applet its self is being run from a directory just inside my domain. And this package is included located in tomcat /lib folder but with the applet not being run from tomcat it cant fint the .jar file. How do i fix this exception?

here is the applet code.

----------------------------Applet---------------------------------
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;

import com.oreilly.servlet.HttpMessage;

public class SocketChatApplet extends Applet implements Runnable {

static final int PORT = 2428;

DataInputStream serverStream;

TextArea text;
Label label;
TextField input;
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 ort/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.
text = new TextArea();
text.setEditable(false);
label = new Label("Say something: ");
input = new TextField();
input.setEditable(true);

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 {
// Connect to the server if we haven't before
if (serverStream == null) {
Socket s = new Socket(getCodeBase().getHost(), PORT);
serverStream = new DataInputStream(
new BufferedInputStream(
s.getInputStream()));
}

// Read a line
nextMessage = serverStream.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());
serverStream = null;
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;
}

void broadcastMessage(String message) {
message = user + ": " + message; // Pre-pend the speaker's name
try {
URL url = new URL(getCodeBase(), "/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;
}
}
---------------------------End of Applet-----------------------

Thanks,
Brian
19 years ago
Hello Nathan,
Thanks for all the help it compiles now. But now i'm having an issue with the applet that loads. But i'll post that in the applets forum. Thanks again for all your help.

Brian
19 years ago
This is my Classpath i have set in my system variables on my PC. What should i add if say i was compiling these examples from C:\Examples\ ?


CLASSPATH:

Thanks
Brian

(NP - edited due to really long line widening page...)
[ March 24, 2005: Message edited by: Nathan Pruett ]
19 years ago
Hello everyone,
I trying to compile this simple RMI chat example from Oreilly Java Servlet Programming book. This example has 3 parts, ChatClient.java, ChatServer.java and ChatServlet.java. I will post some of the example code here or you can download all 3 files from my server at this address.

http://www.powerleap.com/downloads/RMI-Chat-Code.zip

----------ChatClient.java---------------
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface ChatClient extends Remote {
public void setNextMessage(String message) throws RemoteException;
}

-------------END--------------------------

This compiles just fine but the when i try to compile either of the other two examples like lets say. ChatServer.java i get this error.

---------- Java Compiler ----------
ChatServer.java:8: cannot resolve symbol
symbol : class ChatClient
location: interface ChatServer
public void addClient(ChatClient client) throws RemoteException;
^
ChatServer.java:9: cannot resolve symbol
symbol : class ChatClient
location: interface ChatServer
public void deleteClient(ChatClient client) throws RemoteException;
^
2 errors

Output completed (1 sec consumed) - Normal Termination
--------------end compile----------------------

The code for the chat server is short so i'll post it. here it is.

--------------ChatServer-----------------------
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface ChatServer extends Remote {
public String getNextMessage() throws RemoteException;
public void broadcastMessage(String message) throws RemoteException;

public void addClient(ChatClient client) throws RemoteException;
public void deleteClient(ChatClient client) throws RemoteException;
}
--------------END------------------------------

From the error returned by the compiler it looks like ChatServer.java has to import ChatClient some how. To give you some background i've got all the correct Jar files needed and i'm compiling on my personal computer before uploading to my Solaris server. The ChatClient.class file is in the same directory as the ChatServer.java file i'm tring to compile. Any help on this would be greatly appreciated.

Thanks,
Brian
19 years ago
I'll that. I was always told that == couldn't be used on strings in java only != for == you had to use .equals(). Thanks Best Regards.

Fighting for Cleaner Code.

Best Regards,
Brian
20 years ago
Hello All,
Ok i need to ask a stupid question. How would you go about checking a string for null without getting an exception.

The error is caused by this type of check.

----------------------Error Code-------------------------
String ABC = req.GetParameter("FieldABC");

if (ABC.equals(null))
{
ABC = "Empty Field";
}

----------------------End-----------------------

Now it could be done like this but its ugly.

---------------------Ugly Code------------------
String ABC = req.GetParameter("FieldABC");

if (ABC.equals(null))
{
}
else
{
ABC = "Empty Field";
}
---------------------End------------------------

So what i'm asking is does anyone know a cleaner way to check a string for null?

Thanks,
20 years ago
Hello Gregg,
I never noticed that. I tried it both way and its still the same situation the compiler finds everything but the 3 jars needed for javamail.

Should i be restarting my system after changing the Classpath?

Thanks,
Brian Swingle
20 years ago
i have it configured trough the tools section in editplus. I've used this to compile a ton of my servlets before. i'll post the properties for it anyway.

Name: Java Compiler
Command: D:\j2sdk1.4.1_06\bin\javac.exe
Arguement: $(FileName)
Initial Directory: $(FileDir)


Here is everyhting i'm importing into the servlet.

import java.io.*;
import java.text.*;
import java.util.*;
import java.util.Random;
import javax.servlet.*;
import javax.servlet.http.*;
import snaq.db.*;
import java.sql.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Hashtable;
import java.util.Properties;

The only packages it cant find are the 3 for the JavaMail.Thanks for all your guys help and continued help. I'm totaly stumped on this one.

Best Regards,
Brian Swingle
20 years ago
I removed the periods but still the same error on compilation. I have no issue with any of the other jar's in my classpath exept the mail and activation jar's.

It weired i dont get it.
20 years ago
Hello all,
I'm just trying to get JavaMail set up and i'm having trouble. When i'm try to compile my servlet i get the usual message of.

Javax.mail does not exist.
Javax.mail.internet does not exist.
javax.activation does not exist.

My classpath is set correctly to.

d:\jsdk2.1\servlet.jar;.d:jsdk2.1\server.jar;.d:\jsdk2.1\mail.jar;.d:jsdk2.1\activation.jar;.%classpath%"i\QTJava.zip;"

and thats the exat dir they are in i've double checked it ten times.
what do you think could be causeing these jar's not to be found?
any help is most appreciated. I'm in hell right now.

Best Regards,
Brian Swingle
20 years ago
Ok sorry,
I found the answer to my question i was right with the assumtion in my last post. It was explained to another member in this thread.

https://coderanch.com/t/348790/Servlets/java/member-variables-servlet

For anyone else who might be wondering. I think i have all the info i need now thank you for all your guys help and patience.

thanks,
Brian
20 years ago
In addition to that last post.

Unless creating variables in the doPost/doGet is thread safe were creating them outside of the doPost/doGet is not. Is this the answer or am i still way off track?

Thanks,
Brian
20 years ago
High All,
OK, I understand and i dont disagree i want to use the session object but i cant find a brief example of exatly how to implement that structure. Every example i have found is like 2,000 lines. Do you recommend and books that cover this subject in detail? Or do you know of an example you could point me too?

Also can anyone answer my question from before about retirieveing information from the session but only to store it in a variable anyway. Doesn't that defeat the purpose of using the session or am i missing something?

Thanks for all your help and patience.
Brian
20 years ago