如何在java servlet项目中引用更高层次的文件

发布于 2024-12-19 04:12:47 字数 2381 浏览 3 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终止放荡 2024-12-26 04:12:47

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

Move manage_surveys.jsp into WEB-INF folder.I think that will be solution.

丢了幸福的猪 2024-12-26 04:12:47

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

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

或relative:

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

供参考的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:

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

or relative:

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

The Javadoc for reference:
http://docs.oracle.com/javaee/5/api/javax/servlet/ServletRequest.html#getRequestDispatcher(java.lang.String)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文