aspose file tools*
The moose likes Servlets and the fly likes applet-servlet communication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "applet-servlet communication" Watch "applet-servlet communication" New topic
Author

applet-servlet communication

anuj khanna
Greenhorn

Joined: Jan 11, 2001
Posts: 20

its sending a string and add hello to the string by the servlet and returned to the applet
//applet
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
/*
<applet code="appservlet" width=100 height=100>
</applet>
*/
public class appservlet extends Applet{
TextField tf,tf1;
Button b1,b;
URLConnection uc;
public void init()
{
try {
tf=new TextField();
tf1=new TextField();
b=new Button("send");
b1=new Button("receive");
add(tf);
add(tf1);
add(b);
add(b1);
ButtonHandlerS bs=new ButtonHandlerS();
ButtonHandlerR br=new ButtonHandlerR();
b1.addActionListener(br);
b.addActionListener(bs);
}
catch(Exception ex)
{System.out.println(ex);}
}
void sendData()
{
try{
String queryString=tf.getText();
URL url=new URL("http://localhost:8080/servlet/srverapp");
uc=url.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
String qry=URLEncoder.encode("qrl")+"="+URLEncoder.encode(queryString);
DataOutputStream dos=new DataOutputStream(uc.getOutputStream());
dos.writeBytes(qry);
dos.flush();
dos.close();}
catch(Exception ex)
{System.out.println(ex);}
}
void getData()
{
try{
InputStreamReader in=new InputStreamReader(uc.getInputStream());
int chr=in.read();
while (chr!=-1)
{
tf1.setText(String.valueOf((char)chr));
chr=in.read();
}
in.close();}
catch(Exception ex)
{System.out.println(ex);}
}
class ButtonHandlerS implements ActionListener{
public void actionPerformed(ActionEvent ae)
{
sendData();
tf.setText("");
}
}
class ButtonHandlerR implements ActionListener{
public void actionPerformed(ActionEvent ae)
{
getData();

}
}
}
//servlet
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class srverapp extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)
{

try
{

String str=req.getParameter("qry");
PrintWriter pw=res.getWriter();
pw.println("hello"+str);
pw.println("<html><head><title>trial</title></head>");
pw.println("<body>str</body>");
pw.println("</html>");
}
catch(IOException ie)
{
System.out.println(ie);
}
}
}
using serveletrunner
error:FileNotFoundException:http://localhost:8080/servlet/srveapp

thankyou
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

Was your servlet container running at the time? Does it work from a browser?


SCJP
Visit my download page
anuj khanna
Greenhorn

Joined: Jan 11, 2001
Posts: 20
servletrunner was running.whats servlet container
the servletrunner was showing init when the applet called the servlet
Marius Holm
Ranch Hand

Joined: Sep 11, 2000
Posts: 84
Anuj,
If you could give some more details, perhaps it would be easier for people to help you. Like WHAT server do you run? You don't know what a servlet container is? If you gave more info that would perhaps have been obvious to the person above who tried to answer your question.
But, if you come here for the social aspect, I meen if that is what you want, to have a correspondence going on here for weeks, then you might perhaps do just the right thing...
Good luck with your problem, whatever it is...
Marius
anuj khanna
Greenhorn

Joined: Jan 11, 2001
Posts: 20
well i'm not using any server but using servletrunner which is used to view how a sevlet works i don't have java web server.i'm stuck with this problem and not able to move ahead in my project.
well i'm new to servlet programming.
thanks anyway
typksn
Greenhorn

Joined: Feb 05, 2001
Posts: 3
Were you able to find a solution? If not, do you know if the servlet has a process id, which indicates that it is truly running?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
That error message:
"using serveletrunner
error:FileNotFoundException:http://localhost:8080/servlet/srveapp"
suggests that servlet runner was looking for a "srveapp.class" - maybe there is just a spelling error somewhere.
"servlet container" is the way Sun talks about any servlet engine - the idea being that a servlet container provides specific functions for a servlet that "lives" inside it.
Bill
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

Good catch Bill. It is srverapp not srveapp.
public class srverapp extends HttpServlet{
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: applet-servlet communication