tags:

views:

158

answers:

3

I am trying to connect JSP with MYSQL Server 5.1.49 using TOMCAT 6.0. I am able to connect JAVA with MYSQL but unable to configure mysql with tomcate and getting below error

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 54 in the jsp file: /checlLogin.jsp
nme cannot be resolved
51:     String pwd = request.getParameter("password");
52:     String uName = request.getParameter("username");
53: 
54:      if(nme.equals(uName))
55:       {
56:         response.sendRedirect("index.jsp");
57:       }
Stacktrace:
 org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
 org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
 org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:349)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:327)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
 org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

Can anyone kindly explain what I am doing wrong.


Update: Here is my Code: CheckLogin.jsp

<%@ page language="java" import="java.sql.*" errorPage=""%>
<%@ page import="java.util.*"%>


<%
   Connection con = null;
   try {
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "sohail");
   if(!con.isClosed())
   System.out.println("Successfully connected to MySQL server...");

//Step 5: create Statement
Statement st = con.createStatement();

//Step 6: preapare & execute the query
String sql = "SELECT * FROM login";
ResultSet rs = st.executeQuery(sql);

//Step 7: process the results
while(rs.next())
{
String nme = rs.getString("loginID");
String pwd = rs.getString("password");
System.out.println(nme + " " + pwd);
}
//Step 8: close the connection

con.close();

} 
catch(Exception e) 
{
System.err.println("Exception: " + e.getMessage());
} 
finally 
{

try 
{

if(con != null)

con.close();

} 
catch(SQLException e) 
{
} 
}

    String pwd = request.getParameter("password");
    String uName = request.getParameter("username");

     if(nme.equals(uName))
      {
        response.sendRedirect("index.jsp");
      }
      else{
     response.sendRedirect("index.jsp");
      }
%>

Thanks for your further explaination to fix this issue.

+1  A: 

Can anyone kindly explain what I am doing wrong

Writing raw Java code in a JSP file. You should avoid that.


Back to your actual problem: this is just a compilation error. The variable with the name nme cannot be resolved. It has not been declared within the scope where you're trying to access the variable.

To fix this problem, you need to ensure that the variable with the name nme is been declared within the right scope. It's impossible to tell you how exactly this can be fixed since you didn't post the code snippet of relevance. So here's a basic example what can cause this kind of problem:

if (foo == null) {
    String bar = "value";
}

if ("foo".equals(bar)) { // Compile error! bar cannot be resolved.
    // ...
}

You should then fix it like as follows:

String bar = null;

if (foo == null) {
    bar = "value";
}

if ("foo".equals(bar)) { // No compile error anymore, bar can be resolved.
    // ...
}

It's at least not related to JSP, MySQL or Tomcat, but just to basic Java. You would have exactly the same problem when doing so in a plain vanilla Java class ;)


Update: as per your update: you're declaring nme inside the while block, but you're trying to access it outside the while block.

There are however another major problems with the code. First of all, you're not taking benefit of the powers of the WHERE clause, but taking over the DB's task inside Java by and hauling the entire database table into Java's memory and testing every row inside a loop. Learn the WHERE clause. Further the code is also leaking DB resources. You need to close() them all in the finally block.

I suggest to throw away the books/tutorials you're reading as far. They have as far only teached bad practces. Here are some links to better tutorials:

Note: don't learn it before you've learnt Basic Java!

BalusC
A: 

(update moved into quesiton, you should delete this "answer")

Sohail
A: 

Thanks you Balus C for your explaination. I shall definately go through the toturial links. I admit i need to first go through JSP in details...

However, can you explain little more about how to configure TOMCAT environment with MYSQL?

1- Where to place mysql-connector-java-5.1.13-bin in TOMCAR directory? Should i place it under Tomcat 6.0\webapps\myapp\WEB-INF\lib?

2-Do i need to confirgure CONTEXT.XML or SERVER.XML files?

3- Should i write WEB.XML file and need to place under Tomcat 6.0\webapps\myapp\WEB-INF? if Yes, then what should be the contents of file....

Your explaination will really help to start...

Thanks you

Sohail
@Sohail - welcome to Stack Overflow. Stack Overflow is not a forum - it is a Question and Answer site. This is not an answer to the question - this is a new question and should be posted as such, perhaps linking to the old question. _I also recommend registering, as it will let the site remember who your are without relying on cookies. That way, you can edit your questions with more information._ http://stackoverflow.com/faq
McDowell