File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
john mattucci
Ranch Hand
Posts: 331
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13046
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17624
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13046
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic