0

So, I currently have this ->

    <servlet>
        <servlet-name>Home</servlet-name>
        <jsp-file>/index.jsp</jsp-file>
    </servlet>
    <servlet-mapping>
        <servlet-name>Home</servlet-name>
        <url-pattern>/home</url-pattern>
    </servlet-mapping>

Now, everything works fine however I don't want users to be able to access index.jsp at all, is there a way for me to make it so once you visit a url like index.jsp if the file has a servlet like the one above it will redirect them to that one?

Loran
  • 13
  • 5

1 Answers1

0

Move the JSP file into the /WEB-INF folder.

Files in the /WEB-INF folder cannot be accessed from outside, but are accessible from within the webapp.


UPDATE

Works perfectly with just 2 files below the webapps/test folder:

/path/to/tomcat/webapps/test/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <display-name>Test Application</display-name>
    <servlet>
        <servlet-name>Home</servlet-name>
        <jsp-file>/WEB-INF/index.jsp</jsp-file>
    </servlet>
    <servlet-mapping>
        <servlet-name>Home</servlet-name>
        <url-pattern>/home</url-pattern>
    </servlet-mapping>
    <session-config><session-timeout>30</session-timeout></session-config>
</web-app>

/path/to/tomcat/webapps/test/WEB-INF/index.jsp

<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World</title>
</head>
<body>
<h3>Hello World!</h3>
<p>This is a test</p>
</body>
</html>

Accessing http://localhost:8080/test/home shows1:

web page

Accessing http://localhost:8080/test/index.jsp shows 404 – Not Found.

Accessing http://localhost:8080/test/WEB-INF/index.jsp shows 404 – Not Found.

1) Tested on Apache Tomcat/9.0.27 running Java 11.0.2+9.

Andreas
  • 154,647
  • 11
  • 152
  • 247