File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with JSP and Form INput

 
Mike Br
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64188
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gordon Jenkinson
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike Br
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks gordon, that looks really useful, i'll give that a go,
Mike
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic