This question already has answers here:
Change default homepage in root path to servlet with doGet
                                
                                    (2个答案)
                                
                        
                                3年前关闭。
            
                    
我已经尝试过其他答案中提到的先前方法,但http://localhost:8080/InterConnect/仍然显示404错误。
http://localhost:8080/InterConnect/servlet/HomePageServlet页运行平稳。


  web.xml文件:-


<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<!-- Define servlets that are included in the example application -->

<welcome-file-list>
    <welcome-file>HomePageServlet</welcome-file>
</welcome-file-list>


<servlet>
    <servlet-name>SignInServlet</servlet-name>
    <servlet-class>SignInServlet</servlet-class>
</servlet>

<servlet>
    <servlet-name>SignUpServlet</servlet-name>
    <servlet-class>SignUpServlet</servlet-class>
</servlet>

 <servlet>
    <servlet-name>HomePageServlet</servlet-name>
    <servlet-class>HomePageServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>SignInServlet</servlet-name>
    <url-pattern>/servlet/SignInServlet</url-pattern>
</servlet-mapping>

<servlet-mapping>
   <servlet-name>SignUpServlet</servlet-name>
   <url-pattern>/servlet/SignUpServlet</url-pattern>
</servlet-mapping>

<servlet-mapping>
   <servlet-name>HomePageServlet</servlet-name>
   <url-pattern>/servlet/HomePageServlet</url-pattern>
</servlet-mapping>

最佳答案

当浏览器请求http://localhost:8080/InterConnect/时,假定InterConnect是上下文路径,并且假设您已声明<welcome-file>HomePageServlet</welcome-file>,则Servlet容器将查看是否存在/HomePageServlet的servlet映射。

您没有/HomePageServlet的映射,只有/servlet/HomePageServlet的映射,因此Web服务器返回404(未找到)。

尝试<welcome-file>servlet/HomePageServlet</welcome-file>看看是否可行,或将servlet映射更改为/HomePageServlet

10-08 03:03