当前位置 主页 > 网站技术 > 代码类 >

    IDEA JavaWeb项目启动运行后出现404错误的解决方法

    栏目:代码类 时间:2019-12-08 18:08

    404这个错误真的是一言难尽!不过大多是配置文件出错,认真修改还是可以的

    1.web.xml配置错误:

    默认首页没有写的,在web.xml添加一个就行(前提是你有jsp页面,名字不要写错):
    比如这个:

    <welcome-file-list>
     <welcome-file>Login.jsp</welcome-file>
     </welcome-file-list>

    2.IDEA JavaWeb项目运行找不到对应Servlet映射的JSP页面

    在servlet3.0以后提供了注解配置,这样就不需要在web.xml中配置。使用IDEA生成的servlet文件的注解配置如下(第一行):

    @WebServlet(value = "/AdminServlet")
    public class AdminServlet extends HttpServlet {
    
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      doPost(request, response);
     }
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     }
    }

    在这里插入图片描述

    对于怎样配置请看:

    路径:File > Settings > Editor > File and Code Templates > Other > Web > Java code templates 下第二个点开右边编辑模板
    如下(这是我的模板):

    #if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
    #parse("File Header.java")
    @javax.servlet.annotation.WebServlet(value = "/${Entity_Name}")
    public class ${Class_Name} extends javax.servlet.http.HttpServlet {
    
     protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
       doPost(request,response); 
     }
     protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
     } 
    }

    3.创建了一个和以前项目名称相同的项目(项目名称结构一样),把以前的覆盖了 然后运行就404了;

    第一时间想到的就是配置文件有错就开始找 :这个是我刚发现的解决办法:

    启动运行后跳转的页面发现不是你定义的页面报404错误,这就需要你在workspace.xml里面找了:

    你的项目名 > .idea > workspace.xml,找到里面不是你首次跳转的jsp的名字,修改为你定义的jsp名字

    具体在这里:把名字修改一下就好了

    在这里插入图片描述

     <component name="RunManager">
     <configuration name="tomcat" type="#com.intellij.j2ee.web.tomcat.TomcatRunConfigurationFactory" factoryName="Local" APPLICATION_SERVER_NAME="Tomcat 7.0.47" ALTERNATIVE_JRE_ENABLED="true" ALTERNATIVE_JRE_PATH="C:\Program Files\Java\jdk1.8.0_191\jre">
      <option name="OPEN_IN_BROWSER_URL" value="http://localhost:8080/Login.jsp" />
      <option name="UPDATING_POLICY" value="restart-server" />
      <deployment>
      <artifact name="SuShe:war exploded">
       <settings>
       <option name="CONTEXT_PATH" value="/" />
       </settings>
      </artifact>
      </deployment>
      <server-settings>
      <option name="BASE_DIRECTORY_NAME" value="Login_jsp_SuShe" />
      </server-settings>
      <predefined_log_file  enabled="true" />
      <predefined_log_file  enabled="true" />
      <predefined_log_file  enabled="false" />
      <predefined_log_file  enabled="false" />
      <predefined_log_file  enabled="false" />
      <RunnerSettings RunnerId="Debug">
      <option name="DEBUG_PORT" value="61238" />
      </RunnerSettings>
      <ConfigurationWrapper VM_VAR="JAVA_OPTS" RunnerId="Cover">
      <option name="USE_ENV_VARIABLES" value="true" />
      <STARTUP>
       <option name="USE_DEFAULT" value="true" />
       <option name="SCRIPT" value="" />
       <option name="VM_PARAMETERS" value="" />
       <option name="PROGRAM_PARAMETERS" value="" />
      </STARTUP>
      <SHUTDOWN>
       <option name="USE_DEFAULT" value="true" />
       <option name="SCRIPT" value="" />
       <option name="VM_PARAMETERS" value="" />
       <option name="PROGRAM_PARAMETERS" value="" />
      </SHUTDOWN>
      </ConfigurationWrapper>
      <ConfigurationWrapper VM_VAR="JAVA_OPTS" RunnerId="Debug">
      <option name="USE_ENV_VARIABLES" value="true" />
      <STARTUP>
       <option name="USE_DEFAULT" value="true" />
       <option name="SCRIPT" value="" />
       <option name="VM_PARAMETERS" value="" />
       <option name="PROGRAM_PARAMETERS" value="" />
      </STARTUP>
      <SHUTDOWN>
       <option name="USE_DEFAULT" value="true" />
       <option name="SCRIPT" value="" />
       <option name="VM_PARAMETERS" value="" />
       <option name="PROGRAM_PARAMETERS" value="" />
      </SHUTDOWN>
      </ConfigurationWrapper>
      <ConfigurationWrapper VM_VAR="JAVA_OPTS" RunnerId="Run">
      <option name="USE_ENV_VARIABLES" value="true" />
      <STARTUP>
       <option name="USE_DEFAULT" value="true" />
       <option name="SCRIPT" value="" />
       <option name="VM_PARAMETERS" value="" />
       <option name="PROGRAM_PARAMETERS" value="" />
      </STARTUP>
      <SHUTDOWN>
       <option name="USE_DEFAULT" value="true" />
       <option name="SCRIPT" value="" />
       <option name="VM_PARAMETERS" value="" />
       <option name="PROGRAM_PARAMETERS" value="" />
      </SHUTDOWN>
      </ConfigurationWrapper>
      <method v="2">
      <option name="BuildArtifacts" enabled="true">
       <artifact name="SuShe:war exploded" />
      </option>
      </method>
     </configuration>
     </component>