3

I have an Eclipse RAP 2.3 application with two entry points, say /first and /second. In the GUI of the first entry point, there is a button with which I would like to open the second entry point in a new browser tab. The event handler of that button is currently

UrlLauncher launcher = RWT.getClient().getService( UrlLauncher.class );
launcher.openURL( "/second");

This already doesn't work when the application is deployed as myapp.war in a Tomcat web server (should then be /myapp/second).

My questions:

  • What's the best way to determine the URL to open within the event handler?
  • Do I have to fetch the HttpServletRequest, get the context path and so some string manipulation?
  • Is it actually safe to call RWT.getRequest() at this point?

Update

According to Rüdiger's comment I can acquire the context path in two different ways.

  1. The first approach is

    RWT.getRequest().getContextPath();
    

    where RWT.getRequest() is documented with

    This method is not recommended

  2. Secondly, I could obtain it with

    ApplicationContextImpl ac = (ApplicationContextImpl) RWT.getApplicationContext();
    String contextPath = ac.getServletContext().getContextPath();
    

    where the IDE displays the warning

    Discouraged access: The type ApplicationContextImpl is not accessible due to restriction on required library ...\org.eclipse.rap.rwt_2.3.2.20150128-1013.jar

    Despite the warning, it still works when deploying a WAR file with OSGi bundles to Tomcat.

So, in both cases there is some kind of warning, which makes the solutions look rather like workarounds.

bgerth
  • 1,196
  • 1
  • 11
  • 19
  • The _context path_ (myapp in your case) can be obtained from the servlet context `ServletContext#getContextPath()' . Not sure though what else is missing or if such a relative URL would work. – Rüdiger Herrmann Apr 17 '15 at 10:50
  • I have updated the thread with code examples of your suggestion. But I still wonder what the best approach would be. – bgerth Apr 21 '15 at 07:17

1 Answers1

1

Using RWT.getRequest() is not recommended because usually RWT would shield you from the lower-level servlet API and certain direct interactions with the request could even interfere with RWTs life cycle and yield funny responses.

While in your case it would be safe to access the ServletContext via RWT.getRequest(), I recommend to use

RWT.getUISession( display ).getHttpSession().getServletContext();

to access the servlet context.

The second approach accesses internal classes that aren't part of the public API and therefore shouldn't be use. The accessed classes may change or be (re)moved in the future without further notice and break your application.

Rüdiger Herrmann
  • 18,905
  • 11
  • 53
  • 72