aspose file tools*
The moose likes Servlets and the fly likes what am i doing wrong URGENT (this is my true posting) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "what am i doing wrong URGENT (this is my true posting)" Watch "what am i doing wrong URGENT (this is my true posting)" New topic
Author

what am i doing wrong URGENT (this is my true posting)

john mattucci
Ranch Hand

Joined: Nov 03, 2000
Posts: 331
This is my first attempt at both an Applet and Servlet. I hope you can take the time to help me, I really need the help. Im trying to obtain for the moment simply the user name and in turn this information will be saved to SQL. Im using the Applet --> Servlet --> JDBC --> SQL strategy. The problem that im having is that in the line marked ** im not connecting to my Servelt- I think. Because no errors are thrown but the information is never transfered to SQL. I have placed several System.println statements and the last one is executed in my applet class. I hope I was clear and If you need more info please ask. Thank you all for your time.
public class First extends Applet
{
public void addName()
{
try
{
URL dataURL = new URL("http://caaove01/bryantest/JAVA/t/Serv");
URLConnection connection = dataURL.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches (false);
connection.setDefaultUseCaches(false);
// Specify the content type that we will send binary data
connection.setRequestProperty ("Content-Type", "application/octet-stream");
**ObjectOutputStream out = new ObjectOutputStream(connection.getOutputStream());
out.writeObject(getTextField1().getText());
out.flush();
out.close();
}
private Button getButton1()
{
ivjButton1.addActionListener(new ReflectiveAction(this, "addName"));
return ivjButton1;
}
//Servlet code
public class Serve extends HttpServlet
{
private InfoGetter info;
public void doGet(javax.servlet.http.HttpServletRequest .....
performTask(request, response);
public void doPost(javax.servlet.http.HttpServletRequest ....
{
performTask(request, response);
}
public void init()
{
info = new InfoGetter();
}
public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) {
try
{
ObjectInputStream objIn = new ObjectInputStream(request.getInputStream());
String person = (String)objIn.readObject();
objIn.close();
info.addPersInfo(person);
}

}
//Connection to SQL
public class InfoGetter
{
public InfoGetter()
{
String dsn = "jdbc:twtds:sqlserver...";
try
{
Class.forName("com.thinweb.tds.Driver");
myCon = DriverManager.getConnection(dsn, "this", "that");

}
public void addPersInfo(String first)
{
try
{
PreparedStatement statement = myCon.prepareStatement(
"INSERT INTO email ([Name]) VALUES (?)");
statement.setString(1,first);
statement.executeUpdate();
statement.close();
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
I see lots of try{ block starts but I don't see any catch statements where you print out or log the exceptions. Is that really the way your code reads?
Bill
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16019
    
  20

A superficial glance seems to tell me that you're attempting to serialize objects. That can cause all sorts of grief since the receiver may not be compatible with the sender.
Over the years I've come to the conclusion that one should never use a binary format when a text one will do. Binary can't be read without a suitable decoder program, the available tools for manipulation are fewer, when you do inter-system work you get clobbered by byte-order and other hardware issues. I think it was Microsoft that finally convinced me once and for all - if just ONE of their OLE files gets scrambled, an entire system can sometimes topple.
Most of the old arguments for using binary are pretty pointless - we have sufficient hardware capacity to store and transmit it, and bandwidth and security issues can often be resolved at the hardware level using standardized compression and encryption firmware (or at least at some other layer than the application).
Not that I think Java is going to drop serialization - it does have its uses. However, you're not the first to discover the hard way that serialization between and applet and a server isn't a guaranteed success.


Customer surveys are for companies who didn't pay proper attention to begin with.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
I think serializing a String is pretty safe. Serialization is essential to many key Java capabilities, but as you say, the problem comes if both sides have different object versions.
XML & SOAP are much more flexible in that situation.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what am i doing wrong URGENT (this is my true posting)