PDA

View Full Version : unable to read from and generate html file output using JSP


sunil_2215
03-27-2009, 06:54 PM
Hello friends...

I am a newbie to Javascript and JSP.
I wish to create a jsp file to read the inputs from an html (registration form called member.html), check validity of inputs using javascript and finally generate an ouput as an html file displaying the user inputs.


Following is the code for the member.html file

<HTML>
<TITLE>New member application form</TITLE>
<script language="JavaScript" src="javaScript.js" type="text/javascript"></script>

<!--<HEAD><H2><P align = "center">Welcome...</P></H2>-->
<H2><P align = "center"> Member Registration Form </p></H2>

</HEAD>
<HR>
<BODY>

<form method="post" action="ServerPage.jsp" name = "myform" >

<p><Strong>1. Tell us about yourself...</Strong></p>
First Name :<INPUT TYPE="text" NAME="firstName" SIZE="20" hspace="20"><br>
Last Name&nbsp; :<INPUT TYPE="text" NAME="lastName" SIZE="20"><br>
</p>

<p>
<Strong><em>Thanks for registering !</em></Strong><br>
<input onclick="test()" name="button" value="Submit" type="button">
<INPUT TYPE="reset" VALUE="Reset!"></H3></FONT>
</p>

</form>
</body>
</html>


Following is the code for the javascript.js file.


function test(){

var un = document.myform.firstName.value;
un = un.replace(/^\s+|\s+$/g,""); // strip leading and trailing spaces
un = un.replace(/[^a-z-]/gi,""); // strip characters apart from a-z apostrophe hyphen
if (un.length < 2) {
alert ("Enter valid first name...must be at least two characters");
document.myform.firstName.focus();
return;
}

var visn = document.myform.lastName.value;
visn = visn.replace(/^\s+|\s+$/g,""); // strip leading and trailing spaces
visn = visn.replace(/[^a-z-]/gi,""); // strip characters apart from a-z apostrophe hyphen
if (visn.length < 2) {
alert ("Last name must be at least two characters");
document.myform.lastName.focus();
return;
}
submit();

}


and following is the code for the ServerPage.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>response</title>
</head>
<body>
<% String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
%>

<h1>Hello <%= firstName %>! &nbsp;Welcome.</h1>
<h2>Your last name is <%= lastName %>.</h2>
</body>
</html>


The error i experience is that nothing happens when I click submit button...;)
I have installed Tomcat 6.0 and do not seem to have issues with that.
Folks, I do not wish to hijack an existing thread on this issue...but could not find an existing thread, hence a new thread.
Please advice.
Thanks in advance.

brad211987
03-27-2009, 08:28 PM
Your calling a submit method, but it doesn't really belong to anything.

Try something like document.myform.submit();

sunil_2215
03-27-2009, 08:45 PM
Thanks for your inputs.
I tried changing the submit() method...but it's still not working.
Please advice.

brad211987
03-27-2009, 08:49 PM
Seems to work fine for me now. Here is the modified version of your javascript file:


function test(){

var un = document.myform.firstName.value;
un = un.replace(/^\s+|\s+$/g,""); // strip leading and trailing spaces
un = un.replace(/[^a-z-]/gi,""); // strip characters apart from a-z apostrophe hyphen
if (un.length < 2) {
alert ("Enter valid first name...must be at least two characters");
document.myform.firstName.focus();
return;
}

var visn = document.myform.lastName.value;
visn = visn.replace(/^\s+|\s+$/g,""); // strip leading and trailing spaces
visn = visn.replace(/[^a-z-]/gi,""); // strip characters apart from a-z apostrophe hyphen
if (visn.length < 2) {
alert ("Last name must be at least two characters");
document.myform.lastName.focus();
return;
}
document.myform.submit();

}

sunil_2215
03-27-2009, 09:20 PM
Thanks a lot Brad...I restarted Tomcat and now it works fine for me too.
I appreciate the time you took to debug / check my code.
If I may ask, could you please advice on how to display the output in an html file?

brad211987
03-27-2009, 09:22 PM
I'm not sure exactly what you are asking. To look at the output of an HTML file, just load it in a web browser.