JSP in /WEB-INF returns “HTTP Status 404 The requested resource is not available”

前端 未结 3 860
無奈伤痛
無奈伤痛 2020-11-22 07:43

I created a JSP file.

sample.jsp

<%@ page pageEncoding="UTF-8"%>


  
    <         


        
相关标签:
3条回答
  • 2020-11-22 08:09

    It's mostly related to your directory structure or packaging.
    Can you please add your directory structure?

    Similar to below -

    src 
    |-html\
    |-jsp\
    

    Perhaps this should do it

    <form action="sample.jsp" method=get>
          <input type =submit value="submit">
    </form>
    

    Edit - WEB-INF does not allow direct access to JSP.

    0 讨论(0)
  • 2020-11-22 08:15

    In my spring boot web application with JSP it didn't worked until I have added this dependency

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
    

    even after configuring view resolver

    spring.mvc.view.prefix: /WEB-INF/views/ 
    spring.mvc.view.suffix: .jsp
    

    If you find why this worked please comment.

    0 讨论(0)
  • 2020-11-22 08:32

    404 simply means "Not Found".

    Either the URL is wrong (note: case sensitive!), or the resource is not there where you think it is.

    Just verify the URL and/or verify if the resource is there where you'd expect it to be. You placed sample.jsp in /WEB-INF folder. This way it is not publicly accessible without calling through a front controller servlet.

    Put it outside /WEB-INF.

    samplejsp
     `-- WebContent
          |-- WEB-INF
          `-- sample.jsp
    

    If you want to keep it in /WEB-INF, then you need to create a front controller servlet which forwards to it in doGet() method as below.

    request.getRequestDispatcher("/WEB-INF/sample.jsp").forward(request, response);
    

    Finally "open" the JSP by just calling servlet's actual URL instead of JSP's fictive URL.

    See also:

    • What is WEB-INF used for in a Java EE web application?
    • Calling servlet from JSP
    • doGet and doPost in Servlets
    0 讨论(0)
提交回复
热议问题