I am creating a login page , where when user enters his username and password the the request is sent to the servlets first and then validation is done in a javabean . If validation results true then user is forworded to another page otherwise he remains on the same page .
I have tried to implement MVC model here .
But the problem is that in both the cases the user is forworded to another page . I am not able to figure out the problem . Please help . Here's my code .

1. index.jsp

<HTML>
<HEAD><TITLE>Library Management System</TITLE>
 </HEAD>
<BODY>
 <%@ include file = "header.html" %>
 <FORM name=loginF action=Login method=post> 
  <TABLE cellSpacing=0 cellPadding=5 align=center border=0>
  <TBODY>
   <TR>
    <TD><FONT color=#056796>Username:</FONT></TD>
    <TD><INPUT id=usrnm name=username ></TD></TR>
   <TR>
    <TD><FONT color=#056796>Password:</FONT></TD>
    <TD></LABEL><INPUT id=pwd name=password ></LABEL></TD></TR>
   </TBODY>
  </TABLE><P align=center><INPUT id=loginB type=submit value=Login name=loginB ></P>
 </FORM>
 <P align=center><FONT color=#056796>Not registered?<A href="registration.jsp">Sign Up</A></FONT></P></LABEL>
</BODY>
</HTML>

2.LoginBeanController.java

import java.io.Serializable;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LoginControllerServlet extends HttpServlet {
	public void init(){		
	}
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,
	IOException{
		String usrnm = request.getParameter("username");
		String pwd = request.getParameter("password");
		LoginBean loginB = new LoginBean();
		loginB.setUsername(usrnm);
		loginB.setPassword(pwd);
		
	        if(loginB.isValid()){
			RequestDispatcher rd = request.getRequestDispatcher("memberMain.jsp");
			rd.forward(request,response);
		}else{
			RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
			rd.forward(request,response);
		}
		
	}
	public void destroy(){
	}
}

3.LoginBean.java

import java.io.Serializable; //this allows javabean to be shared between diff components of web 
                             //applicatn ,even between server restarts

public class LoginBean implements Serializable {
	private String username;
	private String password;
	private boolean valid=false;
	
	public LoginBean(){
	}
	public String getUsername(){
		return username;
	}
	public void setUsername(String value){
		username=value;
	}
	public String getPassword(){
		return password;
	}
	public void setPassword(String value){
		password=value;
	}
	public boolean isValid(){
		valid=false;           //let by default
		if((username != null)&&(password != null)) {
			valid=true;			
		}
		return valid;
        }
}

Moreover I would like to know in what cases , in MVC model , View i.e jsp page can be made to communicate directly with the Model i.e javabean . Or it is necessary every time to use controller between them .

Your validation will always return true because of red marked. I think you wanted to say false

public boolean isValid(){
   valid=false;           //let by default
   if((username != null)&&(password != null)) {
	valid=true;			
   }
	return valid;
}

As for the second question you can pull Bean to use any time you need. Question is, would it hold any data... (in the meaning did you initialize the bean with some values or it will just return NULL)

Thanks peter_budo for replying . But even if I do not enter any value in the username and password textfields still then user is forworded to another page . I mean the output remains same if I enter values or not .
I am still not clear .please help

Oh silly, I sure do need break, but hell have to finish project first.

The bellow code illustrate what is your problem. String b is assign two double quotes and that is exactly what you receive if user does not provide username or password

public class StringTest 
{
    public static void main(String[] args) 
    {
        String a = "peter";
        String b = "";
        if(a.equals("peter"))
        {
        	System.out.println("a is peter");
        }
        if(b != null)
        {
        	System.out.println("b is not null");
        	System.out.println("however b length is "+b.length());
        }
    }
}

Return will be

a is peter
b is not null
however b length is 0

Therefore I would recommend to use length() method to see if there is something. Beside that when comparing string you should not use direct "==" or "!=" but use methods related to string like String.equlas(Object), String.equalsIgnoreCase(String) or String.compareTo(String).

Here is small tip on Strings.

The code worked when I did this instead of what written above

public boolean isValid(){
	valid=false;      //let by default
if((username != "")&&(password != ""))  {   valid=true;
........

The code worked when I did this instead of what written above

public boolean isValid(){
	valid=false;      //let by default
if((username != "")&&(password != ""))  {   valid=true;
........

I do not see any difference between this code and what you have written above for the isValid() method.

Also I don't think you have understood the concept which Peter is referring to.
Although he's already made it already as clear as possible, May be this piece of code might make you understand the difference between != (or ==) and equals() method in case of java strings.

/**
 * File Name : TestString.java
 * Here we just show the difference between
 * == (or !=) and the equals() method
 */

public class TestString {
  public static void main(String[] args){
    // Note the use of the String("") constructor instead
    // of directly initialising by ="HELLO" way, to stop the
    // JVM from optimising our code.
    String myString1=new String("HELLO");
    String myString2=new String("HELLO");

    if(myString1 == myString2) {
      System.out.println("Strings refer to the same object.");
    }
    if(myString1.equals(myString2)) {
      System.out.println("String contents are equal.");
    }

    myString1 = myString2;

    if(myString1 == myString2) {
      System.out.println("Strings refer to the same object.");
    }
  }
}

Should give you the output:-

stephen@linux-4f1r:~/Development/Java/daniweb> java TestString
String contents are equal.
Strings refer to the same object.
stephen@linux-4f1r:~/Development/Java/daniweb>

So == and != are used to check when two references are pointing to the same object, whereas the equals() method of the String(not Object) class actually checks if the strings are the same.

So your code username != "" will definitely not work when the username is left blank, you most probably have just not tested for that case.

Also in such cases try your code in a normal Java console program in case you have doubts about the workings of the basic Java features. Cause you quite clearly haven't yet grasped the concepts of references in Java.

The code worked when I did this instead of what written above

public boolean isValid(){
	valid=false;      //let by default
if((username != "")&&(password != ""))  {   valid=true;
........

Try this:

boolean user = getUsername().length() > 0;
boolean pass = getPassword().length() > 0;
if((user)&&(pass)) {
return true;
} else {
return false;
}

This question has already been answered. Start a new discussion instead.