users@grizzly.java.net

Re: add second ServletAdapter to GrizzlyWebServer after webserver.start()

From: Edward Chou <Edward.Chou_at_Sun.COM>
Date: Thu, 02 Apr 2009 16:30:42 -0700

Hi Jeanfrancois,

Attached is my testcase for this issue. Basically, it registers
"/servlet1" to GrizzlyWebServer before server start, and "/servlet2" to
GrizzlyWebServer after server start.

Then you point your browser to "http://localhost:9999/servlet1", it
returns "content for servlet1", this is expected. However, then you
point your browser to "http://localhost:9999/servlet2", it still returns
"content for servlet1", but expected result should be "content for
servlet2".

If you move the "webServer.start()" call after servlet2 is added, then
everything works as expected.

Thanks for your help,
Edward

Jeanfrancois Arcand wrote:
> Salut,
>
> Edward Chou wrote:
>> I am using the latest version, 1.9.10.
>>
>> Jeanfrancois Arcand wrote:
>>> Salut,
>>>
>>> Edward Chou wrote:
>>>> Hi,
>>>>
>>>> I am wondering if the following is possible:
>>>>
>>>> GrizzlyWebServer webServer = new GrizzlyWebServer(9999);
>>>> ServletAdapter adapter1 = createAdapter("/atom1");
>>>>
>>>> webServer.addGrizzlyAdapter(adapter1, new String[] { "/atom1" });
>>>> webServer.start();
>>>>
>>>> < .. do something else ... >
>>>> ServletAdapter adapter2 = createAdapter("/atom2");
>>>> webServer.addGrizzlyAdapter(adapter2, new String[] { "/atom2" });
>>>
>>> Yes, it should work:\. Take a look at:
>>>
>>> https://grizzly.dev.java.net/nonav/xref-test/com/sun/grizzly/http/GrizzlyWebServerTest.html
>>>
>
> I've just added a unit test and for me it pass. Can you share your code?
>
>> +
>> + public void testAddGrizzlyAdapterBeforeAndAfterStart() throws
>> IOException {
>> + System.out.println("testAddGrizzlyAdapterBeforeAndAfterStart");
>> + try {
>> + gws = new GrizzlyWebServer(PORT);
>> + String[] aliases = new String[]{"/1", "/2", "/3"};
>> + for (String alias : aliases) {
>> + addAdapter(alias);
>> + }
>> + gws.start();
>> + String alias = "/4";
>> + addAdapter(alias);
>> +
>> + HttpURLConnection conn = getConnection(alias);
>> + assertEquals(HttpServletResponse.SC_OK,
>> + getResponseCodeFromAlias(conn));
>> + assertEquals(alias, readResponse(conn));
>> + } finally {
>> + stopGrizzlyWebServer();
>> + }
>> + }
>> +
>> +
>> +
>
> Thanks
>
> -- Jeanfrancois
>
>
>
>>>
>>>
>>>>
>>>>
>>>> Basically, I want to deploy a second ServletAdapter after the
>>>> GrizzlyWebServer has already been started. Currently, the second
>>>> servlet is not activated, but first servlet is. Any help is
>>>> appreciated.
>>>
>>> Hum...let me try it then. Which version are you using?
>>>
>>> Thanks
>>>
>>> -- Jeanfrancois
>>>
>>>
>>>>
>>>> Edward
>>>>
>>>>
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
>>> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
>> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>



package grizzly;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.grizzly.http.embed.GrizzlyWebServer;
import com.sun.grizzly.http.servlet.ServletAdapter;

/**
 * GrizzlyServerTest.java
 *
 * @author Edward Chou
 */
public class GrizzlyServerTest {

    
    public static void main(String[] args) {
    
        GrizzlyWebServer webServer = new GrizzlyWebServer(9999);
        
        try {
            ServletAdapter adapter1 = createAdapter1();
            ServletAdapter adapter2 = createAdapter2();

            webServer.addGrizzlyAdapter(adapter1, new String[] { "/servlet1" });

            System.out.println("server started ...");
            webServer.start();

            System.out.println("sleeping for 10 seconds...");
            Thread.sleep(10000);

            webServer.addGrizzlyAdapter(adapter2, new String[] { "/servlet2" });
            System.out.println("servlet2 added ...");

            
            System.out.println("Hit return to stop...");
            System.in.read();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            webServer.stop();
        }
        
        
    }
    
    private static ServletAdapter createAdapter1() {
        ServletAdapter adapter = new ServletAdapter(new HttpServlet() {
            @Override
            protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
                resp.setStatus(HttpServletResponse.SC_OK);
                resp.getWriter().write("content for servlet1");
            }
        });
        return adapter;

    }
    
    private static ServletAdapter createAdapter2() {
        ServletAdapter adapter = new ServletAdapter(new HttpServlet() {
            @Override
            protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
                resp.setStatus(HttpServletResponse.SC_OK);
                resp.getWriter().write("content for servlet2");
            }
        });
        return adapter;

    }
    
}