使用链接列表登录和注册-无法访问从其他servlet创建的对象

问题描述 投票:0回答:1

我创建了两个servlet页面,第一个servlet页面是RegistrationServlet,第二个servlet页面是LoginServlet,并为链接列表创建了一个对象,以便从其他servlet调用它,但是我无法调用该对象。

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

    String name,email,pass;

    name=request.getParameter("un");
    email = request.getParameter("em");
    pass = request.getParameter("pass");

    LinkedList<String> a1=new LinkedList();  
    a1.add(name);
     a1.add(pass);

    if (!name.isEmpty() && !email.isEmpty() && !pass.isEmpty()) {
      RequestDispatcher rd=getServletContext().getRequestDispatcher("/Login.html");
        rd.forward(request, response);

        request.getSession().setAttribute("someone", a1);
     request.getRequestDispatcher("LoginServlet").forward(request, response);
    } else {
        RequestDispatcher rd1 = getServletContext().getRequestDispatcher("/Register.html");
        rd1.include(request, response);
    }   
    }

    Other Servlet---------------------------------------
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException  {
    processRequest(request, response);
    String email = request.getParameter("eml");
        String pass = request.getParameter("pass");


        object fg;
    fg = request.getSession().getAttribute("someone");


     if(email.equals(fg.a1.get(0)) && pass.equals(fg.a1.get(1)))
    {
     System.out.println("sucess");

     }
      else{
        System.out.println("not");

      }   
     }
java jsp servlets
1个回答
0
投票
您通过以下命令将列表放入会话:request.getSession()。setAttribute(“ someone”,a1);“ a1”是变量的名称,不会放入会话中。request.getSession()。getAttribute(“ someone”)给定的值必须为List类型。该对象没有字段“ a1”。因此,要获取该列表中的项目,您必须编写fg.get(0),而不是fg.a1.get(0)。
© www.soinside.com 2019 - 2024. All rights reserved.