How do I call this method from a JavaBean in JSP using scriplets?

I have a java bean class called “diaryApplication” which has the following code. My question is how do I call the saveUsers() method using jsp or scriptlets so that i can add another user to my xml??. My jsp contains the usual name,email, password etc etc.

I also have a users javabean which has an arraylist code allowing me to add users to an xml code everytime a user registers or signs up, this code is below diaryapplication.
I also have a “user” Java bean the code is below that one.

    package uts.wsd;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;

    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Unmarshaller;

    public class DiaryApplication {

        private String filePath;
        private Users users;
        public DiaryApplication(String filepath, Users users) {
            super();
            this.filePath = filepath;
            this.users = users;
        }
        public DiaryApplication() {
            super();
            // TODO Auto-generated constructor stub
        }
        public String getFilepath() {
            return filePath;
        }
        public void setFilePath(String filePath) throws JAXBException, IOException {
            this.filePath = filePath;
            // This is the file path given to us.
            // We should use it

            // Load the users from the XML file...
            JAXBContext jc = JAXBContext.newInstance(Users.class);
            Unmarshaller u = jc.createUnmarshaller();
            FileInputStream fin = new FileInputStream(filePath); // use the given file path
            users = (Users)u.unmarshal(fin); // This loads the "users" object
            fin.close();
        }
        public Users getUsers() {
            return users;
        }
        public void setUsers(Users users) {
            this.users = users;
        }
        // this is the code im trying to call???
        public void saveUsers() throws JAXBException, IOException{
            // Create the unmarshaller
            JAXBContext jc = JAXBContext.newInstance(Users.class);
            Unmarshaller u = jc.createUnmarshaller();

            // Now unmarshal the object from the file
            FileInputStream fin = new FileInputStream(filePath);
            users = (Users)u.unmarshal(fin); // This loads the "shop" object
            fin.close();
        }
    }

//// the code for users is below

    package uts.wsd;

    import java.util.*;
    import java.io.Serializable;
    import javax.xml.bind.annotation.*;

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement(name = "users")
    public class Users implements Serializable {
        // The list of user elements does NOT have an extra wrapper element.
        // See the comment in the XML file, and compare to the bookshop example.
        @XmlElement(name = "user")
        private ArrayList<User> list = new ArrayList<User>();

        public ArrayList<User> getList() {
            return list;
        }
        public void addUser(User user) {
            list.add(user);
        }
        public void removeUser(User user) {
            list.remove(user);
        }
        public User login(String email, String password) {
            // For each user in the list...
            for (User user : list) {
                if (user.getEmail().equals(email) && user.getPassword().equals(password))
                    return user; // Login correct. Return this user.
            }
            return null; // Login incorrect. Return null.
        }
    }

////////////User javabean

package uts.wsd;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
public class User implements Serializable {

    @XmlElement(name = "email")
    private String email;

    @XmlElement(name = "name")
    private String name;

    @XmlElement(name = "password")
    private String password;

    @XmlElement(name = "gender")
    private String gender;

    @XmlElement(name = " favouritecolour")
    private String favouritecolour;

    public User() {
        super();
        // TODO Auto-generated constructor stub
    }

    public User(String email, String name, String password, String gender, String favouritecolour) {
        super();
        this.email = email;
        this.name = name;
        this.password = password;
        this.gender = gender;
        this.favouritecolour = favouritecolour;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getFavouritecolour() {
        return favouritecolour;
    }

    public void setFavouritecolour(String favouritecolour) {
        this.favouritecolour = favouritecolour;
    }

}


Source: jsp

Leave a Reply