Re: How to access Tomcat session objects directly via JavaScript?

2021-05-18 Thread leo



On 17 May 2021, at 22:19, Christopher Schultz wrote:

> Leo,
>
> On 5/15/21 21:16, leo wrote:
>> Hi Folks
>>
>> In a small scale personal project served by Tomcat I record things per 
>> logged-in user. The user is available as an attribute to Tomcat’s session 
>> objects. In the JSP pages I retrieve the session object *through Java* like 
>> this
>>
>>      <%
>>      HttpSession session = request.getSession();
>>      ...
>>      user = (String)session.getAttribute("user");
>>      ...
>>      %>
>>
>> Then later on these pages *in the JavaScript* part I have:
>>
>>      
>>      var user = "<%=user%>"
>>      ...
>>      
>>
>> In the JavaScript part I then do something with the *JavaScript* variable 
>> `user`.
>>
>> I now would like to move away from JSP to normal HTML pages (stills served 
>> by Tomcat with the session backend)
>>
>> Question: Can I get the content of the session object in pure JavaScript? 
>> Can I somehow configure Tomcat to put the session info in some page headers 
>> or similar so that JavaScript can read it directly?
>
> If you want to switch to javascript, you'll probably want to make an 
> XMLHttpRequest from your js code on the client to your Tomcat server. Have 
> the server's response be something in JSON. Feel free to pass the whole 
> session (which I wouldn't recommend) or just what you need. Perhaps you could 
> create an API call like /user/info which returns a JSON object representing 
> the user:
>
> {
>   "username" : "leo",
>   "timezone" : "America/Chicago",
>   "first" : "Leo",
>   "last" : "Tomcat"
> }
>
> Short of dumping the whole session data into the HTML page (which is ugly, 
> wasteful, and potentially dangerous), the above is probably your best bet.
>
> -chris


That’s probably the way to go.

Thanks a lot for chiming in here!

Leo

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Re: How to access Tomcat session objects directly via JavaScript?

2021-05-18 Thread leo



On 17 May 2021, at 16:41, Luis Rodríguez Fernández wrote:

> Hello Leo,
>
> You can add your custom header in the response via the
> HttpServletResponse.addHeader() [1] method.
>

That’s exactly what I was looking for!

…However it doesn’t help much, because I have just discovered that JavaSCript 
cannot access its own page headers!

Never mind…

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Re: How to access Tomcat session objects directly via JavaScript?

2021-05-17 Thread Christopher Schultz

Leo,

On 5/15/21 21:16, leo wrote:

Hi Folks

In a small scale personal project served by Tomcat I record things per 
logged-in user. The user is available as an attribute to Tomcat’s 
session objects. In the JSP pages I retrieve the session object *through 
Java* like this


     <%
     HttpSession session = request.getSession();
     ...
     user = (String)session.getAttribute("user");
     ...
     %>

Then later on these pages *in the JavaScript* part I have:

     
     var user = "<%=user%>"
     ...
     

In the JavaScript part I then do something with the *JavaScript* 
variable `user`.


I now would like to move away from JSP to normal HTML pages (stills 
served by Tomcat with the session backend)


Question: Can I get the content of the session object in pure 
JavaScript? Can I somehow configure Tomcat to put the session info in 
some page headers or similar so that JavaScript can read it directly?


If you want to switch to javascript, you'll probably want to make an 
XMLHttpRequest from your js code on the client to your Tomcat server. 
Have the server's response be something in JSON. Feel free to pass the 
whole session (which I wouldn't recommend) or just what you need. 
Perhaps you could create an API call like /user/info which returns a 
JSON object representing the user:


{
  "username" : "leo",
  "timezone" : "America/Chicago",
  "first" : "Leo",
  "last" : "Tomcat"
}

Short of dumping the whole session data into the HTML page (which is 
ugly, wasteful, and potentially dangerous), the above is probably your 
best bet.


-chris

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Re: How to access Tomcat session objects directly via JavaScript?

2021-05-17 Thread Luis Rodríguez Fernández
Hello Leo,

You can add your custom header in the response via the
HttpServletResponse.addHeader() [1] method.

Hope it helps,

Luis

[1]
https://tomcat.apache.org/tomcat-8.5-doc/servletapi/javax/servlet/http/HttpServletResponse.html#addHeader(java.lang.String,%20java.lang.String)






El lun, 17 may 2021 a las 3:54, leo ()
escribió:

> Rony,
>
> Thanks for chiming! :-)
>
> >> […] In the JSP pages I retrieve the session
> >> object *through Java* like this
> >>
> >> <%
> >> HttpSession session = request.getSession();
> >> ...
> >> user = (String)session.getAttribute("user");
> >> ...
> >> %>
> >>
> >> Then later on these pages *in the JavaScript* part I have:
> >>
> >> 
> >> var user = "<%=user%>"
> >> ...
> >> 
> >>
> >> Question: Can I get the content of the session object in pure
> >> JavaScript? […]
> >
> > what taglib are you using?
>
> Sorry to appear ignorant, but I don’t know. I just use the <% …. %>
> and <%= … %> constructs in my JSP pages.
>
> I don’t maintain the server (it is Tomcat 8.5), but I could request
> small changes  (as long as they don’t break existing stuff!). Any way
> to find out what taglib the server provides?
>
> > or is your example purely hypothetical?
>
> No, no not at all. This is the code I use.
>
> Thanks and regards,
> Leo
>
> -
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
>
>

-- 

"Ever tried. Ever failed. No matter. Try Again. Fail again. Fail better."

- Samuel Beckett


Re: How to access Tomcat session objects directly via JavaScript?

2021-05-16 Thread leo
Rony,

Thanks for chiming! :-)

>> […] In the JSP pages I retrieve the session
>> object *through Java* like this
>>
>> <%
>> HttpSession session = request.getSession();
>> ...
>> user = (String)session.getAttribute("user");
>> ...
>> %>
>>
>> Then later on these pages *in the JavaScript* part I have:
>>
>> 
>> var user = "<%=user%>"
>> ...
>> 
>>
>> Question: Can I get the content of the session object in pure
>> JavaScript? […]
>
> what taglib are you using?

Sorry to appear ignorant, but I don’t know. I just use the <% …. %>
and <%= … %> constructs in my JSP pages.

I don’t maintain the server (it is Tomcat 8.5), but I could request
small changes  (as long as they don’t break existing stuff!). Any way
to find out what taglib the server provides?

> or is your example purely hypothetical?

No, no not at all. This is the code I use.

Thanks and regards,
Leo

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Re: How to access Tomcat session objects directly via JavaScript?

2021-05-16 Thread Rony G. Flatscher (Apache)
Leo,

On 16.05.2021 03:16, leo wrote:
> Hi Folks
>
> In a small scale personal project served by Tomcat I record things per 
> logged-in user. The user is
> available as an attribute to Tomcat’s session objects. In the JSP pages I 
> retrieve the session
> object *through Java* like this
>
>     <%
>     HttpSession session = request.getSession();
>     ...
>     user = (String)session.getAttribute("user");
>     ...
>     %>
>
> Then later on these pages *in the JavaScript* part I have:
>
>     
>     var user = "<%=user%>"
>     ...
>     
>
> In the JavaScript part I then do something with the *JavaScript* variable 
> `user`.
>
> I now would like to move away from JSP to normal HTML pages (stills served by 
> Tomcat with the
> session backend)
>
> Question: Can I get the content of the session object in pure JavaScript? Can 
> I somehow configure
> Tomcat to put the session info in some page headers or similar so that 
> JavaScript can read it
> directly?

what taglib are you using or is your example purely hypothetical?

---rony



-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



How to access Tomcat session objects directly via JavaScript?

2021-05-15 Thread leo

Hi Folks

In a small scale personal project served by Tomcat I record things per 
logged-in user. The user is available as an attribute to Tomcat’s 
session objects. In the JSP pages I retrieve the session object *through 
Java* like this


<%
HttpSession session = request.getSession();
...
user = (String)session.getAttribute("user");
...
%>

Then later on these pages *in the JavaScript* part I have:


var user = "<%=user%>"
...


In the JavaScript part I then do something with the *JavaScript* 
variable `user`.


I now would like to move away from JSP to normal HTML pages (stills 
served by Tomcat with the session backend)


Question: Can I get the content of the session object in pure 
JavaScript? Can I somehow configure Tomcat to put the session info in 
some page headers or similar so that JavaScript can read it directly?


Thanks for any pointers,
Leo

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org