hi guys >> i need ur help please >>> i ve created a login page which includes user name and password and i created a welcome page which contains the required codes for connection and verifying the username and password from oracle database .. when i run the login page and entering the username and password there is no outputs (seems no connection ).

here are the codes for login page

<%@ page contentType="text/html;charset=windows-1256"%>

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
</head>
<body>


<br><br><br>

<table align="center" width="400px" border=0>

</table>

<table align="center"><tr><td colspan=2><h1>User Login</h2></h1></td></tr></table>


<form name="loginform" method="post" action="welcome.jsp" >
<table align="center" width="300px" style="border:1px solid #000000;" >
  <tr><td colspan=2>&nbsp;</td></tr>
  <tr>
    <td><b>Login Name</b></td>
    <td><input type="text" name="userName" value=""></td>
  </tr>
  <tr>
    <td><b>Password</b></td>
    <td><input type="password" name="password" value=""></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="Submit" value="Submit"></td>
  </tr>
  <tr><td colspan=2>&nbsp;</td></tr>
</table>



</form>
    
  </body>
</html>

here the codes for the welcome page

<%@ page contentType="text/html;charset=windows-1256"%>
<%@ page language="java" import="java.sql.*"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1256"></meta>
    <title>untitled</title>
  </head>
  <body>

  </body>
  <%  
String user=request.getParameter("userName");
String pass=request.getParameter("password");

     try{
    
     java.lang.Class.forName("oracle.jdbc.driver.OracleDriver");
      Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "sourcepad", "s");
     Statement st=conn.createStatement();
     ResultSet rs=st.executeQuery("select username,password from user_tab");
     while(rs.next()){
         
         String username=rs.getString(1);
         String password=rs.getString(2);
   if (user.equals("username") && pass.equals("password")) {
                out.println("<a href=\"welcome.jsp\"> </a>");
              }  
            
          
         else
         out.println("Login Failed,Please try Againe");

     
}}
catch(Exception e1)
{}
%>
</html>

This is the html code you are displaying at the page:

<a href=\"welcome.jsp\"> </a>

But there is nothing between the <a> </a> , so the link is there and you can click it. The problem is that you don't display anything to show the user where the link is:

<a href=\"welcome.jsp\">Click me</a>

And finally. DON'T DO THAT. Remove the code you have in the welcome.jsp. Put all the database code in a separate class inside a method. Call that method from the jsp. And I would suggest if the login is successful not to show a link that goes to the same page.

From your code you send the username, password to the welcome page, and if the login is successful you again show a link to go to the welcome.jsp. Shouldn't go to the main page?

thanks for your reply ..
i did the changes u suggested .. but still no effects.
i am trying to put all the db codes in separate class but i failed could u help me .. i am using jdeveloper 11

thanks again

Post your latest code

thanks again javaAddict i did solve it myself here is the right code

<%@ page contentType="text/html;charset=windows-1256"%>
<%@ page language="java" import="java.sql.*"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1256"></meta>
    <title>untitled</title>
  </head>
  <body>

  </body>
  <%  
String user=request.getParameter("userName");
String pass=request.getParameter("password");

     try{
    
    Class.forName("oracle.jdbc.driver.OracleDriver");    
    Connection conn =DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1521:orcl", "sourcepad", "s");
     Statement st=conn.createStatement();
     ResultSet rs=st.executeQuery("select username,password from user_tab where username='"+user+"' and password='"+pass+"'");
//out.print(rs.next());
 while(rs.next())
         {
         String username=rs.getString(1);
         String password=rs.getString(2);
             if(user.equals(username) && pass.equals(password))
             {
             %>
             <jsp:forward page="/indexenglish.jsp" />
         <%}
         else {
          %>
             <jsp:forward page="/fail.jsp" />
         <% }
}}
catch(Exception e1)
{}
%>
</html>

thanks again javaAddict i did solve it myself here is the right code

What was the problem?

I would suggest not to use that kind of code. Didn't this work:

public class UserMgt {

  public static boolean login(String user, String pass) throws Exception  {

Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");    
    conn =DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1521:orcl", "sourcepad", "s");
     st=conn.createStatement();
     rs=st.executeQuery("select username,password from user_tab where username='"+user+"' and password='"+pass+"'");

        if(rs.next())
         {
         String username=rs.getString(1);
         String password=rs.getString(2);
         return user.equals(username) && pass.equals(password);
          }
          return false;
// the finally will execute despite the return and the connection will be closed and it MUST be closed.
} catch (Exception e) {
    e.printStackTrace(); // you print the error for debugging
    throw e; // you re-throw the exception to "alert" the caller of this method that something wrong has happened
// the finally will execute despite the throw and the connection will be closed and it MUST be closed.
} finally {
            if (conn!=null) conn.close;
            if (rs!=null) rs.close;
            if (st!=null) st.close;
}

}

}
<%@ page contentType="text/html;charset=windows-1256"%>
<%@ page language="java" import="java.sql.*"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1256"></meta>
    <title>untitled</title>
  </head>
  <body>

  </body>
  <%  
String user=request.getParameter("userName");
String pass=request.getParameter("password");

     try{
    
    boolean login = UserMgt.login(user, pass);
 
             if(login)
             {
             %>
             <jsp:forward page="/indexenglish.jsp" />
         <%}
         else {
          %>
             <jsp:forward page="/fail.jsp" />
         <% }
}
catch(Exception e1)
{
%>
<jsp:forward page="/errorPage.jsp" />
<%
}
%>
</html>

Better take a look at the tutorial at the top of the jsp forum page with title database connectivity (MVC)

thanks again. the problem was in the sql query not in the connection rs=st.executeQuery("select username,password from user_tab where username='"+user+"' and password='"+pass+"'")


thnx in advance

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.