wood burning stoves 2.0*
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
Author

Help with JSP and Form INput

Mike Br
Greenhorn

Joined: Nov 22, 2001
Posts: 20
Hiya,
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,
Mike
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61608
    
  67

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).
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Gordon Jenkinson
Greenhorn

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>
<% } %>
<b>Login</b></font></p>
<table width="41%" border="1" align="center" bordercolor="#000000">
<form action="loginCheck.jsp" method="POST">
<tr>
<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>
</tr>
<tr>
<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>
</tr>
<tr>
<td colspan="2" height="35">
<div align="center">
<font face="Verdana, Arial, Helvetica, sans-serif">
<input type="submit" name="Submit" value="Login">
</font></div>
</td>
</tr>
</form>
</table>
</HTML>
</BODY>

*************** 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>
<jsp:useBean id="dbm" scope="page" class="com.mwp.DatabaseManager" >
</jsp:useBean>

<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) {
user.setIpAddr(request.getRemoteHost());
// Got user. Now do they already have a session?
if (monitor.containsKey(user)) {
HttpSession oldSession = (HttpSession)monitor.get(user);
oldSession.invalidate();
monitor.remove(user);
}
session.setAttribute("user", user);
monitor.put(user, session);
System.out.println("Assigned new session for: " + user);
session.setMaxInactiveInterval(900);

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.
HTH
Gordon


Gordon Jenkinson
Mike Br
Greenhorn

Joined: Nov 22, 2001
Posts: 20
Thanks gordon, that looks really useful, i'll give that a go,
Mike
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with JSP and Form INput