Java项目服务启动后跳转页面显示404,需要手动输入路径才能访问首页
**方法一:**首先查看显示404的页面路径信息,路径中显示的上下文路径信息是否与你启动项目的项目名称一致,如若不一致需要修改一下项目本身的上下文路径
**方法二:**找到项目下的WebContent目录下的WEB-INF文件,打开web.xml文件,项目启动后我们往往都是通过web.xml文件来设置跳转的首页,设置如下:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file> <--此处写明要跳转的页面-->
</welcome-file-list>
这样配置完成后,启动项目时服务器会自动寻找项目下的index.jsp页面进行跳转。
**方法三:**通过代码实现跳转首页,在index.jsp中进行重定向,进入后代处理,然后再跳转首页,代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>基于SSM的禁毒教育平台</title>
</head>
<body>
<!--两种方式选择一种即可-->
<!--通过页面重定向进行,跳转到指定的页面-->
<%
response.sendRedirect(request.getContextPath()+"/index/index.jsp");
%>
<!--通过页面重定向进行java后台代码处理,根据后代处理进行跳转 -->
<%
response.sendRedirect(request.getContextPath()+"/login");
%>
</body>
</html>
**方法四:**通过过滤器方式进入首页,其处理的逻辑为获取URL,并对URL进行判断,判断URL携带的参数,如果URL携带参数正确,其可以进行正常的处理和响应,当没有携带对应的参数时,则进入首页。此处代码省略。同时需要在web.xml中配置监听器,配置监听器代码如下:
<filter>
<filter-name>SystemFilter</filter-name>
<filter-class>com.test.filter.SystemFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SystemFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>