Issue
I have to check if a user is using a mobile device.
I can do it with this code
String ua=request.getHeader("User-Agent");
In order to use request
I need a servlet (do I?), so I created one and I put that code inside doPost/doGet
method.
Since those are void
methods, how can I get a return value to know if a user is using a mobile?
Probably my approach is not the right one.
My goal is to get from that servlet (assuming that I need just a servlet) a value that tells me if a user is using a mobile device.
Solution
One way is to use session and keep the String ua
in the session
Either in the doPost()
or in doGet()
String ua=request.getHeader("User-Agent");
Httpsession session=request.getSession();
session.setAttribute("ua",ua);
Now where ever you want to check just do
session.getAttribute("ua");
As long as the session
exists, the value will be present.
Answered By - SpringLearner
Answer Checked By - Robin (JavaFixing Admin)