java servlet 返回404,Servlet返回“HTTP状态404所请求的资源(/ servlet)不可用”

I have an HTML form in a JSP file in my WebContent/jsps folder. I have a servlet class servlet.java in my default package in src folder. In my web.xml it is mapped as /servlet.

I have tried several URLs in action attribute of the HTML form:

But none of those work. They all keep returning a HTTP 404 error like below in Tomcat 6/7/8:

HTTP Status 404 — /servlet

Description: The requested resource (/servlet) is not available.

Or as below in Tomcat 8.5/9:

HTTP Status 404 — Not Found

Message: /servlet

Description: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists

Why is it not working?

解决方案

Put servlet class in a package

First of all, put the servlet class in a Java package. You should always put publicly reuseable Java classes in a package, otherwise they are invisible to classes which are in a package, such as the server itself. This way you eliminiate potential environment-specific problems. Packageless servlets work only in specific Tomcat+JDK combinations and this should never be relied upon.

In case of a "plain" IDE project, the class needs to be placed in its package structure inside "Java Resources" folder and thus not "WebContent", this is for web files such as JSP. Below is an example of the folder structure of a default Eclipse Dynamic Web Project as seen in Navigator view:

EclipseProjectName

|-- src

| `-- com

| `-- example

| `-- YourServlet.java

|-- WebContent

| |-- WEB-INF

| | `-- web.xml

| `-- jsps

| `-- page.jsp

:

In case of a Maven project, the class needs to be placed in its package structure inside main/java and thus not e.g. main/resources, this is for non-class files. Below is an example of the folder structure of a default Maven webapp project as seen in Eclipse's Navigator view:

MavenProjectName

|-- src

| `-- main

| |-- java

| | `-- com

| | `-- example

| | `-- YourServlet.java

| |-- resources

| `-- webapp

| |-- WEB-INF

| | `-- web.xml

| `-- jsps

| `-- page.jsp

:

Note that the /jsps subfolder is not strictly necessary. You can even do without it and put the JSP file directly in webcontent/webapp root, but I'm just taking over this from your question.

Set servlet URL in url-pattern

The servlet URL is specified as the "URL pattern" of the servlet mapping. It's absolutely not per definition the classname/filename of the servlet class. The URL pattern is to be specified as value of @WebServlet annotation.

package com.example; // Use a package!

@WebServlet("/servlet") // This is the URL of the servlet.

public class YourServlet extends HttpServlet { // Must be public and extend HttpServlet.

// ...

}

In case you want to support path parameters like /servlet/foo/bar, then use an URL pattern of /servlet/* instead. See also Servlet and path parameters like /xyz/{value}/test, how to map in web.xml?

@WebServlet works only on Servlet 3.0 or newer

In order to use @WebServlet, you only need to make sure that your web.xml file, if any (it's optional since Servlet 3.0), is declared conform Servlet 3.0+ version and thus not conform e.g. 2.5 version or lower. Below is a Servlet 3.1 compatible one (which matches Tomcat 8+, WildFly 8+, GlassFish 4+, etc).

xmlns="http://xmlns.jcp.org/xml/ns/javaee"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

id="WebApp_ID" version="3.1"

>

Or, in case you're not on Servlet 3.0+ yet (not Tomcat 7 or newer, but Tomcat 6 or older), then remove the @WebServlet annotation.

package com.example;

public class YourServlet extends HttpServlet {

// ...

}

And register the servlet instead in web.xml like this:

yourServlet

com.example.YourServlet

yourServlet

/servlet

Note thus that you should not use both ways. Use either annotation based configuarion or XML based configuration. When you have both, then XML based configuration will override annotation based configuration.

Verifying the build/deployment

In case you're using a build tool such as Eclipse and/or Maven, then you need to make absolutely sure that the compiled servlet class file resides in its package structure in /WEB-INF/classes folder of the produced WAR file. In case of package com.example; public class YourServlet, it must be located in /WEB-INF/classes/com/example/YourServlet.class. Otherwise you will face in case of @WebServlet also a 404 error, or in case of a HTTP 500 error like below:

HTTP Status 500

Error instantiating servlet class com.example.YourServlet

And find in the server log a java.lang.ClassNotFoundException: com.example.YourServlet, followed by a java.lang.NoClassDefFoundError: com.example.YourServlet, in turn followed by javax.servlet.ServletException: Error instantiating servlet class com.example.YourServlet.

An easy way to verify if the servlet is correctly compiled and placed in classpath is to let the build tool produce a WAR file (e.g. rightclick project, Export > WAR file in Eclipse) and then inspect its contents with a ZIP tool. If the servlet class is missing in /WEB-INF/classes, then the project is badly configured or some IDE/project configuration defaults have been mistakenly reverted (e.g. Project > Build Automatically has been disabled in Eclipse). In case you have no clue, best is to restart from scratch and do not touch any IDE/project configuration defaults.

Testing the servlet individually

Provided that the server runs on localhost:8080, and that the WAR is successfully deployed on a context path of /contextname (which defaults to the IDE project name, case sensitive!), and the servlet hasn't failed its initialization (read server logs for any deploy/servlet success/fail messages and the actual context path and servlet mapping), then a servlet with URL pattern of /servlet is available at http://localhost:8080/contextname/servlet.

You can just enter it straight in browser's address bar to test it invidivually. If its doGet() is properly overriden and implemented, then you will see its output in browser. Or if you don't have any doGet() or if it incorrectly calls super.doGet(), then a "HTTP 405: HTTP method GET is not supported by this URL" error will be shown (which is still better than a 404 as a 405 is evidence that the servlet itself is actually found).

Overriding service() is a bad practice, unless you're reinventing a MVC framework — which is very unlikely if you're just starting out with servlets and are clueless as to the problem described in the current question ;) See also Design Patterns web based applications.

Regardless, if the servlet already returns 404 when tested invidivually, then it's entirely pointless to try with a HTML form instead. Logically, it's therefore also entirely pointless to include any HTML form in questions about 404 errors from a servlet.

Referencing the servlet URL from HTML

Once you've verified that the servlet works fine when invoked individually, then you can advance to HTML. As to your concrete problem with the HTML form, the

value needs to be a valid URL. The same applies to . You need to understand how absolute/relative URLs work. You know, an URL is a web address as you can enter/see in the webbrowser's address bar. If you're specifying a relative URL as form action, i.e. without the http:// scheme, then it becomes relative to the current URL as you see in your webbrowser's address bar. It's thus absolutely not relative to the JSP/HTML file location in server's WAR folder structure as many starters seem to think.

So, assuming that the JSP page with the HTML form is opened by http://localhost:8080/contextname/jsps/page.jsp, and you need to submit to a servlet located in http://localhost:8080/contextname/servlet, here are several cases (note that you can safely substitute

with

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目录 前言 ............................... 3 其他资料 ............................................................................................................................................................3 谁应该读此规范 ................................................................................................................................................3 API 规范 ............................................................................................................................................................3 其他的 Java 平台规范 .......................................................................................................................................3 其他重要参考资料 ............................................................................................................................................4 提供反馈 ............................................................................................................................................................4 专家组成员 ........................................................................................................................................................5 答谢 ....................................................................................................................................................................5 目录 ............................... 6 概览 .............................. 13 1.1 什么是 Servlet? ..................................................................................................................................13 1.2 什么是 Servlet 容器?...............................................................................................................................13 1.3 例子 ...........................................................................................................................................................13 1.4 Servlet 与其他技术的比较 ........................................................................................................................14 1.5 与 Java 平台企业版的关系 ......................................................................................................................14 1.6 与 Java Servlet 规范 2.5 版本间的兼容性 ..............................................................................................14 1.6.1 监听器(Listener)顺序 ...............................................................................................................14 1.6.2 注解处理 .........................................................................................................................................14 Servlet 接口 ...................... 15 2.1 请求处理方法 ...........................................................................................................................................15 2.1.1 基于 Http 规范的请求处理方法 ...................................................................................................15 2.1.2 附加的方法 ....................................................................................................................................15 2.1.3 有条件 GET 支持 ..........................................................................................................................16 2.2 实例数量 ...................................................................................................................................................16 2.2.1 关于 Single Thread Model..............................................................................................................16 2.3 Servlet 生命周期 .......................................................................................................................................16 2.3.1 加载和实例化 ................................................................................................................................16 2.3.2 初始化 ............................................................................................................................................16 2.3.3 请求处理 ........................................................................................................................................17 2.3.4 终止服务(End of Service) .........................................................................................................27 Request ........................... 29 3.1 HTTP 协议参数 .........................................................................................................................................29 3.1.1 当参数可用时 ................................................................................................................................29 3.2 文件上传 ....................................................................................................................................................30 3.3 属性 ...........................................................................................................................................................30 3.4 头 ...............................................................................................................................................................30 3.5 请求路径元素 ...........................................................................................................................................31 3.6 路径转换方法 ...........................................................................................................................................32 7 3.7 非阻塞 IO...................................................................................................................................................32 3.8 Cookies........................................................................................................................................................33 3.9 SSL 属性 ....................................................................................................................................................33 3.10 国际化 .....................................................................................................................................................34 3.11 请求数据编码 .........................................................................................................................................34 3.12 Request 对象的生命周期 ........................................................................................................................34 Servlet Context ................... 35 4.1 ServletContext 接口介绍 ...........................................................................................................................35 4.2 ServletContext 接口作用范围 ...................................................................................................................35 4.3 初始化参数 ...............................................................................................................................................35 4.4 配置方法 ...................................................................................................................................................35 4.4.1 编程式添加和配置 Servlet.............................................................................................................36 4.4.2 编程式添加和配置 Filter ...............................................................................................................36 4.4.3 编程式添加和配置 Listener............................................................................................................37 4.5 上下文属性 ...............................................................................................................................................39 4.6 资源 ...........................................................................................................................................................39 4.7 多主机和 Servlet 上下文 ..........................................................................................................................40 4.8 Reload 注意事项 .......................................................................................................................................40 4.8.1 临时工作目录 ................................................................................................................................40 Response .......................... 41 5.1 缓冲 ...........................................................................................................................................................41 5.2 头信息 .......................................................................................................................................................41 5.3 非阻塞 IO...................................................................................................................................................42 5.4 简便方法 ...................................................................................................................................................43 5.5 国际化 .......................................................................................................................................................43 5.6 结束响应对象 ...........................................................................................................................................44 5.7 Response 的生命周期 ................................................................................................................................44 过滤器 ............................ 45 6.1 什么是过滤器 ...........................................................................................................................................45 6.1.1 过滤器组件示例 ............................................................................................................................45 6.2 主要概念 ...................................................................................................................................................46 6.2.1 过滤器生命周期 ............................................................................................................................46 6.2.2 包装请求和响应 ............................................................................................................................47 6.2.3 过滤器环境 ....................................................................................................................................47 6.2.4 在 Web 应用中配置过滤器 ...........................................................................................................47 6.2.5 过滤器和 RequestDispatcher..........................................................................................................49 会话 .............................. 51 7.1 会话跟踪机制 ...........................................................................................................................................51 7.1.1 Cookies.............................................................................................................................................51 7.1.2 SSL 会话 .........................................................................................................................................51 7.1.3 URL 重写 .......................................................................................................................................51 7.1.4 会话完整性 ....................................................................................................................................52 7.2 创建会话 ...................................................................................................................................................52 8 7.3 会话范围 ...................................................................................................................................................52 7.4 绑定 Session 属性 .....................................................................................................................................52 7.5 会话超时 ...................................................................................................................................................53 7.6 最后访问时间 ...........................................................................................................................................53 7.7 重要的会话语义 .......................................................................................................................................53 7.7.1 多线程问题 ....................................................................................................................................53 7.7.2 分布式环境 ....................................................................................................................................53 7.7.3 客户端语义 ....................................................................................................................................54 注解和可插拔性 .................... 55 8.1 注解和可插拔性 .......................................................................................................................................55 8.1.1 @WebServlet....................................................................................................................................55 8.1.2 @WebFilter ......................................................................................................................................56 8.1.3 @WebInitParam ...............................................................................................................................56 8.1.4 @WebListener..................................................................................................................................56 8.1.5 @MultipartConfig............................................................................................................................57 8.1.6 其他注解/惯例 ...............................................................................................................................57 8.2 可插拔性 ...................................................................................................................................................57 8.2.1 web.xml 模块 ..................................................................................................................................57 8.2.2 web.xml 和 web-fragment.xml 顺序 ..............................................................................................58 8.2.3 装配 web.xml、web-fragment.xml 描述符和注解 .......................................................................63 8.2.4 共享库 / 运行时可插拔性 ...........................................................................................................72 8.3 JSP 容器可插拔性 .....................................................................................................................................74 8.4 处理注解和 fragment ................................................................................................................................74 分派请求 .......................... 75 9.1 获得一个 RequestDispatcher.....................................................................................................................75 9.1.1 请求调度器路径中的查询字符串 ................................................................................................75 9.2 请求调度器的使用 ...................................................................................................................................75 9.3 Include 方法 ...............................................................................................................................................76 9.3.1 内置请求参数 ................................................................................................................................76 9.4 Forward 方法 .............................................................................................................................................76 9.4.1 查询字符串 ....................................................................................................................................77 9.4.2 转发的请求参数 ............................................................................................................................77 9.5 错误处理 ...................................................................................................................................................77 9.6 获得一个异步上下文对象 .......................................................................................................................77 9.7 Dispatch 方法 .............................................................................................................................................78 9.7.1 查询字符串 ....................................................................................................................................78 9.7.2 调度请求参数 ................................................................................................................................78 Web 应用 .......................... 80 10.1 Web 服务器中的 Web 应用程序 .............................................................................................................80 10.2 与 ServletContext 的关系 .......................................................................................................................80 10.3 Web 应用的要素 ......................................................................................................................................80 10.4 部署层次结构 .........................................................................................................................................80 10.5 目录结构 .................................................................................................................................................80 9 10.5.1 应用程序目录结构示例 ......................................................................................................................81 10.6 Web 应用程序归档文件 ..........................................................................................................................82 10.7 Web 应用程序部署描述文件 ..................................................................................................................82 10.7.1 扩展的依赖关系 ..........................................................................................................................82 10.7.2 Web 应用程序类加载器 ...............................................................................................................83 10.8 更新 Web 应用 ........................................................................................................................................83 10.9 错误处理 .................................................................................................................................................83 10.9.1 请求属性 ......................................................................................................................................83 10.9.2 错误页面 ......................................................................................................................................84 10.9.3 错误过滤器 ..................................................................................................................................85 10.10 欢迎文件 ...............................................................................................................................................85 10.11 Web 应用程序环境 ................................................................................................................................86 10.12 Web 应用程序部署 ................................................................................................................................86 10.13 包含 web.xml 部署描述文件 ...............................................................................................................86 应用生命周期事件 .................. 87 11.1 介绍 .........................................................................................................................................................87 11.2 事件监听器 .............................................................................................................................................87 11.2.1 事件类型和监听器接口 ..............................................................................................................87 11.2.2 监听器使用的一个例子 ..............................................................................................................88 11.3 监听器类配置 .........................................................................................................................................88 11.3.1 提供监听器类 ..............................................................................................................................88 11.3.2 部署声明 ......................................................................................................................................88 11.3.3 监听器注册 ..................................................................................................................................89 11.3.4 关闭时通知 ..................................................................................................................................89 11.4 部署描述符示例 .....................................................................................................................................89 11.5 监听器实例和线程 .................................................................................................................................90 11.6 监听器异常 .............................................................................................................................................90 11.7 分布式容器 .............................................................................................................................................90 11.8 会话事件 .................................................................................................................................................90 映射请求Servlet................. 91 12.1 使用 URL 路径 .......................................................................................................................................91 12.2 映射规范 .................................................................................................................................................91 12.2.1 隐式映射 ......................................................................................................................................91 12.2.2 示例映射集合 ..............................................................................................................................92 安全 .............................. 93 13.1 介绍 .........................................................................................................................................................93 13.2 声明式安全 .............................................................................................................................................93 13.3 编程式安全 .............................................................................................................................................93 13.4 编程式安全策略配置 .............................................................................................................................94 13.4.1 @ServletSecurity 注解 ..................................................................................................................95 13.4.2 ServletRegistration.Dynamic 的 setServletSecurity.....................................................................101 13.5 角色 .......................................................................................................................................................101 13.6 认证 .......................................................................................................................................................102 10 13.6.1 HTTP 基本认证 ..........................................................................................................................102 13.6.2 HTTP 摘要认证 ..........................................................................................................................102 13.6.3 基于表单的认证 .........................................................................................................................102 13.6.4 HTTPS 客户端认证 ....................................................................................................................104 13.6.5 其他容器认证机制 ....................................................................................................................104 13.7 服务器跟踪认证信息 ...........................................................................................................................104 13.8 指定安全约束 .......................................................................................................................................104 13.8.1 组合约束 ....................................................................................................................................105 13.8.2 示例 ............................................................................................................................................105 13.8.3 处理请求 ....................................................................................................................................107 13.8.4 未覆盖的 HTTP 协议方法 ........................................................................................................107 13.9 默认策略 ...............................................................................................................................................109 13.10 登录和退出 ......................................................................................................................................... 110 部署描述文件 ..................... 111 14.1 部署描述文件元素 ............................................................................................................................... 111 14.2 部署描述符处理规则 ........................................................................................................................... 111 14.3 部署描述文件 ....................................................................................................................................... 112 14.4 部署描述文件图解 ............................................................................................................................... 112 1、web-app 元素 ................................................................................................................................... 112 2、description 元素 ............................................................................................................................... 113 3、display-name 元素 ........................................................................................................................... 113 4、icon 元素 .......................................................................................................................................... 114 5、distributable 元素 ............................................................................................................................. 114 6、context-param 元素 .......................................................................................................................... 114 7、filter 元素 ......................................................................................................................................... 114 8、filter-mapping 元素 .......................................................................................................................... 115 9、listener 元素 ..................................................................................................................................... 116 10、servlet 元素 .................................................................................................................................... 116 11、servlet-mapping 元素 ..................................................................................................................... 117 12、session-config 元素 ........................................................................................................................ 118 13、mime-mapping 元素 ...................................................................................................................... 118 14、welcome-file-list 元素 .................................................................................................................... 119 15、error-page 元素 .............................................................................................................................. 119 16、jsp-config 元素 ............................................................................................................................... 119 17、security-constraint 元素 .................................................................................................................120 18、login-config 元素 ...........................................................................................................................121 19、security-role 元素 ...........................................................................................................................121 20、env-entry 元素 ................................................................................................................................122 21、ejb-ref 元素 ....................................................................................................................................123 22、ejb-local-ref 元素 ...........................................................................................................................124 23、service-ref 元素 ..............................................................................................................................125 24、resource-ref 元素 ............................................................................................................................126 25、resource-env-ref 元素 ....................................................................................................................127 26、message-destination-ref 元素 .........................................................................................................128 27、message-destination 元素 ..............................................................................................................129 11 28、locale-encoding-mapping-list 元素 ................................................................................................130 14.5 实例 .......................................................................................................................................................131 14.5.1 一个简单的例子 ........................................................................................................................131 14.5.2 安全示例 ....................................................................................................................................132 15.与其它规范有关的要求 .......... 135 15.1 会话 .......................................................................................................................................................135 15.2 Web 应用程序 ........................................................................................................................................135 15.2.1 Web 应用程序类加载器 .............................................................................................................135 15.2.2 Web 应用程序环境 .....................................................................................................................135 15.2.3 Web 模块上下文根 URL 的 JNDI 名称 ....................................................................................136 15.3 安全性 ...................................................................................................................................................136 15.3.1 EJB™调用传播的安全标识 ......................................................................................................137 15.3.2 容器授权的要求 ........................................................................................................................137 15.3.3 容器认证的要求 ........................................................................................................................137 15.4 部署 .......................................................................................................................................................137 15.4.1 部署描述符元素 ........................................................................................................................137 15.4.2 打包和 JAX-WS 组件部署 .......................................................................................................138 15.4.3 处理部署描述符的规则 ............................................................................................................138 15.5 注解和资源注入 ...................................................................................................................................139 15.5.1 @DeclareRoles.............................................................................................................................140 15.5.2 @EJB 注解 ................................................................................................................................140 15.5.3 @EJBs 注解 ...............................................................................................................................141 15.5.4 @Resource 注解 ........................................................................................................................141 15.5.5 @PersistenceContext 注解 .........................................................................................................142 15.5.6 @PersistenceContexts 注解 .......................................................................................................142 15.5.7 @PersistenceUnit 注解 ..............................................................................................................142 15.5.8 @PersistenceUnits Annotation.....................................................................................................142 15.5.9 @PostConstruct 注解 .................................................................................................................142 15.5.10 @PreDestroy 注解 ...................................................................................................................143 15.5.11 @Resources 注解 .....................................................................................................................143 15.5.12 @RunAs 注解 ..........................................................................................................................144 15.5.13 @WebServiceRef 注解 ............................................................................................................144 15.5.14 @WebServiceRefs 注解 ...........................................................................................................145 15.5.15 JavaEE 要求的上下文和依赖注入 ..........................................................................................145 变更历史 ......................... 146 A.1 自 Servlet3.0 以后的变更 ......................................................................................................................146 A.2 自 Servlet 3.0 Proposed Final Draft 以后的变更 ..................................................................................147 A.3 自 Servlet 3.0 Public Review 以后的变更 ............................................................................................147 A.4 自 Servlet 3.0 EDR 以后的变更 ............................................................................................................148 A.5 自 Servlet 2.5 MR6 以后的变更 ............................................................................................................148 A.6 自 Servlet 2.5 MR 5 以后的变更 ...........................................................................................................148 A.6.1 明确 SRV 8.4“Forward 方法” ...................................................................................................148 A.6.2 更新部署描述符“http-method values allowed” ........................................................................148 A.6.3 明确 SRV 7.7.1 “多线程问题” ...................................................................................................149 12 A.7 自 Servlet 2.5 MR 2 以后的变更 ...........................................................................................................149 A.7.1 更新 JavaEE 容器注解的要求 ...................................................................................................149 A.7.2 更新 Java 企业版的要求 ............................................................................................................149 A.7.3 明确 HttpServletRequest.getRequestURL() ................................................................................149 A.7.4 从 HttpSession.getId()移除 IllegalStateException.......................................................................149 A.7.5 ServletContext.getContextPath()...................................................................................................150 A.7.6 web 应用中的 web.xml 的要求 ...................................................................................................150 A.8 自 Servlet 2.4 以后的变更 .....................................................................................................................150 A.8.1 Session 解释 .................................................................................................................................150 A.8.2 过滤所有分派 .............................................................................................................................151 A.8.3 多次出现的 Servlet 映射 ............................................................................................................151 A.8.4 多次出现 Filter 映射 ..................................................................................................................151 A.8.5 授权约束支持其他的 HTTP 方法 .............................................................................................152 A.8.6 最低 J2SE 要求 ...........................................................................................................................153 A.8.7 注解和资源注入 .........................................................................................................................153 A.8.8 移除了 SRV.9.9(“错误处理”)要求 .......................................................................................154 A.8.9 明确 HttpServletRequest.isRequestedSessionIdValid()...............................................................154 A.8.10 明确 SRV.5.5 (“结束响应对象”) ........................................................................................154 A.8.11 明确 ServletRequest.setCharacterEncoding()............................................................................154 A.8.12 Java 企业版要求 ........................................................................................................................154 A.8.13 新增了 Servlet 2.4 MR 更新的变更历史 ................................................................................154 A.8.14 明确同步访问 Session 对象 .....................................................................................................155 A.9 自 Servlet 2.3 以后的变更 .....................................................................................................................155

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值