Solved

submitting form with enter key in JSP

Posted on 2003-12-08
5
1,118 Views
Last Modified: 2012-06-27
I want to do the same action as pressing the login button when a user clicks return/enter in the input box. When user clicks the login button - page is reloaded- and embedded java executes the code after 'if (strbtnSubmit != null) {' condition. Thanks

heres my code (it is a jsp page)

<%@page import="utilities.*"%>


<head>


<%

String strbtnSubmit = request.getParameter("btnSubmit");

// submit - refresh the page
try{
if (strbtnSubmit != null) {
        String pass1 = request.getParameter("pass");
        utilities.Password password = new utilities.Password();
        HttpSession session1 = request.getSession(true);

            if (password.checkPassword(pass1)) {
           
               
                session1.setAttribute("matchingPassword", request.getParameter("pass"));
                session1.setAttribute("sessionID", session1.getId());
                String redirectURL = "/im/menu/Control.jsp";
                response.sendRedirect(redirectURL);
             } else { session1.invalidate(); }
          }

} catch (Exception exception) {throw exception;}
%>


</script>

</head>

<body leftmargin="0" topmargin="30" marginwidth="0" marginheight="0"></head>
<body><font face="verdana">

<table bgcolor="#FFFFCC" width="100%" border="0" cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr>
    <td width="34" rowspan="3" valign="top" bgcolor="#FFFFCC"><!--DWLayoutEmptyCell-->&nbsp;</td>
    <td width="100%" rowspan="4" valign="top"><form name="loginform" action="/im/forms/Login.jsp">
        <table bgcolor="#000000" border="0">
          <!--DWLayoutTable-->
          <tr>
            <td width="223" rowspan="3"> <img src="/im/menu/im.jpg" width="217" height="148">
            </td>
            <td width="90" rowspan="3"> <font color="silver">Password:</font>
            </td>
            <td height="63" colspan="2" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
            <td width="18" rowspan="3">&nbsp;&nbsp;&nbsp;</td>
          </tr>
          <tr>
            <td height="33" colspan="2" valign="top"><input style="color:black; background-color:grey; border-width: 1px; border-color:black" type="password" name="pass" size="15"></td>
          </tr>
          <tr>
            <td width="39" height="50" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
            <td width="50" valign="top"><input style="color:black; background-color:grey; border-width: 1px; border-color:black" align="right" type="submit" value="Login" name="btnSubmit"></td>
          </tr>
          <tr>
            <td height="26" align="right"><!--DWLayoutEmptyCell-->&nbsp; </td>
            <td colspan="4" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
          </tr>
        </table>
      </form></td>
    <td width="238" height="35">&nbsp;</td>
    <td width="112">&nbsp;</td>
  </tr>
  <tr>
    <td height="26" valign="top">entry portal | <font color="orange"><strong>IM</strong></font></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="106"></td>
    <td></td>
  </tr>
  <tr>
    <td height="15"></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td height="0"></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td height="1"><img src="/im/menu/spacer.gif" alt="" width="34" height="1"></td>
    <td></td>
    <td><img src="/im/menu/spacer.gif" alt="" width="238" height="1"></td>
    <td><img src="/im/menu/spacer.gif" alt="" width="112" height="1"></td>
  </tr>
</table>
</body>


</html>

0
Comment
Question by:cratnayake_pa
5 Comments
 

Author Comment

by:cratnayake_pa
ID: 9901754
btw...

<td height="33" colspan="2" valign="top"><input style="color:black; background-color:grey; border-width: 1px; border-color:black" type="password" name="pass" size="15"></td>


is the input box which the user enters their password- upon enter in this box - the form is submitted same as pressing the login button underneath

at present moment.. when enter is pressed after entering correct password.. the page just reloads with

http://localhost:8081/im/forms/Login.jsp?pass=

in the URL (IE 6.0)
0
 

Author Comment

by:cratnayake_pa
ID: 9901755
or
http://localhost:8081/im/forms/Login.jsp?pass=mypassowrd

(where mypassword varies depending on what you have entered into the input box)
0
 
LVL 10

Expert Comment

by:D_M_D
ID: 9903732
You can use javaScript to capture the enter keystroke...

<script language="JavaScript">
if (document.all || document.layers){
      document.onkeypress=pressed; // IE4+ & NS4+
      
}
function pressed(e) {
     var Val='';
     if (document.layers){
         Val='' + e.which;
     }else{
         Val = window.event.keyCode;
     }
     if (Val == 13){
        document.YourFormName.submit();
     }
}
</script>

Just add your Form Name above...

Hope this helps...

--------
D_M_D
0
 
LVL 6

Accepted Solution

by:
DoppyNL earned 200 total points
ID: 9903989
I would strongly recommend using the post-method for forms that contain passwords.
That way the password entered won't be in the URL wich is **MUCH MORE SECURE**
0
 
LVL 6

Expert Comment

by:jwenting
ID: 9911593
If your HTML form has one or more submit buttons, the enter key will trigger the first of those automatically (unless you remap the enter key to do something else).
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question