0

Can anyone tell me please if what i am doing is right ?

I have this web.xml file with my servlets declared inside, i wanted to creat another servlet that access [reach] the same webservice

it doesn't work and i don't know what i am missing can you help please ? Here is my servlets and code:

<!-- old servlet-->

<servlet>
    <servlet-name>api</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-config/api-r-servlet.xml</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>api</servlet-name>
    <url-pattern>/sitesApi/*</url-pattern>
</servlet-mapping>

<!-- my secod servmet V2  -->
<servlet>
    <servlet-name>webservice</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-config/api-web-servlet.xml</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>webservice</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

So for my other XML file, api-web-servlet it's quite the same :

<?xml version="1.0" encoding="UTF-8"?>
<beans   xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
    > 
<context:annotation-config/>
<context:component-scan base-package="com.me.project.rest.api" />
<mvc:annotation-driven />      
</beans>

for my URL test i use: http://localhost:8080/sitesApi/v1/mysite?id=1&fields=site.name and it works but when i try with the other servlet it doesnt : http://localhost:8080/api/v1/mysite?id=1&fields=site.name

******* Edition*****

    @RequestMapping(value = "/v1/mySite", method = RequestMethod.GET)
public @ResponseBody
Object  SitesV1( @RequestParam(value = "ajouterstatus", required = false) Integer ajouterstatus,
        @RequestParam(value = "monadresse", required = false) String monadresseHttpServletRequest request, HttpServletResponse responseHttp{ 

 try {
        appname = request.getHeaders( "Name" ).nextElement().toString();
    } catch ( Exception e ) {
        return new Result( "", ": AppName is Empty" );
    }

So exactly the same thing with the other method

@RequestMapping(value = "/v2/mySite", method = RequestMethod.GET)
public @ResponseBody
Object  SitesV2( @RequestParam(value = "ajouterstatus", required = false) Integer ajouterstatus,
        @RequestParam(value = "monadresse", required = false) String monadresseHttpServletRequest request, HttpServletResponse responseHttp{ 

 try {
        appname = request.getHeaders( "Name" ).nextElement().toString();
    } catch ( Exception e ) {
        return new Result( "", ": AppName is Empty" );
    }

Any idea ?? Thank you

  • Do you have any error in your log when you start the server or when you access the URL? – Cédric Couralet Apr 01 '16 at 15:20
  • @CédricCouralet Thank you for you comment, no i dont have any error at all, and i run teh app in a debug mode and i saw with the first link i can recover my parameter with the second one [it accesses] the method but the parameters are null, and this is why it returns no name found – Mehaibia Oumaima Apr 01 '16 at 15:28
  • I think you will have to provide the code you're using to retrieve the parameters. – Cédric Couralet Apr 01 '16 at 15:33
  • OK, i'll edit my question but i think that the problem is with the servlet or the xml file, please stay with me, will post the code in a minute Thank you – Mehaibia Oumaima Apr 01 '16 at 15:35
  • So with this link http://localhost:8080/sitesApi/v1/mysite?id=1&fields=site.name i can have what i seek and with this one too : http://localhost:8080/sitesApi/v2/mysite?id=1&fields=site.name BUT with the following i don't get what i want: http://localhost:8080/api/v1/mysite?id=1&fields=site.name the same thing for http://localhost:8080/api/v2/mysite?id=1&fields=site.name – Mehaibia Oumaima Apr 01 '16 at 15:48
  • What do you get in the error message? is it appName is empty? – Cédric Couralet Apr 01 '16 at 15:53
  • Actually it's not an error it's a message that i code to be returned in case my Appname is empty. – Mehaibia Oumaima Apr 01 '16 at 15:54

1 Answers1

0

You can use as many Dispatcher Servlets as you want but each one of them will need to be configured with their own xml file.

So, for your servlet named 'api', you must be having the configuration file in place named as: 'api-servlet.xml'.

Now, you need to have 'webservice-servlet.xml' in place for your 'webservice' dispatcher servlet to work.

You can refer to following answers:

How does DispatcherServlet work if we have multiple XML Configuration file?

Multiple application context, multiple dispatcher servlets?

Community
  • 1
  • 1
Anmol Gupta
  • 2,797
  • 9
  • 27
  • 43
  • thank you for you answer, actually i have three XML files the web.xml and the webservice-servlet.xml and api-servlet.xml, but still doesnt work ! – Mehaibia Oumaima Apr 03 '16 at 21:21
  • This `webservice-servlet.xml` is `api-web-servlet.xml` and this `api-servlet.xml` is `api-r-servlet.xml` and both of these XML files have information about **beans/context annotation and base-package** – Mehaibia Oumaima Apr 03 '16 at 21:39
  • can you tell me please what should i put inside the second servlet ? `webservice-servlet.xml` ?? – Mehaibia Oumaima Apr 04 '16 at 09:10
  • try naming them exactly webservice-servlet.xml and api-servlet.xml ? – Anmol Gupta Apr 04 '16 at 09:16
  • can you tell what can i find in webservice-servlet.xml ?? – Mehaibia Oumaima Apr 04 '16 at 09:23
  • It will be same as your current api-web-servlet.xml, since you want the same servelets to be mapped in both the dispatches servlets. – Anmol Gupta Apr 04 '16 at 09:26
  • you want to say for the servlet `webservice /api/*` i should name its xml with `api-web-servlet.xml` and with `api /sitesApi/*` i should name the xml with `webservice-servlet.xml` – Mehaibia Oumaima Apr 04 '16 at 09:36
  • i am sorry i don't get it why should i have two xml files for one servlet! ? – Mehaibia Oumaima Apr 04 '16 at 09:42
  • Can you please explain this part of your answer ? **Now, you need to have 'webservice-servlet.xml' in place for your 'webservice' dispatcher servlet to work.** – Mehaibia Oumaima Apr 04 '16 at 09:54
  • Which is the one 'servlet' you are talking about? 1) You have two 'Dispatcher' Servlets in your web.xml 2) You want both the 'Dispatcher' Servlets to have reference to some same set of 'Servlets'. So, you need two different xml's to activarte both of your 'Dispatcher' Servlets. And since, both the Dispatcher servlets need to refer to common servlets, each of their file will have reference to those 'Servlets'. You should read more about 'Dispatcher' Servlets. – Anmol Gupta Apr 04 '16 at 09:55
  • And anyway, if your objective is to point two different URL's to same servlet, then you can do it with single Dispatcher Servlet: One such answer is here: http://stackoverflow.com/questions/3898442/spring-mvc-mapping-multiple-urls-to-same-controller – Anmol Gupta Apr 04 '16 at 09:56
  • Thank you for your time Sir – Mehaibia Oumaima Apr 04 '16 at 10:01