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.
That should actually work.
However, this is an alternate way of getting it to work:
Add setters and getters for
usernameandpasswordin yourRegistrationBeanclass.usernameandpasswordwill be class variables. Makeloginfunction as non-parameterized i.e. call it asregister.login().