*
The moose likes Servlets and the fly likes Basic Servlet question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Basic Servlet question" Watch "Basic Servlet question" New topic
Author

Basic Servlet question

Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 573
I have servlet called :TestServlet

It is in the package ackage brokearage.broker.co.servlet.broker;

In my web.xml I have the following

?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<display-name>TestServlet</display-name>
<description>TestServlet</description>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>brokearage.broker.co.servlet.broker.TestServlet</servlet-class>
</servlet>
</web-app>

Its jarred into a war file BrokerWar.war.

two questions how would I call TestServlet from a JSP and directly from a URL

I know this ia a basic question but I have spent all afternoon trying to access my servlet.

Thanks for any help

Tony
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60740
    
  65

You need to set up a servlet mapping in the deployment descriptor (web.xml).

Here's an example:



Assuming that the web app context path is /xyz, the URL to the servelt would be:

http://myserver.com/xyz/someServlet

If the web app is mapped to the root context, omit the /xyz.

P.S. Also, when posting code, be sure to use the UBB code tags to preserve the formatting.
[ July 19, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 573
Thanks for anyone else interested I now have in my web.xml the following:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<display-name>TestServlet</display-name>
<description>TestServlet</description>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>brokearage.broker.co.servlet.broker.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/testServlet</url-pattern>
</servlet-mapping>
</web-app>

And call it with

http://localhost:11001/BrokerWAR/testServlet
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
I presume your servlet is running fine now.

Also, if you are adding another Servlet in you web.xml file, keep your servlet and servlet mapping segregated.

For Example,
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>s1<</servlet-class>
</servlet>
<servlet>
<servlet-name>s2</servlet-name>
<servlet-class>s2<</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/s1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>s2</servlet-name>
<url-pattern>/s2</url-pattern>
</servlet-mapping>


SCJP 1.4 85%
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Basic Servlet question
 
Similar Threads
servlets in Oracle Application server for windows2000
writing web.xml
Tomcat - Form based declarative Security
servlet deployment error in web.xml on WSAD 4.x
Resin 3 Forms Authentication