Spring MVC初始页面加载异常:始终跳转至index.jsp而非hello.jsp
Hey there, I totally get how frustrating this can be—you set up your hello.jsp expecting it to load first, but Tomcat keeps pulling up index.jsp instead. Let's walk through the most common fixes that usually resolve this issue:
1. Adjust the Welcome File Configuration
Tomcat uses a list of "welcome files" to determine which page loads when you hit the root URL of your project. You can update this either globally for all Tomcat projects, or just for your specific app.
For your project's local WEB-INF/web.xml (recommended, since it only affects your app):
Add or modify the <welcome-file-list> section to put hello.jsp at the top of the list. This tells Tomcat to check for hello.jsp first before falling back to other default pages:
<welcome-file-list> <welcome-file>hello.jsp</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list>
For global Tomcat conf/web.xml (affects all apps on the server):
If you want this change to apply to every project running on your Tomcat instance, edit the same section in Tomcat's main conf/web.xml file. Just make sure to restart Tomcat after saving the changes.
2. Verify Your Project Structure & File Location
Double-check that hello.jsp is placed in the root directory of your project (not inside WEB-INF—files in WEB-INF can't be accessed directly by Tomcat's welcome file mechanism). Your project should look something like this:
your-project/ ├── hello.jsp <-- This needs to be at the root ├── index.jsp └── WEB-INF/ ├── web.xml └── classes/ └── [your compiled classes]
3. Check for Unintended Redirects via Filters/Servlets
Sometimes custom filters or servlets in your project might be redirecting requests to index.jsp automatically. Look through your web.xml for any <filter> or <servlet> entries that map to the root path (/). For example, if you have a filter like this:
<filter-mapping> <filter-name>MyCustomFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping>
Check the filter's code to see if it's forcing a redirect to index.jsp.
4. Clear Tomcat's Cache & Restart
Tomcat sometimes caches old configuration files or compiled JSPs, which can cause unexpected behavior. Try these steps:
- Stop your Tomcat server completely
- Delete all contents in the
workdirectory of your Tomcat installation (this is where cached files are stored) - Restart Tomcat and test again
If none of these steps work, feel free to share your project's exact directory structure and the full content of your web.xml file—I can help dig deeper into what's going on.
内容的提问来源于stack exchange,提问作者Hardik Gami

