我用jsdk编译一个servlet程序,程序如下
package test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Test extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
out.println("<html><body><h1>This is a servlet test.</h1></body></html>");
out.flush();
}
}
编译后,提示
C:\test>javac Test.java
Test.java:4: package javax.servlet does not exist
import javax.servlet.ServletException;
^
Test.java:5: package javax.servlet.http does not exist
import javax.servlet.http.HttpServlet;
^
Test.java:6: package javax.servlet.http does not exist
import javax.servlet.http.HttpServletRequest;
^
Test.java:7: package javax.servlet.http does not exist
import javax.servlet.http.HttpServletResponse;
^
Test.java:8: cannot resolve symbol
symbol : class HttpServlet
location: class test.Test
public class Test extends HttpServlet {
^
Test.java:9: cannot resolve symbol
symbol : class HttpServletRequest
location: class test.Test
protected void doGet(HttpServletRequest request, HttpServletResponse
^
Test.java:9: cannot resolve symbol
symbol : class HttpServletResponse
location: class test.Test
protected void doGet(HttpServletRequest request, HttpServletResponse
^
Test.java:10: cannot resolve symbol
symbol : class ServletException
location: class test.Test
throws ServletException, IOException {
^
8 errors
是没加载servlet包吗,怎么解决?
package test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Test extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
out.println("<html><body><h1>This is a servlet test.</h1></body></html>");
out.flush();
}
}
编译后,提示
C:\test>javac Test.java
Test.java:4: package javax.servlet does not exist
import javax.servlet.ServletException;
^
Test.java:5: package javax.servlet.http does not exist
import javax.servlet.http.HttpServlet;
^
Test.java:6: package javax.servlet.http does not exist
import javax.servlet.http.HttpServletRequest;
^
Test.java:7: package javax.servlet.http does not exist
import javax.servlet.http.HttpServletResponse;
^
Test.java:8: cannot resolve symbol
symbol : class HttpServlet
location: class test.Test
public class Test extends HttpServlet {
^
Test.java:9: cannot resolve symbol
symbol : class HttpServletRequest
location: class test.Test
protected void doGet(HttpServletRequest request, HttpServletResponse
^
Test.java:9: cannot resolve symbol
symbol : class HttpServletResponse
location: class test.Test
protected void doGet(HttpServletRequest request, HttpServletResponse
^
Test.java:10: cannot resolve symbol
symbol : class ServletException
location: class test.Test
throws ServletException, IOException {
^
8 errors
是没加载servlet包吗,怎么解决?
如果你用tomcat,可参考
http://www.chinaunix.net/jh/26/148405.html
添加到classpath
将tomcat服务器下的:
C:\Tomcat 5.0\common\lib\servlet-api.jar 文件拷贝到
JDK目录下的:
C:\Java\jdk1.5.0\jre\lib\ext 的文件夹下
注意:以上tomcat和jdk的目录就是已安装的实际位置,并不一定是C盘!
后,报错:
HTTP Status 404 - /myapp/Test
type Status report
message /myapp/Test
description The requested resource (/myapp/Test) is not available.
Apache Tomcat/5.0.11
此时,http://localhost:8080/myapp/index.jsp好使,可以看到输出页面。但是其他一般的jsp都不好使了。http://localhost:8080/myapp/second.jsp(原本好使的)
设置了JAVA_HOME,path,CATALINA_HOME,CATALINA_BASE,classpath,其他的未设置。
请高手们指点问题在哪里,如何解决,谢谢!!