Java Reference
In-Depth Information
<tr>
<td></td>
<td><input type="submit" value="SUBMIT" name="submitButton"></td>
</tr>
</form>
</body>
</html>
The only significant difference between login.html and LoginForm.jsp is that the action parameter
has been changed to point to ProcessLogin.jsp . The UserName and Password parameters are
passed to the new action method just as they are to LoginServlet.
A simple JSP page that picks up the HTTP request parameters and echoes them to the client is shown
in Listing 12-5 . The example illustrates how you can combine HTML and embedded Java in a single
JSP page using a number of JSP-specific tags:
 
<% %> delimiters for in-line Java scriptlets
 
<%=expression %> output the evaluated value of the expression
 
<%@ page %> directive defining page properties
Listing 12-5: Using a JSP page to display CGI parameters
<HTML>
<HEAD>
<TITLE>
Display Login Parameters
</TITLE>
</HEAD>
<BODY>
<%@ page language="java"%>
<%
String userName=request.getParameter("username");
String password=request.getParameter("password");
%>
User Name = <%=userName%><p/>
Password = <%=password%><p/>
</BODY>
</HTML>
This example can be extended by including the SQL query code used in the JDBC servlet example of
Listing 12-3 . This approach combines both the JDBC code and the HTML generation, emulating the
behavior of the original servlet example. However, writing the JSP page this way is messy, since it
combines Java and HTML in a single page.
A much better way to structure the JSP page is to encapsulate the JDBC logic in a JavaBean, which
acts as the model in a model-view-controller (MVC) structure. The view is provided by a JSP page,
using the <jsp:forward /> directive. The action method of the LoginForm.jsp calls a controller
JSP, which loads the bean, passes it the request parameters, and forwards the user to the appropriate
view JSP, depending on the results of the SQL the JavaBean executes.
Before implementing the MVC approach to handling the login form, it is worth reviewing the use of
JavaBeans with JSP pages. The next few paragraphs give a brief overview.
Search WWH ::




Custom Search