Change default homepage in root path to servlet with doGet

前端 未结 2 495
遥遥无期
遥遥无期 2020-11-22 10:22

I have a small maven (indirectly through Netbeans 8.1 & tomcat setup)

And whenever I ran the project it opens the browser with a HelloWord on the root:

相关标签:
2条回答
  • 2020-11-22 10:39

    How can I change the contents of this page to something else ?

    Open the underlying JSP/HTML/XHTML file in a text editor. This page is identified by <welcome-file> entry in web.xml. If it's e.g. <welcome-file>index.jsp</welcome-file>, then you need to open /index.jsp file in your project's web content in the IDE builtin text editor.


    Or, at the very least (if the former is impossible): Can I use a permanent redirect on root path to avoid the user from seeing this page?

    This question is badly thought out. You don't want to redirect the visitor forth and back all time. You want to map your servlet on webapp root. In order to map a servlet on root path, use the empty string URL pattern "" instead of the default servlet URL pattern "/" as in your attempt.

    @WebServlet("")
    

    Or if you're still not on Servlet 3.0 yet, here's the old fashioned web.xml way.

    <servlet-mapping>
        <servlet-name>yourHomeServlet</servlet-name>
        <url-pattern></url-pattern> <!-- Yes, empty string! -->
    </servlet-mapping>
    

    If you still keep using the default servlet URL pattern of "/", then you have to take over all responsibilities of the container's builtin default servlet such as serving up static resources like CSS files, adding browser-caching headers, supporting file download resumes, etc. See also the first related link below for detail.

    At least there's no need to abuse <welcome-file> for this. This does not represent the "homepage file" as many starters seem to expect. This represents "folder's default file to serve when any subfolder is requested". Thus not only on /, but also on /foo/, /bar/, etc.

    See also:

    • Difference between / and /* in servlet mapping url pattern
    • How to configure welcome file list in web.xml
    0 讨论(0)
  • 2020-11-22 10:52

    Whenever you define a <welcome-file>index.jsp</welcome-file> in web.xml the landing page, when you launch the application will be index.jsp page i.e by default the servlet path will be "/index.jsp" (http://localhost:8084/index.jsp). However in the browser that won't be displayed. In your servlet class you can match this pattern and redirect to some other page if required.

    0 讨论(0)
提交回复
热议问题