Solved

simple compilation error in servlet

Posted on 2002-03-30
15
171 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 3

Expert Comment

by:msterjev
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumHeights2  challenge 7 75
word0 challenge 4 52
Systems talking to each other 5 107
Securing Jmx Console and web console 2 45
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.

744 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

9 Experts available now in Live!

Get 1:1 Help Now