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 .