编译错误-Tomcat、jsp

发布于 2024-12-10 13:48:02 字数 2126 浏览 0 评论 0原文

所有,

我刚刚安装了apache tomcat并且测试安装确实显示了“成功安装”apache页面。我确实执行了 HelloWorld 示例,没有任何问题。现在,我在 \apache-tomcat-XXX\webapps\mine 下创建了自己的 Web 应用程序,其中包含以下 2 个子文件夹:

\apache-tomcat-XXX\webapps\mine\classes >
\apache-tomcat-XXX\webapps\mine\lib

我创建了一个新的类文件 HelloWorldAgain.java ,其内容如下:

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * My test servlet
 *
 * @author Liz Warner
 */

public class Hi extends HttpServlet {

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Hola</title>");
        out.println("</head>");
        out.println("<body bgcolor=\"white\">");
        out.println("<h1> Hi </h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

但是,当我从命令行编译代码时,我收到与

> Hi.java:4: package javax.servlet does not exist
import javax.servlet.*;
^
Hi.java:5: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
Hi.java:13: cannot find symbol
symbol: class HttpServlet
public class Hi extends HttpServlet {
                        ^
Hi.java:15: cannot find symbol
symbol  : class HttpServletRequest
location: class Hi
    public void doGet(HttpServletRequest request,
                      ^
Hi.java:16: cannot find symbol
symbol  : class HttpServletResponse
location: class Hi
                      HttpServletResponse response)
                      ^
Hi.java:17: cannot find symbol
symbol  : class ServletException
location: class Hi
        throws IOException, ServletException

我设置了以下环境(Windows XP 环境)变量相关的错误:

JAVA_HOME: C:\Program Files\Java\jdk1.6.0_21 CATALINA_HOME: C:\Apache Tomcat\apache-tomcat-7.0.22

All,

I just installed apache tomcat and testing the installtion did show the "Successfully installed" apache page. I did execute the HelloWorld example without any issues. So now, I created my own web application under \apache-tomcat-XXX\webapps\mine with following 2 subfolders:

\apache-tomcat-XXX\webapps\mine\classes
\apache-tomcat-XXX\webapps\mine\lib

I created a new class file HelloWorldAgain.java with following contents:

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * My test servlet
 *
 * @author Liz Warner
 */

public class Hi extends HttpServlet {

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Hola</title>");
        out.println("</head>");
        out.println("<body bgcolor=\"white\">");
        out.println("<h1> Hi </h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

However, when I compile the code from command line, I get errors related to

> Hi.java:4: package javax.servlet does not exist
import javax.servlet.*;
^
Hi.java:5: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
Hi.java:13: cannot find symbol
symbol: class HttpServlet
public class Hi extends HttpServlet {
                        ^
Hi.java:15: cannot find symbol
symbol  : class HttpServletRequest
location: class Hi
    public void doGet(HttpServletRequest request,
                      ^
Hi.java:16: cannot find symbol
symbol  : class HttpServletResponse
location: class Hi
                      HttpServletResponse response)
                      ^
Hi.java:17: cannot find symbol
symbol  : class ServletException
location: class Hi
        throws IOException, ServletException

I have set the following environement (Windows XP env.) variables:

JAVA_HOME: C:\Program Files\Java\jdk1.6.0_21
CATALINA_HOME: C:\Apache Tomcat\apache-tomcat-7.0.22

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ζ澈沫 2024-12-17 13:48:02

您的 Java 类使用不属于 Java SE 的其他 Java 类(“普通”或“桌面”Java),例如 javax.servlet 中的 Java EE 类。因此,编译器不知道它们,除非您告诉它在哪里查找这些类。您需要找到类似 javaee.jarservlet.jar 的东西——Tomcat 应该将它们包含在其 libs/ 目录中,尽管我忘记了正是他们的名字。

然后,您可以使用 javac -cp path/to/javaee.jar ... 将它们添加到编译命令中。

这实际上与 Tomcat 或 J2EE 无关,它只是基本的 Java 编译。

Your Java class uses other Java classes that are not part of Java SE ("normal" or "desktop" Java) -- the Java EE classes in javax.servlet for example. So, the compiler doesn't know about them unless you tell it where to look for those classes. You need to find something like javaee.jar or servlet.jar -- Tomcat ought to contain them in its libs/ dir, though I forget exactly what they are called.

Then you add them to your compile command with javac -cp path/to/javaee.jar ... for example.

This is really nothing specific to Tomcat or J2EE, it's just basic Java compiling.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文