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 come an AMAZINGLY long way in the past 9 years. As the comments 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
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
onloadevent of the body (or$(document).readyin JQuery), to call javascript, that sets thesrcattribute of your image, to the URL that you need to use.