Solved

simple compilation error in servlet

Posted on 2002-03-30
15
174 Views
Last Modified: 2013-11-24
i have this code to connect to access to validate username
and password.
and it gives the error.

location: interface javax.servlet.http.HttpServletResponse
              username = res.getParameter("username");
                            ^
CheckLogin.java:24: cannot resolve symbol
symbol  : method getParameter  (java.lang.String)
location: interface javax.servlet.http.HttpServletResponse
              password = res.getParameter("password");
                            ^
the pointer pointing to res.getParameter .

===============================================

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

public class CheckLogin extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse res)
                                       throws ServletException, IOException {
   
         Connection con = null;          

         // check userID and password
         try {
         
              String strSQL, username, password;
              ResultSet rs;
              Statement stmt;
             
              // get parameters passed from html
              username = res.getParameter("username");
              password = res.getParameter("password");              

              Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
              con = DriverManager.getConnection("jdbc:odbc:db1");
              stmt = con.createStatement();
              strSQL = "select * from LoginTable " +
                    "where username = '" + username + "' and " +
                    "password = '" + password + "'";
              rs = stmt.executeQuery(strSQL);
             
              if (rs.next()) { // valid username and pass
                   res.sendRedirect("http://www.alwatan.com");
              } else {
                   res.sendRedirect("http://www.omania.net");
              }
             
              rs.close();

         } catch (SQLException e) {
              e.printStackTrace(System.err);
         } finally {
              if (con != null) {
                   try {
                        con.close();
                   } catch (SQLException se) {
                        se.printStackTrace(System.err);
                   }
              }
         }                        
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res)
                                       throws ServletException, IOException {
         doGet(req,res);
         
         return;
    }
}
===========================================
0
Comment
Question by:star4
  • 9
  • 6
15 Comments
 
LVL 3

Accepted Solution

by:
msterjev earned 5 total points
ID: 6907311
Not res.getParameter(),
use

req.getParameter(), this method is member of the class HttpServletRequest.

So just change s with q.

Enjoy.
0
 

Author Comment

by:star4
ID: 6907476
it works msterjev but i gives those two errors

unreported exception java.lang.ClassNotFoundException;
must be caught or declared to be thrown
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
     ^

unreported exception java.lang.InstantiationException;
must be caught or declared to be thrown
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
     ^
0
 

Author Comment

by:star4
ID: 6907485
it works msterjev but i gives those two errors

unreported exception java.lang.ClassNotFoundException;
must be caught or declared to be thrown
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
     ^

unreported exception java.lang.InstantiationException;
must be caught or declared to be thrown
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
     ^
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6907501
that newInstance() is not needed.

Just:

try
{
     Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
     //You will never rich this statement
     return;
}



Or you can declare ClassNotFoundException after ServletException,IOException:


   public void doGet(HttpServletRequest req, HttpServletResponse res)
                                      throws ServletException, IOException,ClassNotFoundException
0
 

Author Comment

by:star4
ID: 6907536
i tried what u are saying but get 5 errors .
this the code after modification:

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

public class CheckLogin extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse res)
                                       throws ServletException, IOException {
   
         Connection con = null;          

         // check userID and password
         try {
         
              String strSQL, username, password;
              ResultSet rs;
              Statement stmt;
             
              // get parameters passed from html
              username = req.getParameter("username");
              password = req.getParameter("password");              

              Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
              con = DriverManager.getConnection("jdbc:odbc:db1");
              stmt = con.createStatement();
              strSQL = "select * from LoginTable " +
                    "where username = '" + username + "' and " +
                    "password = '" + password + "'";
              rs = stmt.executeQuery(strSQL);
             
              if (rs.next()) { // valid username and pass
                   res.sendRedirect("http://www.alwatan.com");
              } else {
                   res.sendRedirect("http://www.omania.net");
              }
             
              rs.close();

         } catch (ClassNotFoundException e) {
              return;
         } finally {
              if (con != null) {
                   try {
                        con.close();
                   } catch (SQLException se) {
                        se.printStackTrace(System.err);
                   }
              }
         }                        
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res)
                                       throws ServletException, IOException {
         doGet(req,res);
         
         return;
    }
}


=========================


any suggestions
0
 

Author Comment

by:star4
ID: 6907550
i tried what u are saying but get 5 errors .
this the code after modification:

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

public class CheckLogin extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse res)
                                       throws ServletException, IOException {
   
         Connection con = null;          

         // check userID and password
         try {
         
              String strSQL, username, password;
              ResultSet rs;
              Statement stmt;
             
              // get parameters passed from html
              username = req.getParameter("username");
              password = req.getParameter("password");              

              Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
              con = DriverManager.getConnection("jdbc:odbc:db1");
              stmt = con.createStatement();
              strSQL = "select * from LoginTable " +
                    "where username = '" + username + "' and " +
                    "password = '" + password + "'";
              rs = stmt.executeQuery(strSQL);
             
              if (rs.next()) { // valid username and pass
                   res.sendRedirect("http://www.alwatan.com");
              } else {
                   res.sendRedirect("http://www.omania.net");
              }
             
              rs.close();

         } catch (ClassNotFoundException e) {
              return;
         } finally {
              if (con != null) {
                   try {
                        con.close();
                   } catch (SQLException se) {
                        se.printStackTrace(System.err);
                   }
              }
         }                        
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res)
                                       throws ServletException, IOException {
         doGet(req,res);
         
         return;
    }
}


=========================


any suggestions
0
 

Author Comment

by:star4
ID: 6907561
msterjev  if this code works and i can check password
correctly ,and employees can display their data from access then i well give many points.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 3

Expert Comment

by:msterjev
ID: 6907671
The above has invalid Exception handling. I suggest you ClassNotFoundException, but not as aggregate exception handling mechanisam. In order to keep this code as it is, we should only change ClassNotFoundException with the simple Exception. Another thing. Never use get method to send username and password.So I put all the code into the doPost method. Make the appropriate changes in the HTML form posting this credentals. (METHOD=POST).

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

public class CheckLogin extends HttpServlet {
   public void doPost(HttpServletRequest req, HttpServletResponse res)
                                      throws ServletException, IOException {
   
        Connection con = null;          

        // check userID and password
        try {
       
             String strSQL, username, password;
             ResultSet rs;
             Statement stmt;
             
             // get parameters passed from html
             username = req.getParameter("username");
             password = req.getParameter("password");              

             Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
             con = DriverManager.getConnection("jdbc:odbc:db1");
             stmt = con.createStatement();
             strSQL = "select * from LoginTable " +
                   "where username = '" + username + "' and " +
                   "password = '" + password + "'";
             rs = stmt.executeQuery(strSQL);
             
             if (rs.next()) { // valid username and pass
                  res.sendRedirect("http://www.alwatan.com");
             } else {
                  res.sendRedirect("http://www.omania.net");
             }
             
             rs.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
          finally {
             if (con != null) {
                  try {
                       con.close();
                  } catch (SQLException se) {
                       se.printStackTrace(System.err);
                  }
             }
        }                        
   }

 }
0
 

Author Comment

by:star4
ID: 6908376
I TRIED IT AND EVERY THING GOES OK. BUT IT DIDN'T DIRECT
ME TO WEB PAGE,IT DIRECT ME TO http://localhost/servlet/CheckLogin
showing a white window nothing inside it !!!

whats wrong with it ..
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6908509
Try this:

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

public class CheckLogin extends HttpServlet {
   public void doPost(HttpServletRequest req, HttpServletResponse res)
                                      throws ServletException, IOException {
   
        Connection con = null;          
          boolean logged=false;
          res.setContentType("text/html");
          PrintWriter out=res.getWriter();
        // check userID and password
        try {
       
             String strSQL, username, password;
             ResultSet rs;
             Statement stmt;
             
             // get parameters passed from html
             username = req.getParameter("username");
             password = req.getParameter("password");              

             Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
             con = DriverManager.getConnection("jdbc:odbc:db1");
             stmt = con.createStatement();
             strSQL = "select * from LoginTable " +
                   "where username = '" + username + "' and " +
                   "password = '" + password + "'";
             rs = stmt.executeQuery(strSQL);
             
             if (rs.next())
                     logged=true;
                else
                 logged=false;
              rs.close();
                stmt.close();

        } catch (Exception e)
          {
            e.printStackTrace();
        }
          finally
          {
             if (con != null)
                {
                  try
                      {
                       con.close();
                  } catch (SQLException se) {
                       se.printStackTrace(System.err);
                  }
             }
        }
          if(logged)
               res.sendRedirect("http://www.alwatan.com");
          else
               res.sendRedirect("http://www.omania.net");
   }    

 }
0
 

Author Comment

by:star4
ID: 6908552
i tried it and it work well , but it directs me always to the same web
("http://www.omania.net");
 
it seems it doen;s check the password correctly!!

note : you will get the points even if u didn't answer this question.

but please solve if u can
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6908574
I can't see the reason. There is something in the login process.
Try debugging this way:
Add the following after retrieving username and password.

System.out.println(username);
System.out.println(password);

If there is null on the console output something is wrong with the parameters. Check the html form to see if the input fileds have the same name as your parameters. Try with adding some System.out.println...... in you code to locate the error! Programatically is OK. Check yours database.
Also type System.out.println(strSQL) to see if the string is correct.
0
 

Author Comment

by:star4
ID: 6908922
i write System.out.println(username);
        System.out.println(password);
       System.out.println("CHECK");
inside the CheckLogin but it seems to does not go there.

the username and password in html are ok.
here is the html :

<HTML>
<TITLE>Login</TITLE>
<BODY>
<FORM METHOD=POST ACTION="http://student/servlet/CheckLogin2">
<CENTER>
<TABLE BORDER=0>
<TR><TD COLSPAN=2>
<P ALIGN=CENTER>
<b><font size="7">
Welcome!</font></b><br>
Please enter your&nbsp;<br>
Password, and ID NO. to log in.
</TD></TR>

<TR><TD>
<p ALIGN="RIGHT">
</TD>
<TD>
<P>&nbsp;
<P>&nbsp;
</TD></TR>

</center>

<TR><TD>
<P ALIGN=left><b>Emp ID :</b>
</TD>
<CENTER>
<TD>
<P><INPUT TYPE= "text" NAME="username" VALUE="" SIZE=15>
</TD></TR>                                                            

</center>                                                            

<TR><TD>
<p ALIGN="left"><b>PASSWORD</b>
</TD>
<CENTER>
<TD>
<P><INPUT TYPE=PASSWORD NAME="password" VALUE="" SIZE=15>
</TD></TR>                                                            
                                                                     
<TR><TD COLSPAN=2>                                                    
<CENTER>                                                              
<INPUT TYPE=SUBMIT VALUE="  OK   ">                                   
</CENTER>                                                            
</TD></TR>
</TABLE>
</FORM>                                                              
</BODY></HTML>

===============================
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6909060
I check this code and it works fine on tomcat:

HTML form:

<html>
<body>
<form METHOD=POST Action="servlet/LoginServlet">
USERNAME:<INPUT TYPE=TEXT Name="username"><BR>
PASSWORD<INPUT TYPE=PASSWORD NAME="password"><BR>
<INPUT TYPE=SUBMIT VALUE="LOGON">
</form>
</body>
</html>


LoginServlet.java

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


public class LoginServlet extends HttpServlet
{
     
     
     public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
     {
          Connection con=null;
          Statement stmt=null;
          ResultSet result=null;
          String username,password,stmtSQL;
          boolean logged=false;
          PrintWriter out=res.getWriter();
          res.setContentType("text/html");
          username=req.getParameter("username");
          password=req.getParameter("password");
          try
          {
               Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
               con=DriverManager.getConnection("jdbc:odbc:Imenik");
               stmt=con.createStatement();
               stmtSQL="Select * From Users Where username='"+username+"' and password='"+password+"'";
               result=stmt.executeQuery(stmtSQL);
               if(result.next())
                    logged=true;
               else
                    logged=false;
               stmt.close();
               con.close();
               con=null;
          }
          catch(Exception e)
          {
               e.printStackTrace();
          }
          finally
          {
               if(con!=null)
               {
                    try
                    {
                         con.close();
                    }
                    catch(Exception ignored){}
               }
          }
          if(logged)
               res.sendRedirect("http://localhost:8080/examples");
          else
               res.sendRedirect("http://localhost:8080/Log/index.html");
         
     }
}




All of this for 20 points ???????????
0
 

Author Comment

by:star4
ID: 6909110
i didn't tried what u last said, but i work on my example
until every thing is ok .

so i well give a nother 5 points, and i hope see u in other questions ,
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PDFBox - convert image to PDF, PDF resolution 3 118
nextBoolean(double p) for Random class 3 35
Windows 10 and JDK 7 7u85 4 33
java jdbc batch example 8 22
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now