tags:

views:

106

answers:

3

Hi,

I'm working on an application. I have a servlet (writeDataBase.class) that writes some information in a database. This is working fine. My folder structure looks like: webapps/HelloWord/web-inf/classes. In folder 'classes' is where the file writeDataBase.class is placed. web.xml looks like:

<servlet>
 <servlet-name>HelloWord</servlet-name>
 <servlet-class>writeDataBase.writeDataBase</servlet-class>
</servlet>

<servlet-mapping>
 <servlet-name>HelloWord</servlet-name>
 <url-pattern>/write-data</url-pattern>
</servlet-mapping>

If I want to add a new servlet that will read data from the data base, how should I do it? As a class of the same package? How should I modify the file structure and the web.xml file? Thanks

+1  A: 

Just add servlet mappings to the XML file. As for what package you put the classes in; the package declarations are up to you - but the classes must still be located in the web-inf/classes directory

oxbow_lakes
A: 

If you write a new Servlet class, you will need a new <servlet> and <servlet-mapping> entry in your web.xml file. The <servlet-class> element should contain the fully qualified class name for your new servlet clas, and the <url-pattern> should contain the path that you would like to use in order to call that servlet. The package that you use does not matter.

All of your application classes (the Servlets themselves, as well as any dependencies such as third-party libraries) will need to either live in the /WEB-INF/classes/ directory of your WAR, or in JAR files directly under /WEB-INF/lib.

So let's say you create a second class, some.pkg.MyServlet, your web.xml might look like:

<servlet>
  <servlet-name>HelloWord</servlet-name>
  <servlet-class>writeDataBase.writeDataBase</servlet-class>
</servlet>

<servlet>
  <servlet-name>MyServlet</servlet-name>
  <servlet-class>some.pkg.MyServlet</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>HelloWord</servlet-name>
  <url-pattern>/write-data</url-pattern>
</servlet-mapping>

<servlet-mapping>
  <servlet-name>MyServlet</servlet-name>
  <url-pattern>/my-servlet</url-pattern>
</servlet-mapping>

Note that for both sets of elements, the <servlet-name> element doesn't have any significance except that it ties the servlet definition with the servlet (URL) mapping.

Adam Batkin
A: 

Perhaps:

<servlet>
  <servlet-name>HelloExcel</servlet-name>
  <servlet-class>writeDataBase.readDataBase</servlet-class>
</servlet>
<servlet>
  <servlet-name>HelloWord</servlet-name>
  <servlet-class>writeDataBase.writeDataBase</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>HelloExcel</servlet-name>
  <url-pattern>/read-data</url-pattern>
</servlet-mapping>
<servlet-mapping>
  <servlet-name>HelloWord</servlet-name>
  <url-pattern>/write-data</url-pattern>
</servlet-mapping>
kd304