Servlets/applets

Hi,
I want to invoke a servlet from an applet.
Say,I have an increment button in my applet.If I click it,it should invoke a servlet(which does incrmentation by one) and returns the value to the applet(say 2),so that,this value will be displayed in the textbox in the applet.Can anyone help me.Give me an idea.I'm new to java.
Thankx.
tmmetAsked:
Who is Participating?
 
enotsinConnect With a Mentor Commented:
A simple and dirty way to do it is to:

1. Invoke the servlet from your applet.
2. Have the servlet write its output to a variable in system properties
using System class method to write property (check it out from the API)
3. The applet read the System property (System.getProperty(<name>))
0
 
heyhey_Commented:
use ordinary URL

URL url = new URL("http://www.server.com/servlet/counter?num=2");
InputStream is = url.openStream();
// now read the response
....
is.read();
0
 
tmmetAuthor Commented:
Hi,
I tried using ur.openstram().Then,after reading the contents,I don't know how to send it back to the applet.Just got struck up there?.I have to increment the value and pass to the applet back.
Thankx in advance.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
heyhey_Commented:
he-he


so why did you ACCEPT the answer ??
0
 
enotsinCommented:
tmmet, did you press the wrong button? I didn't expect you to accept an answer why it doesn't solve your problem.

1. Did you try to use System.setProperty() and System.getProperty() to do it?

2. When you say you tried using openstream(), from where you call it? From the applet? If you call it from the applet, you can read the stream, parse its input as a text string, extract the result and put it back to a UI component in your applet and re-paint the screen. But I think it is too cumbersome to do it that way.
0
 
tmmetAuthor Commented:
Hi,
My code is as follows...


///Servlet part class
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class CountServlet extends HttpServlet
{
      int count;

      public void init(ServletConfig conf) throws ServletException
      {
      super.init(conf);
            count = 0;
      }

      public void service(HttpServletRequest req, HttpServletResponse res)
      {
            try
            {
                  String str;
                  str = req.getParameter("operation");

                  if(str.equals("increment"))
                  {
                        res.setContentType("text/html");
                        PrintWriter out = res.getWriter();
                        HttpSession session = req.getSession(true);
                       Integer count = (Integer)session.getValue("tracker.count");
                        if(count == null)
                       count = new Integer(1);
                        else
                        count = new Integer(count.intValue() + 1);
                        session.putValue("tracker.count",count);
                         ServletOutputStream out1 = res.getOutputStream();
                        res.setContentType("text/html");
                        out1.println(" " + count);
                        out1.close();
                  }

                  else if(str.equals("reset"))
                  {
                        count = 0;
                        ServletOutputStream out = res.getOutputStream();
                        res.setContentType("text/html");
                        out.println(" " + count);
                        out.close();
                  }
            }
            catch(Exception e)
            {
                  System.out.println("Error" + e);
            }
      }
}


//Applet part class
import java.awt.*;
import java.io.*;
import java.net.*;


public class CountServletApplet extends java.applet.Applet
{
      private TextField SessionField;
      private Button SessionCount;
      private Button GlobalCount;
      private Button FileCount;



      private URLConnection connect;
      private URL u1;

      String sum = "0";
    public CountServletApplet()
      {
            setLayout(new FlowLayout());
            add(new Label("Count"));
            SessionCount = new Button("Session Count");
            FileCount = new Button("File Count");
            GlobalCount = new Button("GlobalCount");
            SessionField = new TextField("1");
            add(SessionCount);
            add(GlobalCount);
            add(FileCount);
            add(SessionField);
            setVisible(true);
            setSize(600,600);
      }
             public boolean action(Event e, Object arg)
      {
            if(e.target ==  SessionCount)
            {
                  try
                  {
                        System.out.println("Resetting Count..");
                        u1 = new URL(getDocumentBase(),"/servlet/CountServlet?operation=increment");
                        connect = u1.openConnection();

                        connect.setDefaultUseCaches(false);
                        connect.setUseCaches(false);
                        connect.setDoInput(true);
                        connect.setDoOutput(true);
                        connect.connect();

                        DataInputStream in = new DataInputStream(new BufferedInputStream(connect.getInputStream()));
                        String response = in.readLine();
                        System.out.println(response);
                        SessionField.setText(response);
                        in.close();

                        //showStatus("Incrementing");
                         
                  }
                  catch(Exception x)
                  {
                        System.out.println("System Exception " + x);
                  }
                  return true;
            }
            return false;
      }
      
}


0
 
tmmetAuthor Commented:
Hi,
I used "http://localhost:8080/servlet/Appletname.html" to invoke my applet.But,"Not found" error is coming.
0
 
heyhey_Commented:
use
http://localhost:8080/servlet/CountServlet

you probably have to config your web server first ...


0
 
tmmetAuthor Commented:
Hi,
http://localhost:8080/servlet/CountServlet will invoke the servlet only.For invoking applet,we have to use
http://localhost:8080/servlet/appletname or
http://c:/pathname of the appletname.html file.I think the second is correct as we are invoking the applet.Am i correct?.

0
All Courses

From novice to tech pro — start learning today.