File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Help with JSP and Form INput Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Help with JSP and Form INput" Watch "Help with JSP and Form INput" New topic

Help with JSP and Form INput

Mike Br

Joined: Nov 22, 2001
Posts: 20
I think I need to understand this once and for all!
I have a form on a JSP page which a user enters login details into. I want the results of their login, ie success, failure, prompt for password retry etc to appear back in the same window as the one they just pressed submit on (after being processed by some beans (which work nicely)). How is this done?? Obviously I can gather the information from the form but what then?? I was posting it to a servlet but then I lose the page content Ive got.
Whats the standard way of doing this??
Thanks for you help,
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

If after the servlet detects a failure mode you forward back to the original page with the form on it, the request parameters will still be on the request, and you can add attributes which identify the error condition.
If you are redirecting back to the form, your servlet can copy the form request parameters and place them on the request. (Though this will likely make the clear-text password visible in the address field of the browser).

[Asking smart questions] [About Bear] [Books by Bear]
Gordon Jenkinson

Joined: Feb 03, 2002
Posts: 14
Here's an example of how I've implemented it and it seems to works as you want it to.
********login.jsp *******
<%@ page import="com.mwp.*" errorPage="error.jsp" %>
<% String message = request.getParameter("message");
if ( message != null ) {
<p><%=message %></p>
<% } %>
<table width="41%" border="1" align="center" bordercolor="#000000">
<form action="loginCheck.jsp" method="POST">
<td height="35" bgcolor="#9999FF" width="18%"><font face="Verdana, Arial, Helvetica, sans-serif">Username</font></td>
<td height="35" bgcolor="#CCCCFF" width="82%">
<input type="text" name="username">
<td bgcolor="#9999FF" width="18%"><font face="Verdana, Arial, Helvetica, sans-serif">Password</font></td>
<td bgcolor="#CCCCFF" width="82%">
<input type="password" name="password">
<td colspan="2" height="35">
<div align="center">
<font face="Verdana, Arial, Helvetica, sans-serif">
<input type="submit" name="Submit" value="Login">

*************** loginCheck.jsp **************
@ page language="java"
import="com.mwp.*, java.util.*"
errorPage="error.jsp" %>
<jsp:useBean id="loginBean" scope="page" class="com.mwp.Login" >
<jsp:setProperty name="loginBean" property="*"/>
<jsp:useBean id="dbm" scope="page" class="com.mwp.DatabaseManager" >

<jsp:useBean id="monitor" scope="application" class="java.util.HashMap"/>
if ( loginBean.getUsername().length() == 0 ) {
String display = "login.jsp?message=PLease Enter Username and Try Again";
if ( loginBean.getUsername().length() == 0 ) {
String display = "login.jsp?message=Please Enter password and Try Again";
String display = "login.jsp?message=Invalid Login. Try Again";
User user = loginBean.authenticate(dbm.getConnection(session));
if (user != null) {
// Got user. Now do they already have a session?
if (monitor.containsKey(user)) {
HttpSession oldSession = (HttpSession)monitor.get(user);
session.setAttribute("user", user);
monitor.put(user, session);
System.out.println("Assigned new session for: " + user);

display = "customerSearch.jsp";
<jsp:forward page="<%= display %>"/>


The a session attribute "message" is used to pass the reason for the failure back to the Login.jsp page. The login bean is used to get the userId and password.

Gordon Jenkinson
Mike Br

Joined: Nov 22, 2001
Posts: 20
Thanks gordon, that looks really useful, i'll give that a go,
I agree. Here's the link:
subject: Help with JSP and Form INput
It's not a secret anymore!