Hi freinds,
ive been trying applet-servlet- database thing with object serilization, when i made a simple program with just 3 fields it was working fine and now when i increased the fields it is not working and the
java console is giving file not found error for the
servlet while it was working fine earlier and of course the file is there, Im pasting the code of
applet here , plz try and see if u can help me,
Regards,
Daman
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
/*<applet code=TestApplet.class width=300 height=200>
</applet>*/
public class TestApplet extends Applet implements ActionListener,Runnable
{
Panel top,middle,bottom;
Label lname, lage, lweight,ltelephone,laddress;
TextField name, age, weight,telephone,address;
TextArea display;
Button submit, clear,obtain;
String servletPath;
URL studentDBservlet;
URLConnection servletConnection;
private String webServerStr, protocol;
private String hostName="localhost";
private int port = 8080;
public void init()
{
this.setLayout(new BorderLayout());
top=new Panel();
top.setLayout(new GridLayout(6,2));
middle=new Panel();
middle.setLayout(new BorderLayout());
bottom=new Panel();
bottom.setLayout(new GridLayout(1,3));
servletPath="/servlet/DBServlet";
lname=new Label("Name");
lage=new Label("Age");
lweight=new Label("Weight");
ltelephone=new Label("Telephone");
laddress=new Label("Address");
name=new TextField();
age=new TextField();
weight=new TextField();
telephone=new TextField();
address=new TextField();
display=new TextArea();
submit=new Button("Submit");
clear=new Button("Clear");
obtain=new Button("Obtain");
submit.addActionListener(this);
clear.addActionListener(this);
obtain.addActionListener(this);
top.add(new Label("Registration Form"));
top.add(new Label());
top.add(lname);
top.add(name);
top.add(lage);
top.add(age);
top.add(lweight);
top.add(weight);
top.add(ltelephone);
top.add(telephone);
top.add(laddress);
top.add(address);
middle.add(display);
bottom.add(submit);
bottom.add(clear);
bottom.add(obtain);
add(top,BorderLayout.NORTH);
add(middle,BorderLayout.CENTER);
add(bottom,BorderLayout.SOUTH);
setVisible(true);
URL hostURL = getCodeBase();
protocol=hostURL.getProtocol();
hostName = hostURL.getHost();
port = hostURL.getPort();
if (port == -1)
{
port = 80;
}
webServerStr = "http://"+hostName+":"+port+servletPath;
try
{
studentDBservlet = new URL(webServerStr);
System.out.println(webServerStr);
servletConnection = studentDBservlet.openConnection();
// inform the connection that we will send output and accept input
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);
// Don't used a cached version of URL connection.
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches(false);
// Specify the content type that we will send binary data
servletConnection.setRequestProperty("Content-Type","application/octet-stream");
Thread t=new Thread(this);
t.start();
}
catch(Exception e)
{
System.out.println("Exception in constructor"+e);
}
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==clear)
{
name.setText("");
age.setText("");
weight.setText("");
telephone.setText("");
address.setText("");
}
if(ae.getSource()==submit&&!name.getText().trim().equals("")&&!age.getText().trim().equals("")&&!weight.getText().trim().equals("")&&!telephone.getText().trim().equals("")&&!addres s.getText().trim().equals(""))
{
try
{
Student objStudent=new Student(name.getText(),Integer.parseInt(age.getText()),new Double(weight.getText()).doubleValue(),Integer.parseInt(telephone.getText()),address.getText());
System.out.println("name "+objStudent.getName()+" age "+objStudent.getAge()+" weight "+objStudent.getWeight()+" telephone "+objStudent.getTelephone()+" address "+objStudent.getAddress());
ObjectOutputStream oos=new ObjectOutputStream(servletConnection.getOutputStream());
oos.writeObject(objStudent);
oos.flush();
oos.close();
}
catch(Exception e)
{
System.out.println("exception in comtacting host "+e);
}
}
}
public void run()
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(servletConnection.getInputStream()));
while(true)
{
String read=in.readLine();
if(read==null)
return;
display.append(read);
}
}
catch(Exception e)
{
System.out.println("Exception in reading "+e);
}
}
}