0

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 need 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.

Andy
  • 33
  • 1
  • 5

2 Answers2

2

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

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

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.

Mihir Bera
  • 11
  • 2