HttpServletRequestListener in Vaadin 7

Hi All,

I am migrating an application from Vaadin6 to Vaadin7.

In Vaadin 6, the first class of my application begins with :
“public class AppliLauncher extends Application
implements HttpServletRequestListener
{ …}”

HttpServletRequestListener allows to override the following method which is executed at each request:
public void onRequestStart(HttpServletRequest pRequest, HttpServletResponse pResponse)
{…}

But i cannot use this interface with Vaadin 7 like this :
public class AppliLauncher extends UI
implements HttpServletRequestListener
{ … }.

So, how can i use the onRequestStart method with Vaadin 7 ?

Regards
Eric

This has been discussed before, and there seem to be several ways to do it, but the closest we found was by extending VaadinServlet to return a custom VaadinServletService using code something like this:


    @Override
    protected VaadinServletService createServletService(DeploymentConfiguration deploymentConfiguration) throws ServiceException {
        VaadinServletService servletService = new VaadinServletService(this, deploymentConfiguration){
			@Override
            public void requestStart(VaadinRequest request, VaadinResponse response) {
                super.requestStart(request, response);
            }

            @Override
            public void requestEnd(VaadinRequest request, VaadinResponse response, VaadinSession session) {
                super.requestEnd(request, response, session);
            }
        };
        servletService.init();
        return servletService;
    }

Hi David,

Thank you for your reply, but when i add your function into my example, i get a lot of errors i do not understand (the code between [RED]
tags is in red color in Eclipse, with so suggestion).

Could you explain how to use your function ?


@SuppressWarnings("serial")
public class Testvaadin7UI extends UI {
	
	@Override
	protected void init(VaadinRequest request) {
		
		final VerticalLayout layout = new VerticalLayout();
		layout.setMargin(true);
		setContent(layout);

		Button button = new Button("Click Me");
		//button.addStyleName("blue");
		button.addClickListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				layout.addComponent(new Label("Thank you for clicking"));
			}
		});
		layout.addComponent(button);
     }

    @Override
    protected VaadinServletService createServletService(DeploymentConfiguration deploymentConfiguration) throws ServiceException {
        [red]
VaadinServletService servletService = new VaadinServletService(this, deploymentConfiguration){
			@Override
            public void requestStart(VaadinRequest request, VaadinResponse response) {
                super.requestStart(request, response);
            }

            @Override
            public void requestEnd(VaadinRequest request, VaadinResponse response, VaadinSession session) {
                super.requestEnd(request, response, session);
            }
        };
[/red]
        servletService.init();
        return servletService;
    }

[/quote]

You misunderstood since you put that code in your overriding of UI, not of VaadinServlet.

I agree with you, but i do not understand how to use it.
Could you please show me how to put it in the following simple application which displays one button ??


@Override
	protected void init(VaadinRequest request) {
		final VerticalLayout layout = new VerticalLayout();
		layout.setMargin(true);
		setContent(layout);

		Button button = new Button("Click Me");
		button.addClickListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				
				layout.addComponent(new Label("Thank you for clicking"));
				int remoteScreenSize = getPage().getWebBrowser().getScreenHeight();
				WebBrowser browser = getUI().getPage().getWebBrowser();
				//System.out.println("width is "+browser.getBrowserWidth()+" height is "+browser.);
			}
		});
		layout.addComponent(button);
	}

You basically have to create a new class extending VaadinServlet. I recommend you to do that in a new file although you could put it in your UI class as a subclass. public class MyServlet extends VaadinServlet{ //The code David posted }

Thanks, but i get the following issue :

With Vaadin 6, i had one class for the init() method and for the onRequestStart() methods, these 2 methods shared, among other things, the log file.
onRequestStart() is executed first, then init() is executed only once, then, for each request, onRequestStart() is executed.

But now, with Vaadin 7, these 2 methods are in 2 separated classes : init() is inside MyProject extends UI,
and onRequestStart is inside MyServlet extends VaadinServlet.

I would like these 2 classes to share some reference of objects like log file and user settings, do you know how to do that ?

Eric

Not sure if that’s what you mean but the servlet also has a servletinitialized void.[code]
@Override
protected void servletInitialized()
throws ServletException {

}

[/code]

There is no relation between servletInitialized() and “MyAppli extends UI” because UI is not a servlet

Eric

In my vaadin7 application, the starting class is “StartingClass extends UI”
I am using also “MyServlet extends VaadinServlet”, and i want this class uses the same references than StartingClass (for logs, …).

So, my problem is : how to find the reference of the StartingClass instance from MyServlet.

A first solution is the following code which takes all te UIs, and searches the one named “StartingClass” :


ArrayList<UI> collUi = (ArrayList<UI>) VaadinSession.getCurrent().getUIs(); 
for (Iterator<UI> it = ((Collection<UI>)collUi).iterator();it.hasNext();)
{
	if (it instanceof StartingClass ) {
		StartingClass ref_startingClass = (StartingClass ) it;
	}
}

Is there an easier way to get the reference of the instance of StartingClass ?

Eric

In my case I was using in Vaadin 6 the following code to get LIferay portlet info:

[code]
public void onRequestStart(PortletRequest request, PortletResponse response) {

      if (getUser() == null) {

            try {

                User user = PortalUtil.getUser(request);

                setUser(user);

                ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

                setScopeGroupId((int) themeDisplay.getScopeGroupId());    

                setUrlPortal(themeDisplay.getPortalURL());

                System.err.println("URL de PORTAL" +themeDisplay.getPortalURL());

                

            }

[/code]As in Vaadin 7.1 onRequestStart(…) is gone , after looking for a alternative, I didn’t find it, but testing I got the following code that works fine in Vaadin 7 to get the same Info, then I post here to show How you can very easy info form Portlet response.
here is the code:

    @Override

    protected void init(VaadinRequest request) {

        

        ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

.....................
        if (themeDisplay != null) // then you can test also as standalone / no portlet

            {

            setIdLogin(themeDisplay.getUserId());

            setIdEntidad(themeDisplay.getCompanyId());

            setScopeGroupId((int) themeDisplay.getScopeGroupId());

            }

In other words, “init(VaadinRequest request)…” is equivalent to “onRequestStart(PortletRequest request,” and more simple to implement