Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

validation  RSS feed

 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
some one please tell me where i did mistake given below...My request is not going to servlet....I provided html and servlet code below

1. <html><center>



2. <form name="f1"onsubmit="return validate()"action="./validate"method="get">

3. username: <input type="text"name="t1"id="t1">


4. password: <input type="password"name="p1"id="t2">


5. <input type="submit"value="sign in">

6. </form>

7. <script>

8. function validate()

9. {

10. if((f1.t1.value.length==0)||(f1.t2.value.length==0))

11. {

12. document.getElementById("demo").innerHTML='<h1>'+"invalid username and password"+''

13. }

14. return false;
}
</script>
</html>

servlet code:




import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class validate extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

String uname=request.getParameter("t1");

String pwd=request.getParameter("p1");

out.println(uname+"\n"+pwd);
}
}
thanks...
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really not have spaces between the attributes of the <form> tag, or is that a copy/paste error? If you don't have them, start with that.

Also, what is the "mistake given below"? How are you using this page, and what happens when you use it?
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also need <body> tags and a closing element for the <center> tag. Your <script> tag should have a "type" attribute.

Please use code tags instead of adding line numbers. Just click the "Code" button above and paste your code between the elements.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition,
I cannot see any element with id or name
'demo' in your html page.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code. It will automatically assign line number so you don't have to.
 
Henry Wong
author
Sheriff
Posts: 22851
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sujesh Katri,

The "report" button is to report an issue / post / rancher to a moderator. It is not used to reply to posts. Please use the "post reply" button below to reply to posts.

Henry
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i type <script type="javascript">then my javascript validation code is not executed but it send requestparameters to servlet....in my actual code i tyep<p id=demo>but here it is missed.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

if i type <script type="javascript">then my javascript validation code is not executed but it send requestparameters to servlet....in my actual code i tyep<p id=demo>but here it is missed.

Sujesh,
Try as much as possible to include in double quotes the value of your attributes. Example: <p id="demo">
and it is recommended that you write <script type="text/javascript"> Please post your html page again. Get it right before thinking of the servlet.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<html><center><p id="demo"></p>
<form name="f1"onsubmit="return validate()"action="./validate"method="get">
username: <input type="text"name="t1"id="t1"><br>
password: <input type="password"name="p1"id="t2"><br>
<input type="submit"value="sign in">
</form>
<script type="text/javascript">
function validate()
{
if((f1.t1.value.length==0)||(f1.t2.value.length==0))
{
document.getElementById("demo").innerHTML='<font color=red><h1>'+"invalid username and password"+'</font>'
}
return false;
}
</script>
</center>
</html>

in the above html code if i write type="text/javascript" my validation i.e if did n't write username and password that error msg won't be display on my browser window......if i remove type attribure then my validation running properly but parameters not going to servlet...some one please provide me solution ...thanks.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Please UseCodeTags when posting code

So many people have told you so. Which editor do you use?
and the following if executed will return false no matter what so your servlet will not get called.

so try to move the return false; inside the if statement like this

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)which environment you are using ?

2)Did you map that URL of Servlet class in web.xml file .
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using netbeans environment and i mapped url to web.xml....After moving return false into if block it is executed successfully....can i know the reason why it happens inside the if block only it is executing properly...Thank you very much.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the information clearly..Thanks
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
problem solved? or not?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!