0

How do I call

actionPerformed(ActionEvent e)

from another method, that is,

returnHolder()

in my case, so that the arraylist can have all the data so then I can use servlet to write the data on the localhost. For now, in my

doGet

method,

System.out.println("size of the list is " + list.size());

gives me zero. Hope someone could help me out. Thank you so much.

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
public class Dummy extends JFrame{  
    public static ArrayList<String> list = new ArrayList<String>();
    public static ArrayList<String> holder = new ArrayList<String>();
    public static JButton play;
    public Dummy() {
        Container content = getContentPane();
        play = new JButton("fuck");
        play.setEnabled(true);
        PlayListener playListener = new PlayListener();
        play.addActionListener(playListener);
        content.add(play, BorderLayout.SOUTH);  
    }

    class PlayListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            list.add("what");
            list.add("the");
            list.add("hell");
            for(int i = 0; i < list.size(); i++){
                holder.add(list.get(i));
            }       
        }
    }

    public static ArrayList<String> returnHolder() {
        //play.doClick();
        return holder;
    }

    public static void main(String args[]) {
        JFrame frame = new Dummy();
        frame.pack();
        frame.show();
    }
}




import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tutorials.Dummy;

public class ListJson extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ArrayList<String> list = Dummy.returnHolder();
    System.out.println("size of the list is " + list.size());
    resp.setContentType("application/json");
    PrintWriter writer = resp.getWriter();


    for(int i = 0; i < list.size(); i++) {
        writer.println(list.get(i));
    }
    writer.flush();
    writer.close();
}

}

vkosyj
  • 757
  • 1
  • 7
  • 21

1 Answers1

0

After reading Your other question it is more clear what you want to do.

Servlet and your main program live in different JVM and can not talk directly.

To communicate between them you need some kind of remote communication. In this case the simplest method is to use http as you already running http server.

For example, you can add doPost method to your servlet and from your main program post some json data there. There are enough tutorials on sending http from client, e.g. look here

Note that you can not just keep data as a non-static field in servlet instance, because it is not guaranteed that doGet will be invoked on same instance as doPost.

For real-life system you would keep data in a data storage, probably abstracted by a framework or persistence layer. I guess for you it is not the case yet. For study / tutorial purposes, you can keep the data in some static member, so doGet and doPost access the same data instance. You need also guard retrieve/update e.g. with synchronized, because doGet and doPost may come from different threads.

You can look at this question for servlet update from standalone client example.

Community
  • 1
  • 1
Fedor Losev
  • 3,244
  • 15
  • 13
  • Thank you for the help. I thought doGet is writting json to the server – vkosyj Dec 03 '16 at 23:59
  • I think you need to dedicate a hour and read some tutorial on servlets, it would be much faster than trying to go though it by guess. Http servlet methods write or receive data to/from http client. Your doGet writes data to some client e.g. browser (note that your method is incorrect because what you write is not a JSON, so you'll need some work here too). From your swing app you want to **update** that data on server. Easiest method is to receive data on server from http post, and you'll need to handle received data with another method, not doGet that returns the data. – Fedor Losev Dec 04 '16 at 00:09
  • Actually you can handle it with the same doGet method in somewhat ugly way, by distinguishing fetch or update through url parameter, but separating these is much cleaner. – Fedor Losev Dec 04 '16 at 00:13
  • You can look at this almost duplicate question http://stackoverflow.com/questions/13601435/how-to-call-the-servlet-from-java-swing-login-page-using-httpclient-in-apache – Fedor Losev Dec 04 '16 at 00:16
  • Thank you for the reply. I will definitely take your advice and learn more about servlet. Thank you for pointing out the jason is wrong. Actually here what I am trying to do it the following. I have a arraylist called fresh. When I click the play button, "what", "the", "hell" will be added to the arraylist. Then I try to send the these three string to the server, so only the last step involves the networking staff. – vkosyj Dec 04 '16 at 00:16
  • and here i just have the issue of classNotFound – vkosyj Dec 04 '16 at 00:18
  • As I said, this would give you the an example of how to send the update http://stackoverflow.com/questions/13601435/how-to-call-the-servlet-from-java-swing-login-page-using-httpclient-in-apache – Fedor Losev Dec 04 '16 at 00:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129708/discussion-between-fedor-losev-and-vkosyj). – Fedor Losev Dec 04 '16 at 00:26