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...

register.jsp

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
body {
	background-image: url();
}
-->
</style></head>

<body>
<table width="800" height="1028" border="0" align="center">
  <tr>
    <td width="795" height="134"><img src="banner2.gif" width="795" height="95" align="top" /><br />
    <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>
  </tr>
  <tr>
    <td height="788" align="left" valign="top"><form id="form1" name="form1" method="post" action="doregister.jsp">
      <p>&nbsp;</p>
      <table width="200" border="1">
        <tr>
          <td colspan="2"><div align="center">Register</div></td>
        </tr>
        <tr>
          <td width="83">Username</td>
          <td width="101"><label>
            <input type="text" name="txtuser" id="txtuser" />
          </label></td>
        </tr>
        <tr>
          <td>Password</td>
          <td><label>
            <input type="password" name="txtpass" id="txtpass" />
          </label></td>
        </tr>
        <tr>
          <td>Age</td>
          <td><label>
            <input type="text" name="txtage" id="txtage" />
          </label></td>
        </tr>
        <tr>
          <td>Club</td>
          <td><label>
            <select name="selectclub" id="selectclub">
              <option>Manchaster United</option>
              <option>Inter Milan</option>
            </select>
          </label></td>
        </tr>
        <tr>
          <td colspan="2"><label>
            <div align="center">
              <input type="submit" name="button" id="button" value="Submit" />
              </div>
          </label></td>
        </tr>
        <tr>
          <td height="62" colspan="2">  <%
			String error = request.getParameter("err");
			if(error!=null) out.println(error);
		%></td>
        </tr>
      </table>
    </form>
    <p>    </p></td>
  </tr>
  <tr>
    <td height="80" align="left" valign="top"><p>&nbsp;</p>
    <p>
    </p></td>
  </tr>
</table>
</body>
</html>


doregister.jsp

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ include file ="konek.jsp" %>

<%
	String user = request.getParameter("txtuser");
	String pass = request.getParameter("txtpass");
	String age = request.getParameter("txtage");
	String query = null;
	
	
	if(user == null || user.equals(""))
	{
		response.sendRedirect("register.jsp?err = please fill the username");
	}
		
	else if (user != null)
	{
		query = "select username from msUser where username like '%"+user+"%'";
	}
				
	else if(pass == null || pass.equals(""))
	{
		response.sendRedirect("register.jsp?err = please fill the pass");
	}
	else if(age == null || age.equals(""))
	{
		response.sendRedirect("register.jsp?err = please fill the age");
	}
	
	
	else
	{
		if(user == query)
			{
				response.sendRedirect("register.jsp?err = please choose a diffrent user name");
				}
		else if(user != query)
		{
		st.executeUpdate("insert into msUser values('"+user+"','"+pass+"',"+age+")");
		con.close();
	    response.sendRedirect("home.jsp");
		}
	}
	
%>

sorry, no need to download the attachement...here is the code..

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 :-

if(user == null || user.equals("")) 
{
  response.sendRedirect("register.jsp?err = please fill the username");
}
else if (user != null)
{
  query = "select username from msUser where username like '%"+user+"%'";
}

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:-

select username 
from msUser 
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:-

SELECT password 
FROM msUser
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 !!! :@

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.