It's not a secret anymore!
The moose likes Servlets and the fly likes Users screen resolution from servlet? JSP? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Users screen resolution from servlet? JSP?" Watch "Users screen resolution from servlet? JSP?" New topic

Users screen resolution from servlet? JSP?

DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
Im working on a new servlet driven site with an extremely constraining design and REALLY need to know the user screen resolution before rendering a page. I have successfully communicated this from the main site page (html) to the next page (servlet) using Javascript to get the screen.width & then concatenating it to the url in the redirect....OK no problem so far you say. All was well until lightning struck me this morning, what do I do when someone doesnt come in thru the main page (IE: a bookmark) ...I have no resolution to work with....YIKES, design blows up!
Does anyone know a way to get the users browser info from a servlet / JSP or any outside package that could possibly help me with this. All I need to know is what resolution they are using....thats all. If I cant make this work its back to the drawing board design wise & that really isnt an option at this point. Any & all help would be DEEPLY appreciated as this puppy is in the final phases......thanks
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

Hmmm, off the top of my head: perhaps if you get to a page and don't have the screen resolution request parameters, you could redirect to a page (that emits no output) that gathers the data using javascript and then redirects back to the original page.

[Asking smart questions] [About Bear] [Books by Bear]
DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
Well Bearyou get a GOLD star for thinking outside the box. I thought about it today & came up with the EXACT same idea & by jove it works like a dream! Heres what I did just incase anyone else gets stuck with this problem:
Servlet code:
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
ServletContext sc = getServletContext();
HttpSession ses = req.getSession(true);
int pageWidth = 0;
String page = req.getParameter("page").trim();
/* Check the incoming request for the screen width variable */
if ((req.getParameter("width") == null) & (ses.getAttribute("custWidth") == null)){
/*variable doesnt exist - redirect user thru javascript page to get the variable & then back to this servlet */
/* Get the customers screen width from either the session or the query string */
if (ses.getAttribute("custWidth") != null){
pageWidth = Integer.parseInt((String)ses.getAttribute("custWidth"));
pageWidth = Integer.parseInt(req.getParameter("width").trim());
String sesPageWidth = ""+pageWidth;
ses.setAttribute("custWidth", sesPageWidth);
and here's the javascript in an html file:
function getWidth(){
var width = screen.width;
var pageString =;
var page = pageString.substring(6, pageString.indexOf("&"));
var servlet = pageString.substring(pageString.indexOf("&serv=") +6, pageString.length);
location.href = "/servlet/"+servlet+"?page="+page+"&width="+width;
The beauty of it is the redirect is so fast they user never even knows the went thru it & if the session expires they are just sent thru it again.
I agree. Here's the link:
subject: Users screen resolution from servlet? JSP?
It's not a secret anymore!