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.