admin管理员组

文章数量:1323348

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 (width) as a jsp variable.

2021 update: Wow, 9 years later. If you're looking at this (and some of you still are) -- Javascript has e an AMAZINGLY long way in the past 9 years. As the ments below say, this isn't possible in JSP, but it is possible in Javascript, and CSS3. If you're trying to determine the width of a page to resize elements on the fly, you're likely not respecting responsive principals that have been developed over the ages. Look into CSS Flexbox and design with that in mind.

Cheers

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 (width) as a jsp variable.

2021 update: Wow, 9 years later. If you're looking at this (and some of you still are) -- Javascript has e an AMAZINGLY long way in the past 9 years. As the ments below say, this isn't possible in JSP, but it is possible in Javascript, and CSS3. If you're trying to determine the width of a page to resize elements on the fly, you're likely not respecting responsive principals that have been developed over the ages. Look into CSS Flexbox and design with that in mind.

Cheers

Share Improve this question edited Jan 13, 2021 at 15:01 A_Elric asked Aug 17, 2012 at 19:38 A_ElricA_Elric 3,56815 gold badges55 silver badges87 bronze badges 3
  • 2 Not possible in JSP, since that's running on the server. You'd need to use client-side Javascript to get that information and pass it back to the server somehow (e.g. ajax request). – Marc B Commented Aug 17, 2012 at 19:41
  • Either that (ajax), or wait until page load to detect and set the img src attribute. What are you actually trying to do? Is this a background image? There are ways to "dynamically size" an image with CSS and/or Javascript. – Jared Farrish Commented Aug 17, 2012 at 19:43
  • Here's what I mean using jQuery (don't hold it against me, I wrote that code a while back): huckabee-inc./en/index.html The same effect can be gained with plain Javascript, you just have to be careful when getting the screen size, which can differ across browsers. Note, refresh to get different images, click to different pages, and try resizing. The script is in the head element. – Jared Farrish Commented Aug 17, 2012 at 19:48
Add a ment  | 

2 Answers 2

Reset to default 6

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.

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>
<body onload=init()>
<A style='' id=cm HREF=''>&nbsp;</A>
</body>

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

本文标签: javascriptGetting screen width as a variable in jspStack Overflow