如何将httpservlet添加到jforum?

时间:2012-06-28 07:34:33

标签: java

我开发了一个基于Jforum的网站,我的网页需要json返回。所以我创建了一个实现这一目标的servlet。我的问题是在哪里放这个代码() - 是net.jfourm.util.mine是一个好地方?以及如何修改它以使其适合jfourm。 感谢。

    package net.jforum.util.mine;

    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import java.util.ArrayList;
    import java.util.List;

    import net.sf.json.JSONArray;

    public class JsonServlet extends HttpServlet
    {
        private static final long serialVersionUID = 1L;

        public JsonServlet()
        {
            super();
        }

        public void init() throws ServletException
        {   
        }

        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            int node = Integer.parseInt(request.getParameter("node"));

            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();

            JSONArray ja = new JSONArray();     
            ja = this.getMenu(node);
            out.println(ja.toString());     
        }

        protected void doGet(HttpServletRequest request,HttpServletResponse response )throws ServletException,IOException
        {
            doPost(request,response);
        }


        public JSONArray getMenu(int node) 
        { 
            List<MenuBean> list = new ArrayList<MenuBean>();

            MenuBean m[][] = new MenuBean[4][3];
            MenuBean l0[] = {new MenuBean("1", "service", false, "folder"), new MenuBean("2", "system", false, "folder"), new MenuBean("3", "statistics", false, "folder")};
            MenuBean l1[] = {new MenuBean("21000", "aaaa", true, "file", "test/show/arg1/arg2.page"), new MenuBean("22000", "aaaa", true, "file"), new MenuBean("23000", "hhhh", true, "file")};    
            MenuBean l2[] = {new MenuBean("11000", "cccc", true, "file"), new MenuBean("12000", "eeee", true, "file"), new MenuBean("13000", "gggg", true, "file")};
            MenuBean l3[] = {new MenuBean("31000", "bbbb", true, "file"), new MenuBean("32000", "dddd", true, "file")};

            m[0] = l0;
            m[1] = l1;
            m[2] = l2;
            m[3] = l3;      

            for(int i = 0; i < m[node].length; i++){
                 list.add(m[node][i]);
            }

            JSONArray ja = JSONArray.fromObject(list);      

            return ja;
        }    
    }        

0 个答案:

没有答案