Link to home
Start Free TrialLog in
Avatar of ozzyfanta
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
<html>
<head>
<title>check user</title>
</head>
<body bgcolor="#999966">
<h2 align="">Login</h2> <hr>
<form action="loginAction.jsp" method="POST">
<p>Enter USername:&nbsp;&nbsp;&nbsp;
<input type="text" name="username" size="20"></p>
<p>Enter password:&nbsp;&nbsp;&nbsp; <input type="password" name="password" size="20"></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" value="Submit"></p>
</form>
</body>
</html>

Open in new window


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>

Open in new window

Avatar of ozzyfanta
ozzyfanta

ASKER

REgistration.html
<html>
<body bgcolor="#999966">

<form action="updateDB.jsp" method="POST">


<table cellpadding=4 cellspacing=2 border=0>
<th bgcolor="#999966" colspan=2>
<font size=5>REGISTRATION</font>
<br>
</th>

<tr bgcolor="#999966">
<td valign=top><b>Username</b>
<br><input type="text" name="Username" value="" size=15 maxlength=20>
</td>

<td valign=top><b>Password</b>
<br><input type="text" name="Password" value="" size=15 maxlength=20>
</td>
</tr>

<tr bgcolor="#999966">
<td valign=top><b>FirstName</b>
<br><input type="text" name="FirstName" value="" size=15 maxlength=20>
<br></td>

<td valign=top><b>Surname</b> 
<br><input type="text" name="Surname" value="" size=15 maxlength=20>
<br></td>
</tr>

<tr bgcolor="#999966">
<td valign=top><b>Address Line 1</b>
<br><input type="text" name="AddressLine1" size=15 value="" maxlength=20>
<br></td>

<td valign=top><b>Address Line 2</b>
<br><input type="text" name="AddressLine2" size=15 value=""maxlength=20>
<br></td>
</tr>

<tr bgcolor="#999966">
<td valign=top><b>City</b>
<br><input type="text" name="City" size=15 value="" maxlength=20>
<br></td>

<td valign=top><b>Telephone</b>
<br><input type="text" name="Telephone" size=15 value="" maxlength=20></td>
<br></td>
</tr>

<tr bgcolor="#999966">
<td valign=top><b>Mobile</b>
<br><input type="text" name="Mobile" size=15 value="" maxlength=20>
<br></td>
</tr>

<tr bgcolor="#663300">
<td align=center colspan=2><hr>
<input type="submit" value="Submit"></td></tr></table></center>
</form>
</body>
</html>

Open in new window


updateDB.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" %>



<sql:setDataSource
      var          = "libraryDB"
      scope        = "session" 
      driver       = "sun.jdbc.odbc.JdbcOdbcDriver" 
      url          = "jdbc:odbc:dB"
 />

<sql:update  var="usersList"  scope="request"  dataSource = "${dB}">
	INSERT INTO  Users
	(Username,Password,FirstName,Surname,AddressLine1,AddressLine2,City,Telephone,Mobile)
	VALUES(?,?,?,?,?,?,?,?,?)

	<sql:param value="%${param.Username}%" />
	<sql:param value="%${param.Password}%" />
	<sql:param value="%${param.FirstName}%" />
	<sql:param value="%${param.Surname}%" />
	<sql:param value="%${param.AddressLine1}%" />
	<sql:param value="%${param.AddressLine2}%" />
	<sql:param value="%${param.City}%" />
	<sql:param value="%${param.Telephone}%" />
	<sql:param value="%${param.Mobile}%" />
</sql:update>
<jsp:forward page="registeredUser.jsp" />

Open in new window

Avatar of Sathish David  Kumar N
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...
>>>>>>>>   url          = "jdbc:odbc:dB"  

url is wrong ....  u must tell port number and IP address  of the DB
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
Avatar of Sathish David  Kumar N
Sathish David Kumar N
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am learning JSP...I just wanna use JSp...thanks
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
<%@ 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>

Open in new window


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();
    }

%>

Open in new window


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>

Open in new window


thanks guys...just learning lol
Thanks