java 无法为 JSP 编译类:无法解析 nme

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3433195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:49:48  来源:igfitidea点击:

Unable to compile class for JSP: nme cannot be resolved

javajsp

提问by sohail

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

我正在尝试使用 TOMCAT 6.0 将 JSP 与 MYSQL Server 5.1.49 连接起来。我能够将 JAVA 与 MYSQL 连接,但无法使用 tomcate 配置 mysql 并出现以下错误

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

更新:这是我的代码: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.

感谢您进一步解释以解决此问题。

回答by BalusC

Can anyone kindly explain what I am doing wrong

谁能解释一下我做错了什么

Writing raw Java code in a JSP file. You should avoidthat.

在 JSP 文件中编写原始 Java 代码。你应该避免这种情况。



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

回到你的实际问题:这只是一个编译错误。nme无法解析具有该名称的变量。它尚未在您尝试访问变量的范围内声明。

To fix this problem, you need to ensure that the variable with the name nmeis 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:

要解决此问题,您需要确保nme在正确的范围内声明具有名称的变量。由于您没有发布相关的代码片段,因此无法告诉您具体如何解决此问题。所以这是一个基本的例子,什么会导致这种问题:

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

它至少与 JSP、MySQL 或 Tomcat 无关,而仅与基本的 Java 相关。在普通的 Java 类中这样做时,您会遇到完全相同的问题;)



Update: as per your update: you're declaring nmeinside the whileblock, but you're trying to access it outside the whileblock.

更新:根据您的更新:您在块nme内声明while,但您试图在while块外访问它。

There are however another major problems with the code. First of all, you're not taking benefit of the powers of the WHEREclause, 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 finallyblock.

然而,代码还有另一个主要问题。首先,您没有利用WHERE子句的功能,而是通过将整个数据库表拖入 Java 内存并测试循环中的每一行来接管 Java 内部的 DB 任务。学习 WHERE 子句。此外,代码还泄漏数据库资源。您需要将close()它们全部放在finally块中。

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 beforeyou've learnt Basic Java!

注意:你学过Basic Java之前不要学它!