Get initial Request Parameters in UI.init Method

Hi,

we are using a static HTML page as a start page for our vaadin application. Our start page makes a post request, which is handled by our vaadin servlet. Within the servlet request we can access the request parameter from our initial request. But they aren’t available in the init method of the UI instance. I guess this happens because the ui instance get the request by the vaadin loader page.

Is there a way to pass the parameter from the initial request to the init method of the UI instance?

thanks in advance

Hi,

I ran into the same problem and also found a ticket for that:
http://dev.vaadin.com/ticket/13079
(no solution yet). I did a bit research and found a workaround.
The problem is indeed that the first POST is handled by com.vaadin.server.communication.ServletBootstrapHandler which just returns an HTML page that bootstraps the UI with a second request (where the POST parameters are not passed through).
My solution was to customize the VaadinServletService in way that is saves the POST parameters in the session, so they are available in UI.init later:

[code]
public static class Servlet extends VaadinServlet {

@Override
protected VaadinServletService createServletService(DeploymentConfiguration deploymentConfiguration) throws ServiceException {
VaadinServletService service = new VaadinServletService(this, deploymentConfiguration) {

  @Override
  public void requestEnd(VaadinRequest request, VaadinResponse response, VaadinSession session) {
    super.requestEnd(request, response, session);
    final String myParam = request.getParameter("myParam");
    if (myParam != null) {
      session.setAttribute("myParam", myParam);
    }
  }
};
service.init();
return service;

}
}

public class MyUI extends UI {

@Override
protected void init(VaadinRequest request) {
    Object myParam = getSession().getAttribute("myParam");
    if (myParam != null) {
        //do sth. with myParam
        getSession().setAttribute("myParam", null);//reset if necessary
    }
}


}
[/code]Note that in this solution the parameter is checked and added to the session on
every
request. Instead of subclassing VaadinServletService you could add a SessionInitListener. Then the parameter would only be added on the first request when the session is created. The downside is that you will not get the parameter in your UI.init method if the the user has a valid Vaadin session open when the initial request is made:

@Override
protected VaadinServletService createServletService(DeploymentConfiguration deploymentConfiguration) throws ServiceException {
    VaadinServletService service = super.createServletService(deploymentConfiguration);
    service.addSessionInitListener(new SessionInitListener() {
        
        @Override
        public void sessionInit(SessionInitEvent event) throws ServiceException {
            final String myParam = event.getRequest().getParameter("myParam");
            if (myParam != null) {
                event.getSession().setAttribute("myParam", myParam);
            }
        }
    });
    return service;
}

Another approach would be to customize the ServletBootstrapHandler such that it passes through the POST parameters.

Hope that helps,
Sven

Hi,
Thanks for the useful info. I am still stuck and i would appreciate your help.
My Vaadin application (app2) is started from another application (app1) which needs to include some request parameters for tailoring the start behaviour of the Vaadin application.
User via browser access app1 and clicks a button to create an http post message with the parameters and send it to the app2.
At app2 the above code (requestEnd) works so i get the parameter, verify and attach to the session.
Ideally the flow should be to allow the user browser into tailored app2. so the question is: how do i forward/redirect the client to get to app2 using the same session (or session info)?

Much appreciated,
B

Hi Sven,
I am facing the same probelem. I tried your approach and created the MyServlet class which extends the VaadinServlet.
However , when I do the request.getParameter(), the value is still coming as null

My Html is really simple


This is my Servlet class

public class Servlet extends VaadinServlet{

@Override
  protected VaadinServletService createServletService(DeploymentConfiguration deploymentConfiguration) throws ServiceException {
    VaadinServletService service = new VaadinServletService(this, deploymentConfiguration) {

      private static final long serialVersionUID = 1L;

        @Override
      public void requestEnd(VaadinRequest request, VaadinResponse response, VaadinSession session) {
        super.requestEnd(request, response, session);
        final String myParam = request.getParameter("app_name");
        System.out.println("--------------userName-------"+myParam);
        if (myParam != null) {
          session.setAttribute("sessionId", myParam);
        }
      }
    };
    service.init();
    return service;
  }

}

Is there something I am missing? Any help will be welcome

Actually I am able to receive the parameters in SessionInit() method only for the first time when the Session is Initialized.
If I want to receive the parameters each time, I had to override the service() method

    @Override
    protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        super.service(request, response);        
        String appName= request.getParameter("app_name");
         if(appName!=null){
            getService().getServlet().getServletContext().setAttribute("app_name", appName);  
          
        }
            
    }

I think I had a similar problem and fixed it by getting the parameter in the init() method and then registering a handler both on the inital call. The handler will get the parameter on subsequent calls:

@Override
    protected void init(VaadinRequest request) {
        
        token = request.getParameter("token");

        VaadinSession.getCurrent().addRequestHandler(
                new RequestHandler() {
            private static final long serialVersionUID = 1L;

            @Override
            public boolean handleRequest(VaadinSession session,
                                         VaadinRequest request,
                                         VaadinResponse response)
                    throws IOException {
                token = request.getParameter("token");
                return false;
            }
        });

Hopefully this can help someone.

Please take a look at this post:
http://outofrange.ru/2016/10/how-to-pass-params-into-vaadin-s-ui-and-control-the-view-to-be-loaded/

Hi, how are you? This method can be used to redirect a user to an error.html? That is, an error page in my wildFly?

I have in my webApp this, And I managed to redirect some path as heartbeat/ but VAADIN/ I can not

<Error-page>
 <Location> /error.html </ location>
</ Error-page>

Ruben, you could use these parameters to take whatever action you want based on the parameters/values you get in the request. I think, however, that you are trying to do something else. Depending on the type of error, you would redirect the user to your error page either using server settings or exception handling in Java.

Many thanks for the real help, I’m trying to just redirect to the main class, when the user accesses a path that does not exist, or to the path like, hearbeat/ vaadin/ uudl/

For example, when I get this path I get that … according to that information only on the client side, but in my company do not want to see that.

In my common application I also get the same, I want to implement that handler to see
31104.png

It looks like you may be trying to manage the endpoints or mappings. You can do that in the main UI within Vaadin:

@WebServlet( value = “/”, value = “/anendpoint”, asyncSupported = true ) // or use: urlPatterns = {“/", "/someendpoint/”, “/VAADIN/*”})
@VaadinServletConfiguration(productionMode = true,
ui = MyUI.class,
closeIdleSessions = true,

If you have a value of “/*” for the WebServlet, it should catch all values in the Vaadin Servlet.

You can map multiple servlets within your app.

You also manage mappings and filters in the web.xml or other container/server configuration files.

My example was to intercept query parameters for use within a Vaadin app, such as “v-uild” and its value in your example. If you just want to go to an error page if you get a certain parameter value, you could look at the parameter and then redirect to an error page if you get anything else.

Thanks so I have my webServlet, the part of the web.xml about error.html, that if it has worked for me in some, some URL path not all
31105.png

Hello;Am facing the same probleme here
did u get the solution?