I have created Sample.java servlet, it is in src folder.
and created HTML page in pages directory i.e, pages/First.html
Now I n开发者_开发百科eed to provide in servlet mapping as pages/Sample that I am not getting why pages directory name should mention in servlet url mapping.
As it is in root folder.
You should never put any class in the root package.
Once you have put your Sample class in a package (example: com.foo.andy.sample
), you need to declare the servet in the web.xml of your web application, and declare one (at least) mapping for this servlet.
You might follow this tutorial to know how to do it.
You need these lines in the web.xml:
<servlet>
<servlet-name>SampleServlet</servlet-name>
<servlet-class>com.foo.andy.sample.Sample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SampleServlet</servlet-name>
<url-pattern>/pages/Sample</url-pattern>
</servlet-mapping>
And your servlet will be accessible at .../yourWebApp/pages/Sample
We need servlet mapping to ensure that which servlet is going invoked at which type of url request . To do so you need to write web.xml
file.
lets assume your class located at com.example
package.
<servlet>
<servlet-name>SampleServlet</servlet-name>
<servlet-class>com.example.Sample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SampleServlet</servlet-name>
<url-pattern>/Sample</url-pattern>
</servlet-mapping>
when you complete this code put the url (/Sample) at your <form action="/Sample">
in HTML page.
make sure you should not put class in root directory.
精彩评论