Skip to content Skip to sidebar Skip to footer

Getting Screen Width As A Variable In Jsp

I need to know to resize an image (it needs to be passes as img src='blah.jpg?width=x) so my page can be displayed at different sizes, can someone help me out here? I just need x

Solution 1:

What you are asking for can't be done in this way. Your JSP is rendered on the server, and sent to the browser as HTML. Only once the page reaches the browser, is the screen width available (using JavaScript). So getting it as a JSP variable is simply not possible.

What you could instead, is use JavaScript to get the screen width in the previous page, and set it as a query parameter in a link, or as a form's hidden field. When the link is clicked, or the form is submitted, you can access the request to get the screen width.

Of course, this doesn't work if it's possible for the user to access the URL for that page by entering it directly in the browser.

Alternatively, use JavaScript to load the image after the page has loaded. You'd have the screen width at this time, and could construct the URL accordingly. Essentially, you'd use the onload event of the body (or $(document).ready in JQuery), to call javascript, that sets the src attribute of your image, to the URL that you need to use.

Solution 2:

I had the same problem, but then realized I could solve it in the following way: Have a startup page, such as index.html in tomcat, which gets called automatically at the start of the session and do this:

<!DOCTYPE html><html><head><script>var init=function() {
        var noi=window.innerHeight;
        url="myPage.jsp?rows="+noi;
        cm=document.getElementById('cm');
        cm.href=url;
        cm.click();
    }
    </script></head><bodyonload=init()><Astyle=''id=cmHREF=''>&nbsp;</A></body>

In your 'myPage.jsp' page, collect the variable 'rows' in your java code and you're ready to roll!

Post a Comment for "Getting Screen Width As A Variable In Jsp"