You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

生产环境访问WEB-INF内页面出现Error 404,开发环境正常

问题描述

生产环境中访问WEB-INF目录下的页面时出现Error 404错误,但开发环境无此问题。服务器使用Tomcat 9、Java 8,服务器日志未显示任何错误信息,寻求技术帮助。

相关代码

Servlet代码

package br.com.bahiatransport.bahiatransport.controller;

import br.com.bahiatransport.bahiatransport.model.ModelSlctcMudancaResidencial;
import br.com.bahiatransport.bahiatransport.model.ModelUser;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name = "ControllerServlet", urlPatterns = {
    "/authentication",
    "/main"}, loadOnStartup = 1)
public class ControllerServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userPath = request.getServletPath();
        switch (userPath) {
            case "/authentication":

                break;
            case "/main":

                break;
            default:
                throw new AssertionError();
        }

        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";
        try {
            request.getRequestDispatcher(url).forward(request, response);
        } catch (ServletException | IOException ex) {
            throw new RuntimeException("Erro no ControllerServlet: " + ex);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userPath = request.getServletPath();
        switch (userPath) {
            case "/authentication":
                ModelUser user = new ModelUser();
                user.setUsername(request.getParameter("user"));
                user.setPassword(request.getParameter("password"));
                ControllerUser controllerUser = new ControllerUser();
                if (controllerUser.authentication(user)) {
                    // LOGIN BEM SUCEDIDO.
                    HttpSession session = request.getSession();
                    session.setAttribute("user", user);
                    session.setAttribute("idSession", session.getId());
                    List<ModelSlctcMudancaResidencial> list = new ArrayList<>();
                    ControllerSlctcMudancaResidencial controllerSlctcMudancaResidencial = new ControllerSlctcMudancaResidencial();
                    controllerSlctcMudancaResidencial.findAll(list);
                    session.setAttribute("mudancaResidencial", list);
                    userPath = "/main";
                }
                break;
            case "/main":

                break;
            default:
                throw new AssertionError();
        }

        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";
        try {
            request.getRequestDispatcher(url).forward(request, response);
        } catch (ServletException | IOException ex) {
            throw new RuntimeException("Erro no ControllerServlet: " + ex);
        }
    }

}

登录JSP代码

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="pt-BR">
    <head>
        <meta charset="utf-8">
        <title>Login</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="favicon.ico" rel="shortcut icon"/>
        <meta name="robots" content="index, follow">
        <meta name="Googlebot" content="index, follow">
        <link rel="canonical" href="https://bahiatransport.com.br/login">
        <link href="style-header.css" rel="stylesheet" type="text/css" media="screen">
        <link href="style.css" rel="stylesheet" type="text/css" media="screen">
    </head>
    <body>
        <%@include file="WEB-INF/jspf/header.jspf" %>
        <main>
            <div id="outer-main-1">
                <div class="wrapper">
                    <div id="breadcrumbs">
                        <p><a href="<c:url value="index.jsp"/>" >Home</a> / Login</p>
                    </div>
                    <section id="banner-3">
                        <h1>Welcome to Bahia Transport</h1>
                        <form action="<c:url value="/authentication"/>" method="post">
                            <fieldset>
                                <legend>User</legend>
                                <input type="text" id="user" name="user" required="requered">
                            </fieldset> 
                            <fieldset>
                                <legend>Password</legend>
                                <input type="password" id="password" name="password" minlength="6" required="requered">
                            </fieldset> 
                            <button type="submit" class="submit_login">Login</button>
                        </form>
                    </section>
                </div>
            </div>
        </main>
        <%@include file="WEB-INF/jspf/footer.jspf" %>
    </body>
</html>
排查与解决方案
  • 检查文件路径大小写:生产环境若为Linux系统,文件名区分大小写。确认WEB-INF/view目录下的JSP文件名与代码中拼接的完全一致(比如代码中是authentication.jsp,实际文件不能是Authentication.jsp)。
  • 验证WAR包完整性:解压生产环境部署的WAR包,确认WEB-INF/view目录下存在对应的JSP文件(authentication.jsp、main.jsp),且目录结构与开发环境一致。
  • 核对路径拼接逻辑:代码中转发路径为/WEB-INF/view + userPath + .jsp,比如访问/authentication时,实际访问的是/WEB-INF/view/authentication.jsp,确认该文件确实存在,而非放在其他目录(如WEB-INF/jsp)。
  • 完善异常日志:当前代码捕获异常后仅抛出RuntimeException,Tomcat可能未记录该错误。修改异常处理代码,添加ex.printStackTrace()将异常信息打印到Tomcat日志,或引入日志框架(如Log4j)记录详细错误,以便定位具体缺失的文件。
  • 检查Tomcat配置:查看生产环境Tomcat的conf/web.xml,确认没有禁用RequestDispatcher转发的配置,也没有URL重写规则干扰路径解析。

内容的提问来源于stack exchange,提问作者Alexandre Bezerra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.01 23:24:54