ozzyfanta
asked on
Login and Registration pages
I am a self learner, trying to get more knowledge on JSP and DATABASE...I have written pages for login and registration. Obviously, as i am a beginner,I still have few issues with syntax ,etc. Could you pls have time to check my mistakes and if possible advice me on the best available resources online about login and registration using JSP. I will post my codes for login and registration, but chose to ignore the error i am getting for later stage...
LOGIN PAGE
==========
loginn.html
loginAction.jsp
LOGIN PAGE
==========
loginn.html
<html>
<head>
<title>check user</title>
</head>
<body bgcolor="#999966">
<h2 align="">Login</h2> <hr>
<form action="loginAction.jsp" method="POST">
<p>Enter USername:
<input type="text" name="username" size="20"></p>
<p>Enter password: <input type="password" name="password" size="20"></p>
<p>
<input type="submit" value="Submit"></p>
</form>
</body>
</html>
loginAction.jsp
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" %>
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
<%! String struser;String strpass;String msg;%>
<html>
<body>
<%
struser=request.getParameter("username");
strpass=request.getParameter("password");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dB");
Statement st=con.createStatement();
String sql;
sql="select*from Users where Username='"+struser+"'";
sql+=" and Password='"+strpass+"'";
ResultSet rs=st.executeQuery(sql);
String str = null;
while (rs.next()) {
str = rs.getString("Username");
}
// if string is not null then login was valid
if (str != null) { %>
Login Successful!<br>
<jsp:forward page=“welcome.jsp"/
<%
} else {
%>
Invalid Login try again:<br>
<jsp:forward page=“loginn.html"/
<% } %>
</body></html>
Use Html table
like
<table>
<tr>
<td>
Enter USername <input type="text" name="username" size="20"></td> </tr>
<tr>
<td>
Enter password: <input type="password" name="password" size="20></td> </tr>
</table>
dont use name speace...
like
<table>
<tr>
<td>
Enter USername <input type="text" name="username" size="20"></td> </tr>
<tr>
<td>
Enter password: <input type="password" name="password" size="20></td> </tr>
</table>
dont use name speace...
>>>>>>>> url = "jdbc:odbc:dB"
url is wrong .... u must tell port number and IP address of the DB
url is wrong .... u must tell port number and IP address of the DB
ASKER
Thanks a lot...i am still trying to read about it roseindia.net...could you pls help as well with a good resource concerning registration...i want the user to input their details, in particular the username must be unique...it means before accepting their registration, the details enter must be validated, if the username is already in the database, a message asking the user to choose another username must appear...the article is that website is excellent...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am learning JSP...I just wanna use JSp...thanks
ASKER
I have done a lot of reserach...here i am again with a login page in jsp which ,with my small underestanding require a very small change in the connection side of things. I am using MS access...Many resources about connection are not written about MS access...Please try to check my doLogin.jsp
login.jsp
doLogin.jsp
success.jsp
thanks guys...just learning lol
login.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%
String error=request.getParameter("error");
if(error==null || error=="null"){
error="";
}
%>
<html>
<head>
<title>User Login JSP</title>
<script>
function trim(s)
{
return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
}
function validate()
{
if(trim(document.frmLogin.sUserName.value)=="")
{
alert("Login empty");
document.frmLogin.sUserName.focus();
return false;
}
else if(trim(document.frmLogin.sPwd.value)=="")
{
alert("password empty");
document.frmLogin.sPwd.focus();
return false;
}
}
</script>
</head>
<body>
<div><%=error%></div>
<form name="frmLogin" onSubmit="return validate();" action="doLogin.jsp" method="post">
User Name <input type="text" name="sUserName" /><br />
Password <input type="password" name="sPwd" /><br />
<input type="submit" name="sSubmit" value="Submit" />
</form>
</body>
</html>
doLogin.jsp
<%@ page language="java" import="java.sql.*" errorPage="" %>
<%
Connection conn = null;
Class.forName("(" sun.jdbc.odbc.jdbcodbcDriver");
conn = DriverManager.getConnection(“jdbc.odbc:DNS”);
ResultSet rsdoLogin = null;
PreparedStatement psdoLogin=null;
String Username=request.getParameter("sUserName");
String Password=request.getParameter("sPwd");
String message="User login successfully ";
try{
String sqlOption="SELECT * FROM Users where"
+" Username=? and Password=password(?) and sStatus='A'";
psdoLogin=conn.prepareStatement(sqlOption);
psdoLogin.setString(1,Username);
psdoLogin.setString(2,Password);
rsdoLogin=psdoLogin.executeQuery();
if(rsdoLogin.next())
{
String sUserName=rsdoLogin.getString("FirstName")+" "+rsdoLogin.getString("Surname");
session.setAttribute("Username",rsdoLogin.getString("Username"));
session.setAttribute("iUserType",rsdoLogin.getString("iUserType"));
session.setAttribute("iUserLevel",rsdoLogin.getString("iUserLevel"));
session.setAttribute("sUserName",sUserName);
response.sendRedirect("success.jsp?error="+message);
}
else
{
message="No user or password matched" ;
response.sendRedirect("login.jsp?error="+message);
}
}
catch(Exception e)
{
e.printStackTrace();
}
/// close object and connection
try{
if(psdoLogin!=null){
psdoLogin.close();
}
if(rsdoLogin!=null){
rsdoLogin.close();
}
if(conn!=null){
conn.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
%>
success.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java"%>
<html>
<head>
<title>Successfully Login by JSP</title>
</head>
<body>
Successfully login by JSP<br />
Session Value<br />
<%
out.print("UserName :"+session.getAttribute("sUserID")+"<br>");
out.print("First & Last Name :"+session.getAttribute("sUserName"));
%>
</body>
</html>
thanks guys...just learning lol
ASKER
Thanks
ASKER
Open in new window
updateDB.jsp
Open in new window