Solved

submitting form with enter key in JSP

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

820 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