• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

applet-servlet communication

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Was your servlet container running at the time? Does it work from a browser?
 
anuj khanna
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
servletrunner was running.whats servlet container
the servletrunner was showing init when the applet called the servlet
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good catch Bill. It is srverapp not srveapp.
public class srverapp extends HttpServlet{
 
reply
    Bookmark Topic Watch Topic
  • New Topic