Solved

simple compilation error in servlet

Posted on 2002-03-30
15
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
A solution for Fortify Path Manipulation.
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses

632 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