servlet WebServlet 注解 404錯誤
我用Myeclipse創建web project,名字叫IMooc后創建servlet,servlet自己寫好了并生成了@WebServlet?
但是啟動后提示404 怎么回事呢?
訪問http://localhost:8080/IMooc/TestServlet出錯404
訪問index.jsp(http://localhost:8080/IMooc/index.jsp)完全沒問題
servlet具體如下:
@WebServlet(description = "Only For Test", urlPatterns = { "/TestServlet" })
public class TestServlet extends HttpServlet {...}//自己生成的什么都沒寫
web.xml如下,也是自己生成的,沒改動:
<?xml version="1.0" encoding="UTF-8"?>
<web-app?
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?
xmlns="http://java.sun.com/xml/ns/javaee"?
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"?
id="WebApp_ID" ?version="3.0">
? <display-name>IMooc</display-name>
? <welcome-file-list>
? ? <welcome-file>index.jsp</welcome-file>
? </welcome-file-list>
</web-app>
2018-11-28
試試勾上這個
2018-07-14
那個8080端口都一樣么?
2018-05-21
應該是路徑問題,jsp所在路徑和Servlet所在路徑不在同一等級目錄,所以沒找到Servlet。
試著把超鏈接中的路徑修改下,向上一級
../servlet/HelloServlet
我用的eclise在Webcontent 下建了pag目錄,下面放的jsp,修改了超鏈接的路徑就好了。
2018-05-14
用a標簽跳轉的話,href應該="TestServlet",表單的話也一樣。
其實就是把 @WebServlet(description = "Only For Test", urlPatterns = { "/TestServlet" })中的 /TestServlet
去掉? /? 后加入href中。
你這個web.xml中沒有自動生成servlet(沒生成也不影響跳轉),因為你的項目是version="3.0",建項目時用3.0以下
就可以自動生成了