IT이야기

다른 스크립트를 계속 실행할 수 있도록 "루트" 서블릿을 어떻게 매핑할 수 있습니까?

cyworld 2021. 9. 12. 20:47
반응형

다른 스크립트를 계속 실행할 수 있도록 "루트" 서블릿을 어떻게 매핑할 수 있습니까?


다음과 유사한 JSP 페이지를 호출하는 서블릿을 빌드하려고 합니다.

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException, ServletException {
    req.getRequestDispatcher("/WEB-INF/main.jsp").forward(req, resp);
}

도메인의 루트(예: http://example.com/ ) 에 응답하려면 이 서블릿이 필요 하므로 web.xml에서 다음 매핑을 사용하고 있습니다.

<servlet-mapping>
    <servlet-name>MainServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

내가 겪고있는 문제는 이것이 모든 것과 일치하므로 디스패처가 "/WEB-INF/main.jsp"로 전달할 때 url 패턴과 일치하여 서블릿이 다시 실행된다는 것입니다. 그 결과 java.lang.StackOverflowError 와 함께 죽을 때까지 루프가 실행됩니다 .

다른 스크립트를 실행할 수 없도록 하지 않고 루트를 어떻게 일치시킬 수 있습니까?


예를 들어 빈 패턴을 사용하십시오.

<servlet-mapping>
    <servlet-name>MainServlet</servlet-name>
    <url-pattern></url-pattern>
</servlet-mapping>

서블릿 3.0 사양은 다음과 같이 설명했습니다.

빈 문자열("")은 애플리케이션의 컨텍스트 루트에 정확히 매핑되는 특수 URL 패턴입니다.

따라서 최소한 3.0 컨테이너에서 작동해야 하며 Jetty 8에서 작동하는지 확인했습니다.


web.xml의 welcome-file 요소를 사용하면 앱 엔진에서 저에게 효과적이었습니다 . 여기 내 것:

<web-app>
    <servlet>
        <servlet-name>RootServlet</servlet-name>
        <servlet-class>com.foo.RootServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>RootServlet</servlet-name>
        <url-pattern>/foo</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>foo</welcome-file>
    </welcome-file-list>
</web-app>

원래 질문에는 App Engine에서 루트 서블릿을 매핑하려고 한다는 언급이 없습니다. Tomcat(및 내가 아는 한 다른 서블릿 컨테이너)에서는 쉽지만 App Engine은 일반 서블릿 컨테이너가 아닙니다.

서블릿으로 웹 애플리케이션을 구축하는 일반적인 방법은 HttpServlet을 확장하고 제목, 콘텐츠, 오류, 메시지 등이 포함된 "페이지" 개체를 추가하고 출력을 JSP 템플릿으로 전달하는 것입니다. 이것은 App Engine에서 작동하는 절대적인 악몽이었습니다.

  • JSP 파일은 시작 부분에 "/" 없이 "이름 지정"할 수 없습니다.
  • JSP 파일은 하위 디렉토리에 있을 수 없습니다.
  • 서블릿은 "/" URL 패턴을 사용하여 애플리케이션의 루트에 매핑할 수 없습니다.

다음은 마침내 작동한 web.xml(간결함을 위해 편집됨)입니다.

<web-app>
  <servlet>
    <!-- this servlet needs to redirect to a NamedDispatcher
         called "template" -->
    <servlet-name>Home</servlet-name>
    <servlet-class>my.domain.HomeSv</servlet-class>
  </servlet>
  <servlet>
    <!-- jsp file must apparently be in root directory and have "/" at
         start of path -->
    <servlet-name>template</servlet-name>
    <jsp-file>/template.jsp</jsp-file>
  </servlet>
  <servlet-mapping>
    <!-- map your home servlet to somewhere other than "/" -->
    <servlet-name>Home</servlet-name>
    <url-pattern>/home</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <!-- make your Home servlet the welcome file -->
    <welcome-file>home</welcome-file>
  </welcome-file-list>
</web-app>

나는 이 모든 것을 검증하는 것에 대해 특별히 과학적이지는 않았지만 지금은 효과가 있는 것 같고 그것에 대해 꽤 만족합니다.


JSTL 등을 사용하여 다음 코드를 사용하여 루트에 index.jsp라는 시작 파일을 만들 수 있습니다.

<c:redirect url="/main"/>

따라서 web.xml 파일에는 다음이 포함됩니다.

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>        
</welcome-file-list>

따라서 루트를 요청하는 사람은 /main으로 리디렉션됩니다. 이제 서블릿을 메인에 매핑할 수 있습니다.

<servlet-mapping>
    <servlet-name>MainServlet</servlet-name>
    <url-pattern>/main</url-pattern>
</servlet-mapping>

패턴에서 '*'를 제거하려고 시도하십시오. 즉,

<url-pattern>/</url-pattern>

A solution is mentioned in another thread URL Pattern for servlet mapping in web.xml using URLrewrite -> http://tuckey.org/urlrewrite/


Have you tried the below? (Note the missing *, which is a wild card and is the reason that your configuration catches everything.)

<servlet-mapping>
        <servlet-name>MainServlet</servlet-name>
        <url-pattern>/index.jsp</url-pattern>
</servlet-mapping>

(Edited as per comments from just /.)


You can't redirect to WEB-INF. A servlet container will never serve requests for documents in that folder.

If you want your application (not just a servlet, but the entire application) to be available under the root context ("/" of "http://www.domainname.com/"), then you need to set up a context entry for it - not a servlet mapping.

With Tomcat, you add a new <Context> mapping (in one of about 3 different possible places).

ReferenceURL : https://stackoverflow.com/questions/1030302/how-can-i-map-a-root-servlet-so-that-other-scripts-are-still-runnable

반응형