Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Servlets/applets

Posted on 1999-07-19
9
Medium Priority
?
310 Views
Last Modified: 2013-11-24
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.
0
Comment
Question by:tmmet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 1246508
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
 

Accepted Solution

by:
enotsin earned 0 total points
ID: 1246509
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
 

Author Comment

by:tmmet
ID: 1246510
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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 16

Expert Comment

by:heyhey_
ID: 1246511
he-he


so why did you ACCEPT the answer ??
0
 

Expert Comment

by:enotsin
ID: 1246512
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
 

Author Comment

by:tmmet
ID: 1246513
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
 

Author Comment

by:tmmet
ID: 1246514
Hi,
I used "http://localhost:8080/servlet/Appletname.html" to invoke my applet.But,"Not found" error is coming.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1246515
use
http://localhost:8080/servlet/CountServlet

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


0
 

Author Comment

by:tmmet
ID: 1246516
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

Featured Post

CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question