How to mimic tomcat default login form

I am trying to mimic the default login form in Tomcat which uses:

    <form method="POST" action="j_security_check">
      <table>
        <tr><td>Name:</td><td><input type="text" name="j_username" /></td></tr>
        <tr><td>Password:</td><td><input type="password" name="j_password" /></td></tr>
        <tr><td colspan="2"><input type="submit" value="Login" /></td></tr>
      </table>
    </form>

I tried using the following:

login.xml:

<?xml version="1.0" encoding="UTF-8"?>
<items>
  <item type="settings" position="label-left" labelWidth="120" inputWidth="150" noteWidth="200"/>
  <item type="fieldset" name="data" inputWidth="auto" label="Login">
    <item type="input" value="" label="Username" name="j_username" validate="NotEmpty" info="true">
      <note>Enter username</note>
    </item>
    <item type="password" value="" label="Password" name="j_password" validate="NotEmpty" info="1">
      <note width="150">Enter password</note>
    </item>
    <item type="button" value="Login" name="login"/>
  </item>
</items>

Page code:

<script>
  myForm = new dhtmlXForm("myForm");
  myForm.loadStruct("login.xml?e=" + new Date().getTime());
  myform.attachEvent("onButtonClick", function(name, command) {
    this.send("j_security_check", "post");
  });
</script>

<div id="myForm" style="height:500px;"></div>

What happens is it seems to login ok but does not re-direct to the secured page.

Hello
First of all check a form name:
myForm
myForm
myform

hi

correct, form submit data using ajax, you need to redirect automaticaly, for example:

myform.attachEvent("onButtonClick", function(name, command) { this.send("j_security_check", "post", function(r){ // assuming response in json, for example {status: "ok", error: "none"} // for xml you need r.xmlDoc.responseXML var t = null; try { eval("t="+r.xmlDoc.responseText);} catch(e) {}; if (t != null) { if (t.status == "ok") { document.location.href = "secret_page.html"; } else { alert("incorrect login or password"); } } }); });

Thanks but doesn’t seem to work correctly either. It’s the way tomcat redirects post login.

However, I stumbled across the answer in another area of the docs. What you have to do is to wrap the div with a form element like:

<form id="frm" action="j_security_check" method="post"><div id="loginForm" style="height:200px;"></div></form>

And then:

loginForm = new dhtmlXForm("loginForm"); ... loginForm.attachEvent("onButtonClick", function(name) { if (name=="login") { if (loginForm.validate()) document.forms["frm"].submit(); } });

Works a charm now :slight_smile: