userregistration.jsp
<%--
Document : userregistration
Created on : 17 May, 2013, 11:22:42 AM
Author : Aravind Sankaran
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
var k=document.getElementById("username").value;
var urls="checkusername.jsp?ver="+k;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4)
{
document.getElementById("err").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",urls,true);
xmlhttp.send();
}
</script>
</head>
<body>
User Name: <input type="text" name="username" id="username" onkeyup="loadXMLDoc()">
<span id="err"> </span>
</body>
</html>
checkusername.jsp
<%--
Document : checkusername
Created on : 9 May, 2013, 12:42:25 PM
Author : Aravind Sankaran
--%>
<%@ page import="java.io.*,java.sql.*" %>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%
String sn=request.getParameter("ver");
Class.forName("com.mysql.jdbc.Driver");
Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","databaseusername","databasepassword");
Statement st=con.createStatement();
ResultSet rs = st.executeQuery("select * from table_name where username='"+sn+"'"); // this is for name
if(rs.next())
{
out.println("<font color=red>");
out.println("Name already taken");
out.println("</font>");
}else {
out.println("<font color=green>");
out.println("Available");
out.println("</font>");
}
rs.close();
st.close();
con.close();
%>
web.xml
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xmlns:web
=
"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation
=
"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id
=
"WebApp_ID"
version
=
"2.5"
>
<
welcome-file-list
>
<
welcome-file
>
userregistration.jsp</
welcome-file
>
</
welcome-file-list
>
</
web-app
>
The code would be nice for those who want a simple solution. But security is relevant in this case!
ReplyDeleteNever use "Statement st=con.createStatement();" and then unchecked string concatenation! That is a no go.
Prepared statements are so much simpler to use and they are mostly safe.
it is very good
ReplyDeletethank you!
Thanks :)
ReplyDeleteI want to make a validate() function so that the user cannot submit the form if he inserts an existing username
ReplyDeleteform name="demo" action="RegisterServlet" method="get" onsubmit="return validate();"
How should I proceed?
thanks!
DeletePlease,if you have time take a look here
ReplyDeletehttp://pcgames4addicts.blogspot.ro/2014/04/i-am-uploading-file-on-server-and.html
I am having trouble with a validate function
GOOD ATTEMPT AND A USEFUL ONE TOO REGARDLESS OF THE FEW GLITCHES THAT HAVE BEEN POINTED OUT EARLIER. BUT FOR HEAVEN'S SAKE DON'T USE SUCH BLINDING COLOR COMBINATIONS!
ReplyDeleteTRY SIMPLER COLORS ON LIGHT BACKGROUND.