Google Answers Logo
View Question
 
Q: My Login Servlet Can't Work!!! ( No Answer,   10 Comments )
Question  
Subject: My Login Servlet Can't Work!!!
Category: Computers > Programming
Asked by: maxflo-ga
List Price: $2.00
Posted: 10 May 2003 17:48 PDT
Expires: 09 Jun 2003 17:48 PDT
Question ID: 202174
When I try to compile my login servlet I get the error below: 
 
Note: LoginServlet.java uses or overrides a deprecated API. 
Note: Recompile with -deprecation for details. 
 
My Login servlet is as shown below: 
 
 
import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
import java.sql.*; 
import java.util.*; 
 
 
public class LoginServlet extends HttpServlet{ 
   
 public void doPost(HttpServletRequest req,HttpServletResponse res) 
                               throws ServletException,IOException { 
   
 res.setContentType("text/html"); 
 PrintWriter out =res.getWriter(); 
 
 
        Connection conn = null; 
 ServletConfig config; 
 Statement stmt=null; 
      ResultSet rs = null; 
 boolean status = false; 
        String wrongUser = "Invalid UserName "; 
        String wrongPwd = "Invalid Password "; 
  
 String userName = req.getParameter("userName"); 
 String password = req.getParameter("password"); 
 
        out.println("user"+userName); 
        out.println("pass"+password); 
                  
 
           
        HttpSession session = req.getSession(true); 
   
        userName = userName.trim(); 
        password = password.trim();          
   
        try{ 
           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
           conn=DriverManager.getConnection("jdbc:odbc:db1"); 
        } 
        catch(Exception  e){ 
            out.println("Could not open connection to DB"
+e.toString());
            e.printStackTrace(); 
        } 
          
 
        try { 
            stmt = conn.createStatement(); 
            rs= stmt.executeQuery("SELECT admin_password FROM
administrator WHERE admin_userName ='"+userName+"' ");
 
    /*     
            if(rs == null) 
                 res.sendRedirect("http://login page");
        
            rs.next(); 
            if(!password.equals(rs.getString(4))) 
                 res.sendRedirect("http://target page");
    */ 
 
 
            if(rs == null){// no user with this username 
                session.putValue("abc.errormsg",wrongUser); 
                res.sendRedirect("http://error page");
            } 
        
            rs.next(); 
            if(!password.equals(rs.getString(4))){ 
                 session.putValue("abc.errormsg",wrongPwd); 
                 res.sendRedirect("error page");
 
            }       
            //put values in session... 
        
            session.putValue("abc.user",userName); 
     session.putValue("abc.pass",password); 
  
           // if(password.equals(rs.getString(4))) 
           //         session.putValue("abc.userToEdit",userName);
      // status = true; 
  
          
 
            if(password.equals(rs.getString(4))) 
                 res.sendRedirect("http://target page");
           // else 
             //   res.sendRedirect("login page");
 
            } 
             
            catch(Exception  e) {  
                session.putValue("abc.errormsg",wrongUser); 
                res.sendRedirect("error page");
     
            } 
 
    }//do post ends here 
 
} 
 
 
 
Please kindly help me out - I'd like my servlet to work.

Request for Question Clarification by aditya2k-ga on 10 May 2003 20:27 PDT
Hi,

Even though the java compiler returns that message, it is more a
warning than an error. Your servlet should work despite the error.

This is not a problem, this simply means that your servlet contain
some methods, which have been superceded by different methods in newer
versions of Java.

Cheers,
Aditya.

Clarification of Question by maxflo-ga on 11 May 2003 02:49 PDT
When I try to Login the servlet won't work. So I would like to know
where the problem lies.
Answer  
There is no answer at this time.

Comments  
Subject: Re: My Login Servlet Can't Work!!!
From: eadfrith-ga on 10 May 2003 18:22 PDT
 
Maxflo,

I believe the deprecation warnings are due to your use of the method
putValue on the HttpSession object. This was deprecated in version 2.2
of the servlet specification. Use 

session.setAttribute(("abc.errormsg",userName)

instead.

However, be aware that the use of a deprectated method will not cause 
your compilation to file - it's just a warning. So, your code should
have compiled. 

I believe this line is a bug though:

if(!password.equals(rs.getString(4)))

Since your SQL query only references a single column in the SELECT 
statement there is no value returned at index 4. Use 

if(!password.equals(rs.getString(1)))

instead.

Cheers,

Eadfrith

PS. I'm not a researcher, so this advice is free.
Subject: Re: My Login Servlet Can't Work!!!
From: maxflo-ga on 11 May 2003 02:46 PDT
 
Eadfrith,
Thanks for your advice. I've tried making those changes but it still
can't work, though now there's no error message when I try to compile
it.
Maxflo.
Subject: Re: My Login Servlet Can't Work!!!
From: eadfrith-ga on 11 May 2003 11:39 PDT
 
Maxflo,

Did you fix the rs.getString(4) to rs.getString(1)?

Here are some other things to try:

The urls in your redirects aren't valid. For example, if everything
goes OK and the username/password are valid then you issue a redirect
to:

res.sendRedirect("http://target page"); 

This isn't a valid url. The same goes for your error redirects. If you
fix these urls to point to real locations then you should at least
start to see a response from your servlet. They can either be absolute
urls, such as

res.sendRedirect("://www.google.com");

or relative to the current application root, as in 
 
res.sendRedirect("/error.html");

Are you using an Access database? Check that your database really has
a table named 'administrator' with columns named 'admin_userName' and
'admin_password'. Also, test your ODBC connection to the database (you
can do this using the database adminstrator under windows).


What servlet engine are you using? If it's Tomcat then you could use
the built-in login mechanism. It's even possible to authenticate
passwords against a database, but this is more advanced.

Cheers,

Eadfrith
Subject: Re: My Login Servlet Can't Work!!!
From: maxflo-ga on 11 May 2003 14:20 PDT
 
Eadfrith,

Thanx once again for your suggestions.
I fixed the rs.getString(4) to rs.getString(1). 
The re-directs urls in the question are dummy ones, in my project I'm
using valid ones.I'm using J-run engine & Access db. The table
"administrator" exists & has those designated field names. My
connection is alright as well. I just wonder what's not happening.

Maxflo.
Subject: Re: My Login Servlet Can't Work!!!
From: eadfrith-ga on 11 May 2003 18:55 PDT
 
Maxflo,

Try this code. I tidied up the logic a little and added log statements
so that you can see what's happening if you examine the log files.

Also, notice that we may as well get the database to validate the
username and password by including them both in the WHERE clause of
the SELECT query. If there's no record that has both these values
(count == 0) then this isn't a valid user.

Let me know if this works, or if you have any questions.

Cheers,

Eadfrith 

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;


public class LoginServlet extends HttpServlet
{
  public static final String WRONG_PASSWORD = "Invalid Password ";

  public void doPost(HttpServletRequest req,HttpServletResponse res)
      throws ServletException,IOException
  {
    Connection conn = null;
    try
    {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      conn = DriverManager.getConnection("jdbc:odbc:db1");
    }
    catch(Exception  e)
    {
      getServletContext().log("Unable to create database connection",
e);
      res.sendRedirect("error page");
    }

    if(null != conn)
    {
      String userName = req.getParameter("userName");
      String password = req.getParameter("password");

      // Check that username and password were supplied
      if(userName == null || password == null)
      {
        getServletContext().log("Missing parameter -
userName/password");
        res.sendRedirect("error page");
      }
      else
      {
        try
        {
          HttpSession session = req.getSession(true);

          Statement stmt = conn.createStatement();
          String qry = " SELECT count(*) " +
                       " FROM administrator " +
                       " WHERE admin_userName ='" + userName + "' AND
" +
                       "       admin_password ='" + password + "'";

          ResultSet rs = stmt.executeQuery(qry);

          if(rs.next() && rs.getInt(1) >= 1) 
          {
            getServletContext().log(userName " logged in");
            session.setAttribute("abc.user", userName);
            session.setAttribute("abc.pass", password);
            res.sendRedirect("http://target page");
          }
          else
          {
            getServletContext().log("Invalid username/password");    
            session.setAttribute("abc.errormsg", WRONG_PASSWORD);
            res.sendRedirect("error page");
          }
        }
        catch(SQLException  e)
        {
          getServletContext().log("Error contacting database", e);
          res.sendRedirect("error page");
        }
      }
    }
  }
}
Subject: Re: My Login Servlet Can't Work!!!
From: maxflo-ga on 12 May 2003 06:54 PDT
 
Eadfrith,

Thanks once again. I discovered one mistake I was making & corrected
it - I didn't upload "LoginServlet.class" onto the server after
compiling the servlet, "LoginServlet.java". I've done that with both
versions of the code - mine & yours - and when I try to login, I get
this error message:

"500 Internal Server Error
/servlet/LoginServlet:

javax.servlet.ServletException: LoginServlet (Unsupported major.minor
version 8237.8195)
java.lang.UnsupportedClassVersionError: LoginServlet (Unsupported
major.minor version 8237.8195)
	at java.lang.ClassLoader.defineClass0(Native Method)
	at java.lang.ClassLoader.defineClass(Unknown Source)
	at java.lang.ClassLoader.defineClass(Unknown Source)
	at allaire.jrun.servlet.JRunServletLoader.loadClass(JRunServletLoader.java:525)
	at allaire.jrun.servlet.JRunServletLoader.loadClass(JRunServletLoader.java:444)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at allaire.jrun.servlet.JRunServletLoader.loadServletInstance(JRunServletLoader.java:227)
	at allaire.jrun.servlet.JRunServletLoader.loadServletInstance(JRunServletLoader.java:190)
	at allaire.jrun.servlet.JRunServletLoader.loadServlet(JRunServletLoader.java:177)
	at allaire.jrun.servlet.JRunSE.getServletReference(JRunSE.java:1308)
	at allaire.jrun.servlet.JRunSE.getNamedDispatcher(JRunSE.java:1578)
	at allaire.jrun.servlet.Invoker.service(Invoker.java:64)
	at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1024)
	at allaire.jrun.servlet.JRunSE.runServlet(JRunSE.java:936)
	at allaire.jrun.servlet.JRunRequestDispatcher.forward(JRunRequestDispatcher.java:88)
	at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1163)
	at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1153)
	at allaire.jrun.servlet.JvmContext.dispatch(JvmContext.java:330)
	at allaire.jrun.http.WebEndpoint.run(WebEndpoint.java:107)
	at allaire.jrun.ThreadPool.run(ThreadPool.java:272)
	at allaire.jrun.WorkerThread.run(WorkerThread.java:75)"


Could you please tell me what the problem might be now?

Thanx once again for your untiring concern with my problem

Maxflo.
Subject: Re: My Login Servlet Can't Work!!!
From: eadfrith-ga on 12 May 2003 10:01 PDT
 
Hi Maxflo,

Not having the servlet class available would certainly cause problems
:-) I've made this mistake too in the past, so don't feel too bad.
Becoming a good programmer has a lot to do with remembering all the
mistakes you've made in the past!

I think your problem now is that you're compiling your servlet using a
version of the JDK that is newer than the version used by JRUN. When
Java tries to load a class into memory it checks the version of the
class to ensure that it is compatible - the bytecode format did change
from JDK 1.1.

So, what version of JRUN are you running, and do you know what version
of the JDK it uses? Also, what development environment are you using
to compile your servlet?

When you compile your Java code it's possible to specify the target
platform using, not surprisingly, the -target option. So, let's assume
you discover that JRUN is running under JDK 1.1, but you're compiling
with a newer JDK. You could then modify the compilation command (how
you do this will depend on your IDE) as follows:

javac -target 1.1 ...

You should then be able to deploy your servlet under JRUN.


Cheers,

Eadfrith
Subject: Re: My Login Servlet Can't Work!!!
From: maxflo-ga on 12 May 2003 17:42 PDT
 
Eadfrith,

I know I'm using JRun version 3.0 but I've just made enquiries
concerning the JDK version it's running on. I'm working in Windows 98
environment. Once I find out the JDK version I'll be able to use the
right javac target as you've suggested.

Thanx once again for your valuable advice - you've really brought me
along way & I'm learning a lot of stuff from you.

Maxflo.
Subject: Re: My Login Servlet Can't Work!!!
From: maxflo-ga on 13 May 2003 12:08 PDT
 
Eadfrith,
The JRun version is 3.02a 25232 & the JDK version it's running on is
1.4.1_01. But when I use this Unix Command "javac -target 1.4.1_01
LoginServlet.java" to compile my servlet, I get the following error:
"javac: invalid target release: 1.4.1_01".
My servlet seems to be quite stubborn!

Maxflo.
Subject: Re: My Login Servlet Can't Work!!!
From: eadfrith-ga on 13 May 2003 12:45 PDT
 
Maxflo,

We'll get this servlet to work if it kills us :-)

Since you were running JRUN 3.0 I'd assumed that you were probably
running an older version of the JDK too. This is why I suggested using
the -target option to compile code that would run on the older
platform. However, if you're using JDK 1.4 then this isn't your
problem at all.

Actually I think the problem you're having is that JRUN 3.0 doesn't
work with JDK 1.4. According to this posting by a macromedia support
person you need a version of the JDK between 1.1.8 and 1.3.1:

http://groups.google.com/groups?th=7d05560cb82a5863&seekm=aira3m%2464u%241%40forums.macromedia.com#link2

This seems to be backed up by the JRUN 3.0 release notes:

http://www.macromedia.com/v1/documents/jrun30sp2/relnotes.htm

Have you had success with any servlets under this setup? If so then my
theory goes out of the window, but I think this is your problem.

So, I think your options now are:

1. Install and run JRUN 3.0 under an earler JDK. I'd suggest JDK
1.2.2, just to be safe, which you can download here:

http://java.sun.com/products/jdk/1.2/download.html

2. Install a newer version of JRUN, or some other servlet container,
such
as:

Resin:
http://www.caucho.com/

or 

Tomcat:
http://www.caucho.com/

The nice thing about tomcat is that it comes with set of example
servlets that you can use to test your setup.

Cheers,

Eadfrith

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy