Solved

submitting form with enter key in JSP

Posted on 2003-12-08
5
1,116 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
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…
This video teaches users how to migrate an existing Wordpress website to a new domain.

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now