Change default homepage in root path to servlet with doGet

霸气de小男生 提交于 2019-11-25 23:38:42

问题


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:

i.e the page on http://localhost:8084/ is:

<html>
    <head>
        <title>Start Page</title>
        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>

I\'ve tried to create a servlet to replace it using:

@WebServlet(name = \"HomeServlet\", urlPatterns = {\"/\"}) however, it did not work as expected.

I.e. it still showed the same hello world on: http://localhost:8084

But it did mess with all the files on the root i.e http://localhost:8084/foo.css was being handled by this servlet as well and getting its response.

So, my question is (actually two):

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

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?

(i.e. http code 301) to move the user to http://localhost:8084/home


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/33248473/change-default-homepage-in-root-path-to-servlet-with-doget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!