初学Servlet时主要注意的细节问题
Error 1
- 现象
- 首先呈现HTTP Status 500 – Internal Server Error错误:java.lang.ClassNotFoundException: ResponseDemo1
- 刷新页面后一直出现HTTP Status 404 – Not Found:The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
- 原因
- 在web.xml配置中servlet-class属性忘记加上类的包名,在输入URL之后找不到需要的servlet
- servlet-class必须是类的完整名,如com.mio4.web.ResponseDemo1
Error 2
- 在使用IDEA开发JavaWeb项目时,有时会自动生成@WebServlet(name = “XXX”)注解,如果没有看到这个注解直接在web.xml中配置servlet,可能会发生404错误
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true"
>
- metadata-complete属性表示web.xml部署描述文件是否是完整的,如果true则容器在部署时只依赖web.xml文件而忽视所有注解
- 在web.xml头部中加入metadata-complete="true"将会忽视@WebServlet注解
Error 3
- 在表单提交之后,跳转到web/html/login.html时产生404错误
- 原因:可能是html是保留词,将文件夹名称改为login,再次跳转到web/login/login.html成功
Error 4 (localhost拒绝了我们的访问请求)
- form action为http://localhost/BasicDemo5/login
- 原因:没有添加端口号
- 正确的跳转方式:http://localhost:8080/BasicDemo5/login