Returns a Java array from a java servlet to jQuery

I am currently developing a web application that uses the cycle plug-in for jQuery to display slides of images For ease of use, I'm making the application configurable to allow someone to change the path where the slide image can be found for display I've found the necessary code to create all image file names into a string array, but I'm not entirely sure how to pass the entire array back to my jQuery for processing I have used java servlet as a proxy to access some RSS feeds, so I decided to use the "$. Get()" method to generate HTTP requests with tag parameters to determine the functions to be performed

In short, how do I pass a string array to an httpservletresponse variable so that it can be accessed like the string array in my jQuery? This is some of the code I've used so far... Note: I'm new to Java and JavaScript, including jQuery I know my code can be sloppy and / or inefficient

---HERE'S THE JAVA SERVLET----

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.net.URL;

    import javax.servlet.http.HttpServlet;

    public class BBSServlet extends HttpServlet {
        private void getSlidesList(final HttpServletResponse response) throws ServletException,IOException {
            try {
                File slidesdir = new File(AppConfiguration.getInstance().getSlidesDir());

                if(slidesdir.isDirectory()) {
                    String slidenames[] = slidesdir.list();
                    // This is what I thought I Could do...
                final PrintWriter writer = response.getWriter();

                for(int i = 0; i < slidenames.length; i++) {
                    writer.println(slidenames[i]);
                }
                // But I'm not sure if it works...                  }
            } catch(final IOException e) {
                e.printStackTrace();
            }
        }
        public void doGet(final HttpServletRequest request,final HttpServletResponse response) throws ServletException,IOException {
            response.setContentType("text/xml");
            final URL url;

            if(request.getParameter("p").equals("w")) {
                url = new URL(AppConfiguration.getInstance().getForecastUrl());
                sendXML(response,url);
            }
            else if(request.getParameter("p").equals("n")) {
                url = new URL(AppConfiguration.getInstance().getNewsUrl());
                sendXML(response,url);
            }
            else if(request.getParameter("P").equals("f")) {
                getSlidesList(response);
            }
        }
    }

---jQuery js------------

    // function called from the $(document).ready()

    function DisplaySlides() {
        $.get(baseContext + "/servlet?p=f",function(data) {
            // "data" is hopefully a String array?
        }
        // display my slideshow with that array
    }

Solution

I recommend using JSON parser (such as Jackson) in Java to convert string arrays to JSON JQuery can then read it and convert it to jQuery JavaScript array of getjson

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>