使用arraylist从servlet保存字符串并在其他servlet中检索相同的arraylist以检索数据

时间:2016-05-13 07:14:41

标签: java servlets arraylist

Hello Guys我是java的新手,我试图通过通过html发送的servlet将表单数据保存到数组列表中,并使用第二个servlet来检索相同的arraylist。 问题是,我可以使用“is.setAl(str);”​​使用servlet将数据添加到列表中。以及在servlet中检索数据。但是当我尝试使用“arrList = is.getAl();”在servlet 2中检索数据时它给出了空值。

下面的

是我的Servlet和目标代码。

Servlet 1:

public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public static InfoSave is = new InfoSave();
/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doPost(request, response);
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    BufferedReader br =request.getReader();

    String str = br.readLine();
    System.out.println("The String Recieved is:\n"+str);

    is.setAl(str);  


    System.out.println("----> "+is.getAl()+"\n");

    response.setContentType("application/json");
    response.getWriter().write(str);
}

Servlet 2:

public class SearchServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static InfoSave is = new InfoSave();
/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // TODO Auto-generated method stub
    doPost(request, response);
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // TODO Auto-generated method stub
    BufferedReader br = request.getReader();

    String str = br.readLine();
    System.out.println(str);


     ArrayList<String> arrList = null, result = null;

      arrList = is.getAl();
      System.out.println("\n"+arrList);
      result = new ArrayList<>();
      for (Iterator iterator = arrList.iterator(); iterator.hasNext();)
      {
        String string = (String) iterator.next();
        if(string.contains(str)){
            result.add(string);
        }else{
            String message = "No Matching result Found";
            response.setContentType("application/json");
            response.getWriter().write(str);
        }

      }
      System.out.println("Size: "+result.size());
      String json = new Gson().toJson(result);


    response.setContentType("application/json");
    response.getWriter().write(json);
}
}

ArrayList对象:

public class InfoSave {
private ArrayList<String> al = new ArrayList<>();

public ArrayList<String> getAl() {
    return al;
}

public void setAl(String s) {
    al.add(s);
}
}

3 个答案:

答案 0 :(得分:0)

在Servlet 2中:

arrList = is.getAl();更改为TestServlet.is.getAl();

Servlet 2和Servlet 1中的

is是不同的对象。

答案 1 :(得分:0)

在servlet 2中,您正在访问servlet 2的静态变量is,而不是servlet 1的is变量;他们没有引用相同的InfoSave对象。来自servlet 2的变量未填充,当您调用is.getAl()时,返回的列表为空。

在这种情况下,如果要跨这两个servlet共享信息,可以将对象放在servlet 1中的会话中,并从servlet 2中的会话中检索。

Servlet 1:

...
InfoSave is = new InfoSave();
is.setAl(str);
request.getSession().setAttribute("IS", is);
...

Servlet 2:

...
InfoSave is = (InfoSave) request.getSession().getAttribute("IS");
arrList = is.getAl();
...

通过这种方式,您不需要在servlet中使用静态变量。

有关servlet会话的更多信息:

http://www.tutorialspoint.com/servlets/servlets-session-tracking.htm

答案 2 :(得分:0)

嗯,甚至不尝试这样做!

Servlet旨在以可能的多线程方式从servlet容器中为不同的客户端会话调用。这意味着它们应该是无状态对象。您可以在一个servlet中使用可公开访问的静态InfoSave对象,并在另一个servlet中使用它,但这不是 Java EE方式

如果您希望每个活动会话使用一个InfoSave,或者如果您想要一个单独的共享列表,则在servlet上下文中使用会话属性的正确方法。这样做的好处是,您可以更轻松地从中途切换到另一个。

这是使用会话的示例代码:

public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    ...    
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Get access to a session scoped InfoSave - local to keep the servlet stateless
        HttpSession session = request.getSession();
        InfoSave is = (InfoSave) session.getAttribute("INFOSAVE");
        if (is == null) { // no InfoSave here: create a brand new one
            is = new InfoSave();
            session.setAttribute("INFOSAVE", is); // store it into the session
        }
        // the remaining of the code can safely access the session scoped InfoSave
        ...
    }
}
顺便说一下,你想使用静态InfoSave或将它存储在servlet上下文属性中,你应该同步访问,因为多个线程可以同时执行一个servlet - 但希望只有一个可以使用给定的会话...