java.lang.ClassNotFoundException: javax.servlet.ServletContext解决方法

it2025-01-12  6

1.在配置selvlet的web.xml时,仅在文件中添加以下代码 <servlet> <servlet-name>UserServlet</servlet-name> <servlet-class>com.demo.servlet.UserServlet</servlet-class> </servlet>

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>demo</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>UserServlet</servlet-name> <servlet-class>com.demo.servlet.UserServlet</servlet-class> </servlet> <!-- <servlet-mapping> <servlet-name>UserServlet</servlet-name> <url-pattern>/UserServlet.do</url-pattern> </servlet-mapping> --> </web-app>

2.在UserServlet(接收用户请求的Java类)里添加以下代码: @WebServlet("/UserServlet.do")

ackage come.demo.servlet; import java.io.*; import java.lang.reflect.InvocationTargetException; import javax.servlet.*; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import com.demo.javaBean.UserBean; @WebServlet("/UserServlet.do") /* /UserServlet.do表示访问该servlet的 url 映射(地址)(此处为相对路径,即 “项目名称/UserServlet.do”) */

3.不要在web.xml文件里添加以下代码

<servlet-mapping> <servlet-name>UserServlet</servlet-name> <url-pattern>/UserServlet.do</url-pattern> </servlet-mapping>

第2步和第3步的代码不能同时由否则会造成Tomcat启动失败

最新回复(0)