943,686 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 2421
  • JSP RSS
Dec 2nd, 2008
0

[ask]validation for user name registration

Expand Post »
hallo...this is my first post..

i want to validation my registration..
if there are anyone to regis but the username is already(in database), there are message " sorry, choose the different name please"

i have no idea, i have tried but always failed...

o..yeah i use ms.accsess for my database..and JSP ( not jsp 2 or something like that)
help me please...
i'm so poor in jsp & access ( newbie)

and please tell me where the false from my script..
sorry bad english...
Attached Files
File Type: doc registration.doc (33.5 KB, 117 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kardus is offline Offline
2 posts
since Dec 2008
Dec 2nd, 2008
0

Re: [ask]validation for user name registration

JSP Syntax (Toggle Plain Text)
  1. register.jsp
  2.  
  3. <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  8. <title>Untitled Document</title>
  9. <style type="text/css">
  10. <!--
  11. body {
  12. background-image: url();
  13. }
  14. -->
  15. </style></head>
  16.  
  17. <body>
  18. <table width="800" height="1028" border="0" align="center">
  19. <tr>
  20. <td width="795" height="134"><img src="banner2.gif" width="795" height="95" align="top" /><br />
  21. <img src="home.gif" width="132" height="35" /><img src="team.gif" width="132" height="35" /><img src="players.gif" width="132" height="35" /><img src="message.gif" width="132" height="35" /><img src="tranfer.gif" width="132" height="35" /><img src="about.gif" width="135" height="35" /></td>
  22. </tr>
  23. <tr>
  24. <td height="788" align="left" valign="top"><form id="form1" name="form1" method="post" action="doregister.jsp">
  25. <p>&nbsp;</p>
  26. <table width="200" border="1">
  27. <tr>
  28. <td colspan="2"><div align="center">Register</div></td>
  29. </tr>
  30. <tr>
  31. <td width="83">Username</td>
  32. <td width="101"><label>
  33. <input type="text" name="txtuser" id="txtuser" />
  34. </label></td>
  35. </tr>
  36. <tr>
  37. <td>Password</td>
  38. <td><label>
  39. <input type="password" name="txtpass" id="txtpass" />
  40. </label></td>
  41. </tr>
  42. <tr>
  43. <td>Age</td>
  44. <td><label>
  45. <input type="text" name="txtage" id="txtage" />
  46. </label></td>
  47. </tr>
  48. <tr>
  49. <td>Club</td>
  50. <td><label>
  51. <select name="selectclub" id="selectclub">
  52. <option>Manchaster United</option>
  53. <option>Inter Milan</option>
  54. </select>
  55. </label></td>
  56. </tr>
  57. <tr>
  58. <td colspan="2"><label>
  59. <div align="center">
  60. <input type="submit" name="button" id="button" value="Submit" />
  61. </div>
  62. </label></td>
  63. </tr>
  64. <tr>
  65. <td height="62" colspan="2"> <%
  66. String error = request.getParameter("err");
  67. if(error!=null) out.println(error);
  68. %></td>
  69. </tr>
  70. </table>
  71. </form>
  72. <p> </p></td>
  73. </tr>
  74. <tr>
  75. <td height="80" align="left" valign="top"><p>&nbsp;</p>
  76. <p>
  77. </p></td>
  78. </tr>
  79. </table>
  80. </body>
  81. </html>
  82.  
  83.  
  84. doregister.jsp
  85.  
  86. <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
  87. <%@ include file ="konek.jsp" %>
  88.  
  89. <%
  90. String user = request.getParameter("txtuser");
  91. String pass = request.getParameter("txtpass");
  92. String age = request.getParameter("txtage");
  93. String query = null;
  94.  
  95.  
  96. if(user == null || user.equals(""))
  97. {
  98. response.sendRedirect("register.jsp?err = please fill the username");
  99. }
  100.  
  101. else if (user != null)
  102. {
  103. query = "select username from msUser where username like '%"+user+"%'";
  104. }
  105.  
  106. else if(pass == null || pass.equals(""))
  107. {
  108. response.sendRedirect("register.jsp?err = please fill the pass");
  109. }
  110. else if(age == null || age.equals(""))
  111. {
  112. response.sendRedirect("register.jsp?err = please fill the age");
  113. }
  114.  
  115.  
  116. else
  117. {
  118. if(user == query)
  119. {
  120. response.sendRedirect("register.jsp?err = please choose a diffrent user name");
  121. }
  122. else if(user != query)
  123. {
  124. st.executeUpdate("insert into msUser values('"+user+"','"+pass+"',"+age+")");
  125. con.close();
  126. response.sendRedirect("home.jsp");
  127. }
  128. }
  129.  
  130. %>

sorry, no need to download the attachement...here is the code..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kardus is offline Offline
2 posts
since Dec 2008
Dec 3rd, 2008
0

Re: [ask]validation for user name registration

First point I suggest is read the Read-Me thread which is the first thread in the JSP forum, You are performing database operations from directly withing your JSP page which is considered a bad practice, the above thread will guide you on how to go about organizing your code nicely so that when you get on real world projects in JSP you will be thorough with the right practice.

Now let us check how your coding is going on. First thing I see is you are failing to consider all the circumstances and you have just not traced mentally how your if and else if statements will be executed.

for ex consider this :-
java Syntax (Toggle Plain Text)
  1. if(user == null || user.equals(""))
  2. {
  3. response.sendRedirect("register.jsp?err = please fill the username");
  4. }
  5. else if (user != null)
  6. {
  7. query = "select username from msUser where username like '%"+user+"%'";
  8. }

In the "if" condition you are already checking wheteher user == null or blank user.equals("") so why do you need to check again in the else if (user != null) , the code in that else if block could been easily put in the else block.

Now just take a look at your query:-
sql Syntax (Toggle Plain Text)
  1. SELECT username
  2. FROM msUser
  3. WHERE username LIKE '%"+user+"%'

This would list all the users to you which contain the current user's username.
For example if the current user wants to register the user name as "dracula" and if a user called "dracula1" already exists then the above select query would give you the details of user "dracula1" and hence your system would not allow the user "dracula" to be created even if no such user exists.
Your query should have been:-
sql Syntax (Toggle Plain Text)
  1. SELECT password
  2. FROM msUser
  3. WHERE user='"+user+"';

Now we may have corrected the query, but you have not fired the query in the database to check if the user exists !!!
Also why are you checking if user == query , there is everything wrong in that also.
First thing is that when you want to compare two strings you do not use the == operator, you use the equals() or equalsIgnoreCase() method of the String class.
Also if your consider this logically it is absolutely incorrect. What is the use of comparing the

According to me you still do not have any firm foundation of the language so I suggest you first get your basics of Java strong, by going through tutorials mentioned in this thread.
Next go through the JDBC tutorial, note in core Java only and once you have established yourself firmly in Java only then jump to JSP (else you will just find yourself coming back time and again to clear your core java concepts).

Also you need to read a little background information about JSPs cause you seem to be confused as to what is meant by JSP 2.0 standard etc. Any book on JSP worth its salt will cover these basic topics in the first or second chapter itself.

BTW its Manchester United and not Manchaster, if your are going to use the name of my favourite, you better spell it correctly !!!
Last edited by stephen84s; Dec 3rd, 2008 at 2:46 am.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: jsp with ajax
Next Thread in JSP Forum Timeline: Jsp tag





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC