html - Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available" -
i have html form in jsp file in webcontent/jsps
folder. have servlet class servlet.java
in default package in src
folder. in web.xml
mapped /servlet
.
i have tried several urls in action
attribute of html form:
<form action="/servlet">
<form action="/servlet.java">
<form action="/src/servlet.java">
<form action="../servlet.java">
but none of work. keep returning http 404 error below in tomcat 6/7/8:
http status 404 — /servlet
description: requested resource (/servlet) not available.
or below in tomcat 8.5/9:
http status 404 — not found
message: /servlet
description: origin server did not find current representation target resource or not willing disclose 1 exists
why not working?
put servlet class in package
first of all, put servlet class in java package
. should always put publicly reuseable java classes in package, otherwise invisible classes in package, such server itself. way eliminiate potential environment-specific problems. packageless servlets work in specific tomcat+jdk combinations , should never relied upon.
in case of "plain" ide project, class needs placed in package structure inside "java resources" folder , not "webcontent", web files such jsp. below example of folder structure of default eclipse dynamic web project seen in navigator view:
eclipseprojectname |-- src | `-- com | `-- example | `-- yourservlet.java |-- webcontent | |-- web-inf | | `-- web.xml | `-- jsps | `-- page.jsp :
in case of maven project, class needs placed in package structure inside main/java
and not e.g. main/resources
, non-class files. below example of folder structure of default maven webapp project seen in eclipse's navigator view:
mavenprojectname |-- src | `-- main | |-- java | | `-- com | | `-- example | | `-- yourservlet.java | |-- resources | `-- webapp | |-- web-inf | | `-- web.xml | `-- jsps | `-- page.jsp :
note /jsps
subfolder not strictly necessary. can without , put jsp file directly in webcontent/webapp root, i'm taking on question.
set servlet url in url-pattern
the servlet url specified "url pattern" of servlet mapping. it's absolutely not per definition classname/filename of servlet class. url pattern specified value of @webservlet
annotation.
package com.example; // use package! @webservlet("/servlet") // url of servlet. public class yourservlet extends httpservlet { // must public , extend httpservlet. // ... }
in case want support path parameters /servlet/foo/bar
, use url pattern of /servlet/*
instead. see servlet , path parameters /xyz/{value}/test, how map in web.xml?
@webservlet
works on servlet 3.0 or newer
in order use @webservlet
, need make sure web.xml
file, if (it's optional since servlet 3.0), declared conform servlet 3.0+ version and not conform e.g. 2.5 version or lower. below servlet 3.1 compatible 1 (which matches tomcat 8+, wildfly 8+, glassfish 4+, etc).
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 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" > <!-- config here. --> </web-app>
or, in case you're not on servlet 3.0+ yet (not tomcat 7 or newer, tomcat 6 or older), remove @webservlet
annotation.
package com.example; public class yourservlet extends httpservlet { // ... }
and register servlet instead in web.xml
this:
<servlet> <servlet-name>yourservlet</servlet-name> <servlet-class>com.example.yourservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>yourservlet</servlet-name> <url-pattern>/servlet</url-pattern> <!-- url of servlet. --> </servlet-mapping>
note should not use both ways. use either annotation based configuarion or xml based configuration.
verifying build/deployment
in case you're using build tool such eclipse and/or maven, need make absolutely sure compiled servlet class file resides in package structure in /web-inf/classes
folder of produced war file. in case of package com.example; public class yourservlet
, must located in /web-inf/classes/com/example/yourservlet.class
. otherwise face in case of @webservlet
404 error, or in case of <servlet>
http 500 error below:
http status 500
error instantiating servlet class com.example.yourservlet
and find in server log java.lang.classnotfoundexception: com.example.yourservlet
, followed java.lang.noclassdeffounderror: com.example.yourservlet
, in turn followed javax.servlet.servletexception: error instantiating servlet class com.example.yourservlet
.
an easy way verify if servlet correctly compiled , placed in classpath let build tool produce war file (e.g. rightclick project, export > war file in eclipse) , inspect contents zip tool. if servlet class missing in /web-inf/classes
, project badly configured or ide/project configuration defaults have been mistakenly reverted (e.g. project > build automatically has been disabled in eclipse). in case have no clue, best restart scratch , not touch ide/project configuration defaults.
testing servlet individually
provided server runs on localhost:8080
, , war deployed on context path of /contextname
(which defaults ide project name, case sensitive!), , servlet hasn't failed initialization (read server logs deploy/servlet success/fail messages , actual context path , servlet mapping), servlet url pattern of /servlet
available @ http://localhost:8080/contextname/servlet
.
you can enter straight in browser's address bar test invidivually. if doget()
overriden , implemented, see output in browser. or if don't have doget()
or if incorrectly calls super.doget()
, "http 405: http method not supported url" error shown (which still better 404 405 evidence servlet found).
overriding service()
bad practice, unless you're reinventing mvc framework — unlikely if you're starting out servlets , clueless problem described in current question ;) see design patterns web based applications.
regardless, if servlet returns 404 when tested invidivually, it's entirely pointless try html form instead. logically, it's therefore entirely pointless include html form in questions 404 errors servlet.
referencing servlet url html
once you've verified servlet works fine when invoked individually, can advance html. concrete problem html form, <form action>
value needs valid url. same applies <a href>
. need understand how absolute/relative urls work. know, url web address can enter/see in webbrowser's address bar. if you're specifying relative url form action, i.e. without http://
scheme, becomes relative current url see in webbrowser's address bar. it's absolutely not relative jsp/html file location in server's war folder structure many starters seem think.
so, assuming jsp page html form opened http://localhost:8080/contextname/jsps/page.jsp
, , need submit servlet located in http://localhost:8080/contextname/servlet
, here several cases (note can safely substitute <form action>
<a href>
here):
form action submits url leading slash.
<form action="/servlet">
the leading slash
/
makes url relative domain, form submit tohttp://localhost:8080/servlet
but result in 404 it's in wrong context.
form action submits url without leading slash.
<form action="servlet">
this makes url relative current folder of current url, form submit to
http://localhost:8080/contextname/jsps/servlet
but result in 404 it's in wrong folder.
form action submits url goes 1 folder up.
<form action="../servlet">
this go 1 folder (exactly in local disk file system paths!), form submit to
http://localhost:8080/contextname/servlet
this 1 must work!
the canonical approach, however, make url domain-relative don't need fix urls once again when happen move jsp files around folder.
<form action="${pagecontext.request.contextpath}/servlet">
this generate
<form action="/contextname/servlet">
which submit right url.
use straight quotes in html
you need make absolutely sure you're using straight quotes in html attributes action="..."
or action='...'
, not curly quotes action=”...”
or action=’...’
. curly quotes not supported in html , become part of value.
see also:
- our servlets wiki page - contains hello world examples
- how call servlet class html form
- doget , dopost in servlets
- how pass current item java method clicking hyperlink or button in jsp page?
other cases of http status 404 error:
- http status 404 - servlet [servletname] not available
- http status 404 - requested resource (/projectname/) not available
- http status 404 - requested resource (/) not available
- jsp in /web-inf returns "http status 404 requested resource not available"
- referencing resource placed in web-inf folder in jsp file returns http 404 on resource
- browser can't access/find relative resources css, images , links when calling servlet forwards jsp
Comments
Post a Comment