• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Help with JSP and Form INput

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks gordon, that looks really useful, i'll give that a go,
Mike
 
Legend has it that if you rub the right tiny ad, a genie comes out.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic