Calling a servlet from a form but the servlet is never invoked
I am trying to call a Servlet called AddClass.java using the following line in a jsp
<form method="post" name="addClass" action="AddClass.do">
The class is in package web and the filename is AddClass.java
A similar servlet named SeedSearch is called (and everything works fine) from this piece of code in another jsp.
<form method="post" name="seedSearch" action="SeedSearch">
My web.xml looks like this
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>sempedia</display-name>
<servlet>
<servlet-name>SeedSearch</servlet-name>
<servlet-class>web.SeedSearch</servlet-class>
</servlet>
开发者_运维百科<servlet>
<servlet-name>AddClass</servlet-name>
<servlet-class>web.AddClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SeedSearch</servlet-name>
<url-pattern>/SeedSearch</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AddClass</servlet-name>
<url-pattern>/AddClass.do</url-pattern>
</servlet-mapping>
<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>
Please help.
The web.xml
looks fine, so the problem lies somewhere else.
- URL is plain wrong. Relative URL's must be extracted from its absolute form based on the current request URL or -if any- the HTML
<base>
tag. Take care: the leading slash takes you to the domain root! This is not necessarily the correct solution as others here seem to think, especially not if webapp has a context path. - Package/classname in
web.xml
doesn't match actual Servlet class (casesensitive!) - Servlet class isn't in the classpath of the running webproject. At least, not at the expected location.
- Servlet class failed to load/initialize. Read appserver logs for exception/error details.
- There's actually more in
web.xml
, e.g. another servlet which eats*.do
and got precedence. - You aren't running the webproject with the particular
web.xml
you think you are. Doubleverify, rebuild, redeploy and restart.
There's no leading '/' in your call to SeedSearch.
Perhaps you are mapping "*.do" in somewhere else (struts may be?) and your servlet is never called.
enter image description here
2 servlets: KK is default or normal way; LL is in a custom package. inside web.xml, LL must have fully qualified name not only for class, but also for servlet name.
enter image description here
calling each servlet from html is the same for both: or no need of any qualification
精彩评论