Advertisement
Guest User

Untitled

a guest
May 6th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. // This is a function in class DepartmentBean which check if element already exist in container:
  2.  
  3. bool f;
  4. public void addIfAbsent(UserBean userBean) {
  5.             if (users.stream().anyMatch(x -> x.getUsername().equals(userBean.getUsername()))) {
  6.                 f = false;
  7.             } else {
  8.                 f = true;
  9.                 users.add(userBean);
  10.             }
  11.         }
  12.     }
  13.  
  14. //In my servlet I am trying to send Message of succeseful registration(if f=false, their's no username with the same name):
  15.  
  16. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  17.     try {
  18.                 doAll(request, response);
  19.             } catch (Exception e) {
  20.                 // TODO Auto-generated catch block
  21.                 e.printStackTrace();
  22.             }
  23.         }
  24. public static void doAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
  25.              DepartmentBean dp = new DepartmentBean();
  26.              UserBean user = new UserBean();
  27.              DepartmentBean departmentBean = read();
  28.                 String userName = request.getParameter("username");
  29.                 String password = request.getParameter("password");
  30.                 user.setPassowrd(password);
  31.                 user.setUsername(userName);
  32.                 departmentBean.addIfAbsent(user);
  33.     if(dp.f = true)
  34.     {
  35.         String Message = "msg";
  36.         /*Set attribute msg of Message*/
  37.         request.setAttribute(Message, "msg");
  38.         RequestDispatcher rd=request.getRequestDispatcher("index.jsp");
  39.          rd.forward(request, response);
  40.          write(departmentBean);
  41.     }
  42.         }
  43.         public static DepartmentBean read() throws JAXBException {
  44.             JAXBContext context = JAXBContext.newInstance(DepartmentBean.class, UserBean.class);
  45.             Unmarshaller unmarshaller = context.createUnmarshaller();
  46.             return (DepartmentBean) unmarshaller.unmarshal(new StreamSource(new File("1.xml")));
  47.         }
  48.         public static void write(DepartmentBean department) throws JAXBException {
  49.             JAXBContext context = JAXBContext.newInstance(DepartmentBean.class, UserBean.class);
  50.             Marshaller marshaller = context.createMarshaller();
  51.             marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  52.             marshaller.marshal(department, new File("1.xml"));
  53.         }
  54.     }
  55. // In my html I added this jsp code:
  56.  
  57. <%>
  58.     String s = "msg";
  59.     if (s == request.getAttribute("Message"))
  60.     {  
  61.         out.println("<p>Succesful</p>");
  62.     }
  63.     else
  64.     {
  65.         out.println("<p>Error reg</p>");
  66.     }
  67.     <%>
  68.    
  69.    
  70.   /*  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. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement