• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

simple compilation error in servlet

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
star4
Asked:
star4
  • 9
  • 6
1 Solution
 
msterjevCommented:
Not res.getParameter(),
use

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

So just change s with q.

Enjoy.
0
 
star4Author Commented:
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
 
star4Author Commented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
msterjevCommented:
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
 
star4Author Commented:
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
 
star4Author Commented:
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
 
star4Author Commented:
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
 
msterjevCommented:
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
 
star4Author Commented:
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
 
msterjevCommented:
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
 
star4Author Commented:
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
 
msterjevCommented:
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
 
star4Author Commented:
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
 
msterjevCommented:
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
 
star4Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now