如何在java servlet项目中引用更高层次的文件
我正在尝试在 Netbeans 中为 Java Web 应用程序项目实现基本的 MVC 模式。我的部署描述符(web.xml)是正确的,但我似乎无法从java servlet本身重定向到正确的jsp。这是我的项目文件夹的目录:
/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
,但鉴于上述目录,我不知道使用什么路径/文件名来引用 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);
更新: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>
i'm trying to implement a basic mvc pattern for a java web app project in netbeans. i have the deployment descriptor (web.xml) correct but i can't seem to redirect to the correct jsp from the java servlet itself. here's the directory of my project folder:
/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 */
a link from index.jsp
calls the ManageSurveysServlet
which in turn forwards a request
object and redirects to manage_surveys.jsp
, but given the above directories, i don't know what path/filename to use to reference manage_surveys.jsp
with from the servlet. here's the code in the servlet that forwards the request object:
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
manage_surveys.jsp
移动到WEB-INF
文件夹中。我认为这将是解决方案。Move
manage_surveys.jsp
intoWEB-INF
folder.I think that will be solution.Javadoc 说给定的资源是相对路径。因此,在类目录中执行意味着,您必须将 jsp 放在“类”目录中,但这不是一个好主意,但出于测试原因,您可以尝试或
将代码更改为上下文根relative:
或relative:
供参考的Javadoc:
http://docs.oracle .com/javaee/5/api/javax/servlet/ServletRequest.html#getRequestDispatcher(java.lang.String)
The Javadoc says the given resource is a relative path. So executed in the classes dir means, you have to either put your jsp in the "classes" dir, but thats not a good idea, but for testing reasons u may try or
change your code to context root relative:
or relative:
The Javadoc for reference:
http://docs.oracle.com/javaee/5/api/javax/servlet/ServletRequest.html#getRequestDispatcher(java.lang.String)