Solved

submitting form with enter key in JSP

Posted on 2003-12-08
5
1,122 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

737 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