将对象从servlet传递到另一个servlet

时间:2014-03-02 13:46:00

标签: java servlets attributes request set

如何将对象从servlet传递到另一个servlet?

public class Account
{
    public Account(String username, String password, String lastname, String firstname){
        this.username = username;
        this.password = password;
        this.firstname = firstname;
        this.lastname = lastname;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getFirstname() {
        return firstname;
    }

    public String getLastname() {
        return lastname;
    }


    private String username;
    private String password;
    private String firstname;
    private String lastname;
    public boolean equals(Object obj)
    {
        boolean result = false;
        if (obj != null && obj instanceof Account)
        {
            Account p = (Account)obj;
            if ( getUsername().equals(p.getUsername()) 
              && getPassword() == p.getPassword() )
            {
                result = true;
            }
        }
        return result;
    }
}

的index.html

<html><head><title></title></head><body>
                <div><h3>Enter Username and Password</h3></div>
                <div class="form-group">
                     <label for="Username">Username</label><input type="text" class="form-control" id="Username" name="Username" />
                </div>
                <div class="form-group">
                     <label for="Password">Password</label><input type="text" class="form-control" id="Password" name="Password" />
                </div>
</body></html

&GT;

Servlet1

Set<Account> acc = new HashSet<Account>();

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

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        acc.add(new Account("test1", "pass123", "Boom", "Bang"));
            acc.add(new Account("test2", "pass123", "Beam", "Beng"));
        PrintWriter out = response.getWriter();
        String username = "";
        String password = "";


    if(request.getParameter("Username") != null){
        username = request.getParameter("Username");
    }
    if(request.getParameter("Password") != null){
        password = request.getParameter("Password");
    }

    Account act1 = new Account(username, password, "", "");
            System.out.print(username+password); // test if the username and password change based on the parameter

        out.print("<html>");
        out.print("<head><link href='layoutit/css/bootstrap.min.css' rel='stylesheet'>"
                + "<link href='layoutit/css/style.css' rel='stylesheet'>");
        out.print("<title></title>");
        out.print("</head>");
        out.print("<body");
        if(acc.contains(act1)){ 
            request.setAttribute("acc1", act1);
                out.print("<h3>Click <a href='displayuser.html'>here</a> to continue.</h3>");
        }
    else
        out.print("<h1>Invalid user account entered.Click <a href='index.html'>here</a> to login.</h1>");
        out.print("</body>");
        out.print("</hmtl>");
        out.close();
    }

Servlet2

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

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html"); //setting the MIME of this page

        PrintWriter out = response.getWriter();
                Account act1 = (Account) request.getAttribute("acc1");
                System.out.println(act1.getUsername());
        }

1 个答案:

答案 0 :(得分:2)

您可以在请求对象中使用setAttribute()方法将其转发到下一个资源

request.setAttribute("OBJECT_NAME", object);

//forward the request to Servlet2
RequestDispatcher reqDispatcher = req.getRequestDispatcher("pathToyourServlet2");
reqDispatcher.forward(req, res);

Servlet2中你可以得到对象:

request.getAttribute("OBJECT_NAME");