0

I tried to deploy the servlets. I have a inner class, but it seems like the complier cannot find the inner class. It generates the java.lang.ClassNotFoundException exception. Below is my servlets java code, web.xml, error message. Hope someone could help me. Thank you in advance.

Servlet java code

package com.tutorials;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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 javax.swing.JButton;
import javax.swing.JFrame;
    public class Dummy extends JFrame{  
        public static JButton play;
        public static JButton stop;
        public Dummy() {
            Container content = getContentPane();
            play = new JButton("play");
            play.setEnabled(true);
            PlayListener playListener = new PlayListener();
            play.addActionListener(playListener);
            content.add(play, BorderLayout.NORTH);
    }

    class PlayListener extends HttpServlet implements ActionListener {

        public ArrayList<String> fresh = new ArrayList<String>();

        public void actionPerformed(ActionEvent e) {

            fresh.add("what");
            fresh.add("the");
            fresh.add("hell");
            System.out.println("fresh size is " + fresh.size());
        }

        @Override
        public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setContentType("application/json");
            PrintWriter writer = resp.getWriter();
            if(fresh.size() == 0) {
                writer.println("freshsizeiszer0");

            } else {

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


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

XML

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>Tutorial2</display-name>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>


<servlet>
    <servlet-name>PlayListener</servlet-name>
    <servlet-class>com.tutorials.Dummy.PlayListener</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>PlayListener</servlet-name>
    <url-pattern>/play</url-pattern>
</servlet-mapping>

</web-app>

the structure of my folder

error message

vkosyj
  • 757
  • 1
  • 7
  • 21
  • 1
    Well, don't use an inner class as a servlet. Use a top-lever public class. Why in hell do you mix servlet and swing code together? – JB Nizet Dec 03 '16 at 23:40
  • Try adding another ending brace next to the one for the constructor. – beastlyCoder Dec 03 '16 at 23:42
  • @JBNizet Well, I don't like this as well but I have to do. Actually my first thought is to separate servlet and swing code but I got stuck for two days. Please see http://stackoverflow.com/questions/40953052/how-do-i-call-actionperformed-method-from-another-class-in-java if you are interested. – vkosyj Dec 03 '16 at 23:43
  • @Aaron the brace is right, it is the issue of formatting. – vkosyj Dec 03 '16 at 23:46
  • 1
    Swing is used to make graphical, desktop applications. Servlets are used to make a web server respond to HTTP requests. There is no reason at all to mix them together. And even less to use an inner class of a JFrame subclass as a servlet. That makes no sense. What are you trying to achieve? – JB Nizet Dec 03 '16 at 23:47
  • @JBNizet I got your point. This is what I want to achieve. http://stackoverflow.com/questions/40953052/how-do-i-call-actionperformed-method-from-another-class-in-java It use the result in the UI but I also need to put the data on the server. Thanks – vkosyj Dec 03 '16 at 23:53
  • Read the answer you got. You can't just mix servlet and swing code and hope that will magically make the swing app communicate with a servlet. You need two, completely separated projects. One is a client, written in Swing, and involving no servlet code at all. This client uses the HTTP protocol (using HttpUrlConnection, for example), to send HTTP request to a server. On this server, deployed on a web container, runs a completely separate application, containing a servlet, but no swing code at all, that receives the HTTP requests from the client and does something with them. – JB Nizet Dec 03 '16 at 23:59
  • I repeat: what you have makes absolutel no sense. You need to learn about how the web works. For example, the browser you use to read and post comment is client app communicating with a completely separate app running on stackoverflow's servers. The server doesn't contain any code from the browser. The browser doesn't contain any code from stackoverflow. They're completely separate apps, communicating over HTTP. – JB Nizet Dec 04 '16 at 00:02

1 Answers1

0

Probably because PlayListener is not static, change to

public static class PlayListener ...

Have to say this mix of Swing and Servlet looks quite strange and probably wouldn't work as you expect (not sure what you want to accomplish). Servlet instance and your main instance would be different (even on different JVM) and data that you update in actionPerformed have no any relation to data that doGet would access.

If you plan http server and a standalone Java client that updates data on the server, it should be done in a different way, by client posting data to the server.

Fedor Losev
  • 3,244
  • 15
  • 13
  • Thank you. My first thought is to separate them together but I got stuck for 2 days. Please see this http://stackoverflow.com/questions/40953052/how-do-i-call-actionperformed-method-from-another-class-in-java only if you are interested. – vkosyj Dec 03 '16 at 23:50
  • Look at my answer to the other question (without this one it wasn't clear what you are asking there). Let me know if you need more details. – Fedor Losev Dec 03 '16 at 23:54