Java Reference
In-Depth Information
Creating and Deploying a JDBC LoginBean
The simple example of Listings 12-6 and 12-7 provide the basics of a JSP-based and JavaBean-based
login form handler. Using the LoginForm.jsp of Listing 12-4 , the user enters his or her name and
password and clicks the Submit button to call ProcessLogin.jsp .
ProcessLogin.jsp is an extended version of the JSP page illustrated in Listing 12-6 . These are the
main differences:
 
ProcessLogin.jsp has no HTML content. It acts as a pure controller.
 
A <jsp:forward /> tag is used to display the view portion of the MVC structure.
Like the JSP page illustrated in Listing 12-6 , the ProcessLogin.jsp relies on a JavaBean to handle
the business logic. In this instance, the bean incorporates the JDBC code illustrated in the servlet
example of Listing 12-3 .
Since the functionality of the ProcessLogin.jsp page is reduced to launching the JavaBean and
interpreting the response, the resulting MVC controller is simple and easily understood. All it does is
accept the form inputs, pass them to the LoginBean, and select one of three pages to forward the user
to, depending on his or her login status. The resulting JSP code is shown in Listing 12-8 .
Listing 12-8: ProcessLogin.jsp
<%@ page language="java"%>
<jsp:useBean id="LoginBean" class="JavaDatabaseBible.ch12.LoginBean"/>
<jsp:setProperty name="LoginBean" property="*"/>
<%
String status = LoginBean.validate();
String nextPage = "MemberWelcome.jsp";
if(status.equals("New Member")) nextPage = "NewMemberForm.jsp";
if(status.equals("Bad Password")) nextPage = "BadPasswordForm.jsp";
%>
<jsp:forward page="<%=nextPage%>"/>
Like the servlet example of Listing 12-3 , the JSP version uses page forwarding. In Java Server Pages,
this function is implemented by the <jsp:forward/> tag.
The LoginBean is also relatively simple. Unlike the servlet, which incorporates a certain amount of
HTML generation and other overhead, the LoginBean is simply a logic block. Setup is handled when the
bean is instantiated, and the JSP page sets username and password. Listing 12-9 shows the simplicity
of the LoginBean.
Listing 12-9: LoginBean
package JavaDatabaseBible.ch12;
import java.sql.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginBean extends java.lang.Object{
Search WWH ::




Custom Search