Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change default homepage in root path to servlet with doGet

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

like image 763
DeMarco Avatar asked Oct 20 '15 23:10

DeMarco


1 Answers

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
like image 100
BalusC Avatar answered Oct 21 '22 17:10

BalusC