【问题标题】:how to reference a file of higher hierarchy in a java servlet projectjava - 如何在java servlet项目中引用更高层次的文件
【发布时间】:2011-12-02 06:21:11
【问题描述】:

我正在尝试在 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,但鉴于上述目录,我不知道使用什么路径/文件名来引用@ 987654326@ 来自 servlet。这是 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>

【问题讨论】:

  • 我不确定,但"/" + request.getContextRoot() + "/manage_surveys.jsp" 有效吗?
  • 恐怕HttpServletRequest对象request没有getContextRoot()方法
  • 我的错,是getContextPath()

标签: java model-view-controller servlets


【解决方案1】:

manage_surveys.jsp 移动到WEB-INF 文件夹中。我认为这将是解决方案。

【讨论】:

    【解决方案2】:

    Javadoc 说给定的资源是一个相对路径。所以在类目录中执行意味着,您必须将您的 jsp 放在“类”目录中,但这不是一个好主意,但出于测试原因,您可以尝试或 将您的代码更改为上下文根相关:

    RequestDispatcher dispatcher = request.getRequestDispatcher("/manage_surveys.jsp");
    

    或相对:

    RequestDispatcher dispatcher = request.getRequestDispatcher("../../manage_surveys.jsp");
    

    Javadoc 供参考: http://docs.oracle.com/javaee/5/api/javax/servlet/ServletRequest.html#getRequestDispatcher(java.lang.String)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-04
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-23
      • 2012-04-22
      相关资源
      最近更新 更多