Login thru JSP not working?

Discussions

Web tier: servlets, JSP, Web frameworks: Login thru JSP not working?

  1. Login thru JSP not working? (5 messages)

    Hello

    I am trying to login thru JSP, Web Server is Tomcat & database is sqlserver2000. After filling user, password & pressing submit button,error message is as follows :
    cannot resolve symbol
    symbol : variable connection
    location: class org.apache.jsp.verifyuser_jsp
    Statement statement = connection.createStatement();

    This is the code :
    <BODY>

    <% String user = request.getParameter("user");%>
    <% String pwd = request.getParameter("pwd");%>

    <%
    try
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection connection = DriverManager.getConnection(
    "jdbc:odbc:DEVELOPER1","sa","");
    System.out.println("database connected");
    Statement statement = connection.createStatement();
    }

    String query1 = "SELECT Username, Password FROM login WHERE Username = '"+user+"' AND Password = '"+pwd+"'";

    ResultSet p = statement.executeQuery(query1);

    while (p.next())
    {
    String Puser = p.getString("Username");
    String Ppass = p.getString("Password");
    }

    if (user.equals("Puser") && pwd.equals("Ppass"))

     %>
    <jsp:forward page = "welcome.jsp"/>

    <% else { %>

    <h3>Invalid username or password</h3>

    <% }

    connection.close();
    catch (Exception e)
    {
    out.println(e.getMessage());
    %>
    <h3>error in accessing database</h3>

    <% }
    %>
    </BODY>

    what's wrong?

    Threaded Messages (5)

  2. Login thru JSP not working?[ Go to top ]

    Maybe you didn't add in the import tag at the beginning of the page:

    <%@ page import="java.sql.*"%>
  3. Login thru JSP not working?[ Go to top ]

    <%@ page import="java.sql.*"%> is given even then its not working.
  4. Login thru JSP not working?[ Go to top ]

    it looks like you do not have a catch statement after your try statement

    try the following

    try
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection connection = DriverManager.getConnection(
    "jdbc:odbc:DEVELOPER1","sa","");
    System.out.println("database connected");
    Statement statement = connection.createStatement();
    }
    catch (Exception ex) {
    System.out.println(ex);
    }
  5. Login thru JSP not working?[ Go to top ]

    Statement statement = connection.createStatement();
    }
    Remove this } and paste it after connection.close() statement.

    Actually you are closing your connection after <jsp:forward>, the lines after <jsp:forward> element will not be executed. So, if you get welcome.jsp, your connection.close() statement will never be executed. So, close your connection as soon as you are done.

    Thanks,
    Senthil.
  6. Login thru JSP not working?[ Go to top ]

    The "connection" variable you have will have visibility only inside the try{} block. Standard variable visibility problem.

    -Sanjay