[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Login and Registration pages

Posted on 2011-04-25
9
Medium Priority
?
654 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:ozzyfanta
  • 5
  • 4
9 Comments
 

Author Comment

by:ozzyfanta
ID: 35463821
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

0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35464616
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...
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35464622
>>>>>>>>   url          = "jdbc:odbc:dB"  

url is wrong ....  u must tell port number and IP address  of the DB
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ozzyfanta
ID: 35465792
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...
0
 
LVL 20

Accepted Solution

by:
Sathish David  Kumar N earned 1500 total points
ID: 35466385
on that purpose you can use java script  or ajax ...
for checking the user name

input type="text" name="username" onblur="chekUser()">

and wirte java script function and connect to ur DB and check the User exiting or not ....


java script normally it will refresh page ...
Ajax wont refresh the page ...
0
 

Author Comment

by:ozzyfanta
ID: 35466876
I am learning JSP...I just wanna use JSp...thanks
0
 

Author Comment

by:ozzyfanta
ID: 35477606
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
0
 

Author Closing Comment

by:ozzyfanta
ID: 35489091
Thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
Most folks would know the basics of how Dropbox works, so that’s not the purpose of this article. Security is what it’s all about, so here I’ll share how I choose to secure my Dropbox Account and the Data it contains.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question