Expand my Community achievements bar.

Adobe Summit 2025: AEM Session Recordings Are Live! Missed a session or want to revisit your favorites? Watch the latest recordings now.

Mark Solution

This conversation has been locked due to inactivity. Please create a new post.

SOLVED

URL from address bar

Avatar

Level 2

Hi All,

How can I fetch browser URL in Sling Model? Example If any user has visited site https://www.google.com i want to get that in Sling Model so that process further logic.

Anybody can help?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

 

Hi @sarah_la ,

If you are adapting a request to your Sling Model, then you can use 

@SlingObject
private SlingHttpServletRequest request;

then, get URL from request using 

request.getRequestURI();

Please refer sample code snippet as below,

package com.mysite.core.models;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;

@Model(adaptables = { Resource.class, SlingHttpServletRequest.class })
public class RequestURLModel {

    @SlingObject
    private SlingHttpServletRequest request;

    public String getUrl(){
        return request.getRequestURI(); //get uri from request using
    }
}

Hope that helps!

Regards,
Santosh


Santosh Sai

AEM BlogsLinkedIn


View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

 

Hi @sarah_la ,

If you are adapting a request to your Sling Model, then you can use 

@SlingObject
private SlingHttpServletRequest request;

then, get URL from request using 

request.getRequestURI();

Please refer sample code snippet as below,

package com.mysite.core.models;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;

@Model(adaptables = { Resource.class, SlingHttpServletRequest.class })
public class RequestURLModel {

    @SlingObject
    private SlingHttpServletRequest request;

    public String getUrl(){
        return request.getRequestURI(); //get uri from request using
    }
}

Hope that helps!

Regards,
Santosh


Santosh Sai

AEM BlogsLinkedIn