This is a function in class DepartmentBean which check if element already exist in container:

bool f;

public void addIfAbsent(UserBean userBean) {

            if (users.stream().anyMatch(x -> x.getUsername().equals(userBean.getUsername()))) {
                f = false;

            } else {
                f = true;
                users.add(userBean);

            }
        }
    }

In my servlet I am trying to send Message of succeseful registration(if f=false, their's no username with the same name):

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

    try {
                doAll(request, response);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

public static void doAll(HttpServletRequest request, HttpServletResponse response) throws Exception {

             DepartmentBean dp = new DepartmentBean();
             UserBean user = new UserBean();
             DepartmentBean departmentBean = read();
                String userName = request.getParameter("username");
                String password = request.getParameter("password");

                user.setPassowrd(password);
                user.setUsername(userName);

                departmentBean.addIfAbsent(user);

    if(dp.f = true)

    {
        String Message = "msg";
        /*Set attribute msg of Message*/
        request.setAttribute(Message, "msg");
        RequestDispatcher rd=request.getRequestDispatcher("index.jsp");

         rd.forward(request, response);

         write(departmentBean);

    }

        }

        public static DepartmentBean read() throws JAXBException {
            JAXBContext context = JAXBContext.newInstance(DepartmentBean.class, UserBean.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            return (DepartmentBean) unmarshaller.unmarshal(new StreamSource(new File("1.xml")));
        }

        public static void write(DepartmentBean department) throws JAXBException {
            JAXBContext context = JAXBContext.newInstance(DepartmentBean.class, UserBean.class);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(department, new File("1.xml"));
        }

    }

In my html I added this jsp code:

<%>
    String s = "msg";
    if (s == request.getAttribute("Message"))
    {  
        out.println("<p>Succesful</p>");
    }

    else 
    {
        out.println("<p>Error reg</p>");

    }
    <%>

When I check the xml file the record is succesful but their's no "Succeful" paragraph in my html. It always retuns null, the "Error reg" paragraph.

Hi,

The first code seems correct, not really pretty, but correct. I would write :

    public boolean addIfAbsent(UserBean userBean) {
            if (users.stream().anyMatch(x -> x.getUsername().equals(userBean.getUsername()))) {
                return false;
            } else {
                users.add(userBean);
                return true;
            }
        }
    }

And I would call it in the if statement.

The second code seems to present two problems :
The first one is on the line 23 :
You have created a DepartmentBean called dp on line 14 and you have used it in the if condition on line 25, but the call on line 23 uses another object. dp is never used so its "f" is always false.

The second one is on line 25, you must use "==" to check equality, or just put dp.f because it's a boolean.

Good Luck.