我用的eclipse,建立的項目運行,照著視頻抄的,出問題了,急求幫助
web.xml
<?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_3_1.xsd" id="WebApp_ID"?version="3.1"> <display-name>MicroMessage</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>ListServlet</servlet-name> <!--?servlet類?--> <servlet-class>com.imooc.servlet.ListServlet</servlet-class> </servlet> <!--?添加映射?--> <servlet-mapping> <servlet-name>ListServlet</servlet-name> <url-pattern>/List.action</url-pattern> </servlet-mapping> </web-app>
ListServlet.java
package?com.imooc.servlet; import?java.io.IOException; import?javax.servlet.ServletException; import?javax.servlet.http.HttpServlet; import?javax.servlet.http.HttpServletRequest; import?javax.servlet.http.HttpServletResponse; /** ?*?列表頁面初始化控制 ?*? ?* ?*?@SuppressWarnings("serial")?類可序列化的原因 ?*/ @SuppressWarnings("serial") public?class?ListServlet?extends?HttpServlet?{ //?實現doGet方法 @Override protected?void?doGet(HttpServletRequest?req,?HttpServletResponse?resp) throws?ServletException,?IOException?{ req.getRequestDispatcher("/WEB-INF/jsp/back/list.jsp").forward(req,?resp); } @Override protected?void?doPost(HttpServletRequest?req,?HttpServletResponse?resp) throws?ServletException,?IOException?{ //?TODO?Auto-generated?method?stub this.doGet(req,?resp); } }
index.jsp
<%@?page?language="java"?contentType="text/html;?charset=UTF-8" pageEncoding="UTF-8"%> <% String?path?=?request.getContextPath(); String?basePath?=?request.getScheme()?+?"://" +?request.getServerName()?+?":"?+?request.getServerPort() +?path?+?"/"; %> <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?"http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <base?href="<%=basePath%>"> <meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8"> <title>Insert?title?here</title> </head> <body> </body> </html>
list.jsp
<%@?page?language="java"?contentType="text/html;?charset=UTF-8" pageEncoding="UTF-8"%> <%?String?path=request.getContextPath(); ???String?basePath?=?request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; ???<base?href="<%=basePath%>">?%> <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html?xmlns="http://www.w3.org/1999/xhtml"> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8"?/> <meta?http-equiv="X-UA-Compatible"?content="IE=9;?IE=8;?IE=7;?IE=EDGE"?/> <title>內容列表頁面</title> <link?href="<%=basePath?%>resources/css/all.css"?rel="stylesheet"?type="text/css"?/> </head> <body?style="background:?#e1e9eb;"> <form?action=""?id="mainForm"?method="post"> <div?class="right"> <div?class="current"> 當前位置:<a?href="javascript:void(0)"?style="color:?#6E6E6E;">內容管理</a> >?內容列表 </div> <div?class="rightCont"> <p?class="g_title?fix"> 內容列表?<a?class="btn03"?href="#">新?增</a> <a class="btn03"?href="#">刪?除</a> </p> <table?class="tab1"> <tbody> <tr> <td?width="90"?align="right">演示字段1:</td> <td><input?type="text"?class="allInput"?value=""?/></td> <td?width="90"?align="right">演示字段2:</td> <td><input?type="text"?class="allInput"?value=""?/></td> <td?width="85"?align="right"><input?type="submit" class="tabSub"?value="查?詢"?/></td> </tr> </tbody> </table> <div?class="zixun?fix"> <table?class="tab2"?width="100%"> <tbody> <tr> <th><input?type="checkbox"?id="all"?onclick="#"?/></th> <th>序號</th> <th>演示字段1</th> <th>演示字段2</th> <th>操作</th> </tr> <tr> <td><input?type="checkbox"?/></td> <td>1</td> <td>演示值1</td> <td>演示值2</td> <td><a?href="#">修改</a> ?<a?href="#">刪除</a> </td> </tr> <tr?style="background-color:?#ECF6EE;"> <td><input?type="checkbox"?/></td> <td>2</td> <td>演示值1</td> <td>演示值2</td> <td><a?href="#">修改</a> ?<a?href="#">刪除</a> </td> </tr> <tr> <td><input?type="checkbox"?/></td> <td>3</td> <td>演示值1</td> <td>演示值2</td> <td><a?href="#">修改</a> ?<a?href="#">刪除</a> </td> </tr> <tr?style="background-color:?#ECF6EE;"> <td><input?type="checkbox"?/></td> <td>4</td> <td>演示值1</td> <td>演示值2</td> <td><a?href="#">修改</a> ?<a?href="#">刪除</a> </td> </tr> </tbody> </table> <div?class='page?fix'> 共?<b>4</b>?條?<a?href='###'?class='first'>首頁</a>?<a?href='###' class='pre'>上一頁</a>?當前第<span>1/1</span>頁?<a?href='###' class='next'>下一頁</a>?<a?href='###'?class='last'>末頁</a>?跳至 <input type='text'?value='1'?class='allInput?w28'?/> 頁 ?<a href='###'?class='go'>GO</a> </div> </div> </div> </div> </form> </body> </html>
2018-06-02
我很好奇, 放在WEB-INF下的jsp網頁不是不能直接訪問嗎, 我試了好久, 最后把jsp文件夾放在Webroot下, 再修改servlet的forward路徑, 才可以在瀏覽器里面直接輸入url訪問jsp
2017-09-15
代碼沒毛病,我用eclipse跑的,能走通. 檢查你的tomcat配置
2017-09-15
在eclipse上我試了下清緩存,打斷點之類的操作,根本http://localhost:8080/MicroMessage/List.action請求就沒進入到ListServlet類,看了幾篇博客又試了下,用IntelliJ IDEA重新構建的項目,發現按照視頻套路出牌確實還是報錯,然后我就試了下http://localhost:8090/List.action? (我將發布的請求端口號改掉了8090,因為本機HTTP??port 8080和JMX port 1099都被占用)。這樣確實是請求到頁面了。理解:從web.xml配置文件出發,也能理解成/List.action映射到ListServlet類,執行類的方法,跳轉到了list.jsp,不知道我這樣理解對還是錯?
2017-09-15
早上我試了下清緩存,打斷點之類的操作,根本http://localhost:8090/List.action請求就沒進入到ListServlet類,看了幾篇博客又試了下,用IntelliJ IDEA重新構建的項目,發現按照視頻套路出牌確實還是報錯,然后我就試了下http://localhost:8090/List.action,這樣確實是請求到頁面了。理解:從web.xml配置文件出發,也能理解成/List.action映射到ListServlet類,執行類的方法,跳轉到了list.jsp,不知道我這樣理解對還是錯?
2017-09-15
應該是web.xml映射到listSerlet.class由類內部doGet方法跳轉到list.jsp才對啊。和表單沒啥關系。訪問是通過瀏覽器URL訪問的/List.Action。求解
2017-09-15
404錯誤一般是找不到servlet對應的url? 你的jsp頁面中的form的action沒填
建議你跟著老師一步一步敲代碼吧 老師講得很好