Subash
Subash

Reputation: 7266

pass variable to javabean

I have a register.jsp file. It reads the variable from a form submitted as follows:

    <%
        if(request.getParameter("username") != null && request.getParameter("password") != null){
            String username = request.getParameter("username");
            String password = request.getParameter("password");
         }
     %> 

I have a java bean as follows.

public class RegistrationBean {


// Constructor
public RegistrationBean(){}  


public boolean login(String username, String password){
    boolean result = false;
    System.out.println("username");
    if(username.equals("username") && password.equals("admin")){
        result = true;
    }
    return result;
}


}

Now I am trying to call this login function in my jsp file as follows. I added the useBean at the top of my jsp file as well.

    <jsp:useBean id="register" class="registration.RegistrationBean" scope="session" />

    <%
        if(request.getParameter("username") != null && request.getParameter("password") != null){
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            if(register.login(username, password)){
              // do something
            }
     %> 

But I realised that The value of username and password isn't being passed to the bean. But when I print the value in .jsp file it had the appropriate value. Is there any reason why the value isn't being passed to the function in the login() function. At the momen its false no matter what I do.

Thanks.

Upvotes: 1

Views: 8173

Answers (2)

ashish rawat
ashish rawat

Reputation: 11

Attribute value request.getParameter("sn") is quoted with " which must be escaped when used within the value

above message is occur when i am running my page using value="<%=request.getParameter("val")%>"/>

Upvotes: 1

exception
exception

Reputation: 945

That should actually work. However, this is an alternate way of getting it to work:

<jsp:useBean id="register" class="registration.RegistrationBean" scope="session">
<jsp:setProperty name="register" property="username" value="<%=request.getParameter("username")%>"/>
<jsp:setProperty name="register" property="password" value="<%=request.getParameter("password")%>"/>
<jsp:useBean />

Add setters and getters for username and password in your RegistrationBean class. username and password will be class variables. Make login function as non-parameterized i.e. call it as register.login().

Upvotes: 0

Related Questions