我試圖在netbeans中爲java web應用程序項目實現基本的mvc模式。我有部署描述符(web.xml)正確,但我似乎無法從java servlet本身重定向到正確的jsp。這裏是我的項目文件夾的目錄:如何在java servlet項目中引用更高層次結構的文件
/project
/src
/conf
MANIFEST.MF
/java
/ph
/com
/client
/esurvey
/objects
/* other .java files */
/servlets
ManageSurveysServlet.java
/* other .java files */
/build
/empty
/web
index.jsp
manage_surveys.jsp
script.js
style.css
/META-INF
context.xml
MANIFEST.MF
/WEB-INF
web.xml
/classes
.netbeans_update_resources
.netbeans_automatic_build
/ph
/com
/client
/esurvey
/objects
/* .class files found here */
/servlets
ManageSurveysServlet.class
/* other .class files found here */
從index.jsp
鏈接調用ManageSurveysServlet
這反過來又轉發request
對象,並重定向到manage_surveys.jsp
,但考慮到上面的目錄,我不知道是什麼路徑/文件名用來從servlet引用manage_surveys.jsp
。這裏是在轉發該請求對象的servlet代碼:
request.setAttribute("surveys", surveys); // surveys is an arraylist
RequestDispatcher dispatcher = request.getRequestDispatcher("manage_surveys.jsp"); // i'm guessing it can't find the jsp
dispatcher.forward(request, response);
UPDATE:web.xml中
<?xml version="1.0" encoding="utf-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>ManageSurveysServlet</servlet-name>
<servlet-class>ph.com.client.esurvey.servlets.ManageSurveysServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ManageSurveysServlet</servlet-name>
<url-pattern>/ManageSurveys</url-pattern>
</servlet-mapping>
<session-config><session-timeout>30</session-timeout></session-config>
<welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
</web-app>
我不知道,但確實' 「/」 + request.getContextRoot()+ 「/manage_surveys.jsp」'工作? – happymeal
恐怕HttpServletRequest對象'request'沒有方法getContextRoot() –
我的錯誤,它是'getContextPath()'。 – happymeal