Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Function addiFAbssent in class DepartmentBean check if element already exist in container:

What I have tried:

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>Successful</p>");
    }
    else 
    {
        out.println("<p>Error reg</p>");
    }
    <%>


When I check the xml file the record is succesful but their's no "Successful" paragraph in my html. It always retuns null, the "Error reg" paragraph.
Posted
Updated 6-May-18 4:14am
v2
Comments
[no name] 6-May-18 10:29am    
1.) What is with departmentBean vs. dp. The later is only declared but not involved?
2.) Also this Looks suspecious: if(dp.f = true). Should this not be if(dp.f == true)?

1 solution

Java
        String Message = "msg";
        /*Set attribute msg of Message*/
        request.setAttribute(Message, "msg"); // so the attribute name is "msg"

...

    String s = "msg";
    if (s == request.getAttribute("Message")) // but the attribute name is not "Message"

Your attribute names are different. Use names that match, and content that is more meaningful.
 
Share this answer
 
v3
Comments
[no name] 6-May-18 10:31am    
For me it Looks more that f reflects added or not
Richard MacCutchan 6-May-18 10:51am    
You are correct, but see my updated answer.
[no name] 6-May-18 11:07am    
I don't know Java so I can't say correct or not. For me it Looks like "Message" is the correct Attribute, OP does this:
request.setAttribute(Message, "msg");

See also my comments to the question. But as mentioned I'm lost in this field
Richard MacCutchan 6-May-18 11:36am    
Yes, but in the call to setAttribute he uses the variable Message as the parameter. But the value of Message at that point is the string "msg". So it is the same as writing request.setAttribute("msg", "msg");. Which is a different attribute name to "Message".

BTW much of Java is very similar to C, C++ and C#; so if you know one of the other languages it should not be too difficult.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900