我使用Netbeans和Glassfish服务器开发了一个WebApplication。我想在Tomcat服务器上部署它。但我无法在Tomcat上运行servlet。我做了以下Tomcat上的Servlet配置
- 创建的文件夹“FTI” Tomcat的根目录中
- 放置的index.jsp FTI目录内,我能够通过我的浏览器
- 我创建内部WEB-INF文件夹访问“fti”文件夹并将web.xml文件放入其中。
- 我在“WEB-INF”文件夹内创建了classes文件夹,并将编译后的java文件test.class放入其中。
- 我试着通过浏览器访问/ fti/test来调用servlet,并且因为“请求的资源(/ fti/test)不可用而出现错误。”
我能够运行使用使用Netbeans相同的web.xml文件和GlassFish服务器相同的servlet
我test.java
文件:
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 processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet test</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet test </h1>");
out.println("</body>");
out.println("</html>");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
我的web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
在步骤4访问它,您是否将手动.class文件放入类文件夹? – Rembo
你确定你的代码编译成功,没有这个错误:'尝试'没有'catch','最后'或资源声明 – Rembo
为什么地球上不使用IDE?它会自动执行许多这些步骤,并确保一切都在正确的位置。 – EJP