Eclipse建立web项目通过Tomcat运行过程中出现的一些问题

版权声明:未经博主允许,请勿转载原创,谢谢! https://blog.csdn.net/mystonelxj/article/details/85120859

使用Eclipse建立jsp web工程时,需要注意几个方面:

Tomcat启动

首先注意服务是否启动,如果服务配置的是Tomcat,要确认在Server对话框中启动Tomcat是否成功。如果不成功,则需要再进一步核查Tomcat是否可通过自身的启动文件 Startup.bat 启动成功。如果Tomcat自身能够启动成功而Eclipse无法启动Server,则是Eclipse配置出了问题。

Tomcat类库加载

其次,需要注意在web工程中是否加载了Tomcat相关类库。如果未在对应工程中加载,则会出现“The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path” 错误

对应此类问题,解决方法为如下:

1,在Package Explorer 对应工程文件中点击右键,选择 Build Path- Configure Build Path菜单,打开Property对话框

2,在配置对话框中,选择Library tab页面,点击“Add Library” 按钮,打开“Add Library”对话框

3,在“Add Library”对话框中选择 “Server Runtime” 点击 “Next” 按钮,选择对应的“Tomcat v7.0 Server” runtime,点击“Finish” 按钮完成 Tomcat 类库的添加。

4,添加完成后,相关错误不存在。

相关文件位置不正确

web工程运行需要两个基本的文件,一个是配置文件,一个是jsp文件。其中配置文件名称为固定,需为web.xml,在Package Explorer对话框中,文件web.xml位置一般位于项目工程中的 WebContent\Web-INF 路径下

jsp文件可自定义,在Package Explorer对话框中,其文件一般位于项目工程中的WebContent 路径下,并且该文件需在web.xml 需设置,默认情况下,web.xml 如下配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>testa</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

如果自定义的jsp文件名称为NewFile.jsp ,则需调整 web.xml,如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>testa</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>NewFile.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

经过如此设置,确保Eclipse开发环境能够识别NewFile.jsp,并且在启动服务后,打开地址http://localhost :8080/testa  (testa 为工程名称)可获得网页正确结果,否则会出现类似404 错误,如下所示

 

猜你喜欢

转载自blog.csdn.net/mystonelxj/article/details/85120859