Javascript form post using a JSP page

Hello everyone.  

I'm stumped.  I'm sending a login and encrypted password from an ASP.NET page to a JSP page via the querystring.  I check the querystring for the data, and then decrypt the password and fill in the form boxes with the info.

After I do this, I want to auto-post the form so it logs the user in automatically.  I'm having trouble trying to get javascript to post the form.

The error I'm getting is:  'document.forms.formTest' is null or not an object

Appreciate any help.
<%
	String strDecryptedPassword = "";
	String strLogin = "";
	try
	{
		if(request.getParameter("login") != "" && request.getParameter("password") != "")
		{
			strLogin = request.getParameter("login");
			strDecryptedPassword = DecryptPassword(request.getParameter("password"));
			%>
			<script language="javascript">
				document.forms['formTest'].submit();
	                </script>
			<%
		}
	}
	catch(Exception e)
	{
		strLogin = "";
		strDecryptedPassword = "";
	}
%>
 
<FORM name="formTest" METHOD=POST ACTION="j_security_check">
<input type="text" size="20" name="j_username" id="j_username" class="field" value='<%=strLogin%>'/>
<input type="password" size="20" name="j_password" class="field" value='<%=strDecryptedPassword%>'/>
<input type="submit" name="login" value="Login" class="formButton"/></div>
</form>

Open in new window

evertithAsked:
Who is Participating?
 
contactkarthiConnect With a Mentor Commented:
function name was wrong

here is the correct one

function checkVal() { 
       if(<%=(!request.getParameter("login").equlas("") && !request.getParameter("password").equlas(""));>)
{
document.formTest.submit();
} 

} 

<body onload = "checkVal()" >

Open in new window

0
 
bobbit31Commented:
Hello evertith,

try:

<FORM ID="formTest" METHOD=POST ACTION="j_security_check">

Regards,

bobbit31
0
 
contactkarthiCommented:
if(request.getParameter("login") != "" && request.getParameter("password") != "")

should be replaced as

if(!request.getParameter("login").equlas("") && !request.getParameter("password").equlas(""))
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
bobbit31Commented:
nevermind my first post...

i think the problem is that your form isn't rendered at the time which the jsp code is run... what happens if you put the jsp stuff AFTER the form declaration?
0
 
contactkarthiCommented:
document.forms['formTest'].submit();

should be

 document.formTest.submit();
0
 
contactkarthiCommented:
oops

just having submit in javascript will not submit the page

i will ask you to call a script function on page load and check if the values are there. if it is there then submit it

something like below

function changeVal() {

       if(<%=(!request.getParameter("login").equlas("") && !request.getParameter("password").equlas(""));>)
{
document.formTest.submit();
}


}


<body onload = "checkVal()" >
0
 
evertithAuthor Commented:
Appreciate the quick responses!  Worked like a charm.  I altered your code just a bit to fit my needs, but the logic was dead on.  Appreciate the push over the hump!
0
All Courses

From novice to tech pro — start learning today.