From Rony.Flatscher at wu.ac.at Thu Nov 3 09:30:41 2022 From: Rony.Flatscher at wu.ac.at (Rony G. Flatscher) Date: Thu, 3 Nov 2022 10:30:41 +0100 Subject: How to handle post requests? In-Reply-To: References: Message-ID: On 27.10.2022 13:13, Simon Besenb?ck wrote: > I am using Apache Tomcat 10.0.23 and nashorn 15.4 in an eclipse ide in order to develop JSP's. > I want to send data (user input in a form) via the POST method to another JSP. > However I do not know how I can get the sent data. In Java I guess I would be using > request.getParameter(). > Is there a similar way to get the input using Nashorn? > I tried to play around with fetch() but I didn`t came to a solution. You should be able to directly access the JSP implicit objects, e.g. cf. , if using the ScriptTagLibs library [1], and all of their methods. Therefore you should be able to do something like this: <%@ taglib uri="http://rexxla.org/taglibs/jsr223" prefix="s" %> 40_helloworld_use_request.jsp: Sample Application JSP Page Using JavaScript Via JSR-223 (Title) out.println("

Hello, world (JSP with Nashorn/JavaScript)

"); // implicit JSP object 'out' print("

This JSP was executed, because of the following URL:" , "

  • URL request.getRequestURL():
    " , // implicit JSP object 'request' request.getRequestURL(), ""); print("
  • its URI being request.getRequestURI():
    " , // implicit JSP object 'request' request.getRequestURI(), "

");
HTH ---rony [1]: -------------- next part -------------- An HTML attachment was scrubbed... URL: From simon.besenbaeck at gmail.com Wed Nov 9 10:48:23 2022 From: simon.besenbaeck at gmail.com (=?UTF-8?Q?Simon_Besenb=C3=A4ck?=) Date: Wed, 9 Nov 2022 11:48:23 +0100 Subject: Accessing Java Classes Message-ID: You should be able to create Java classes in Nashorn like this: var HashMap = Java.type("java.util.HashMap")var mapDef = new HashMap()var map100 = new HashMap(100) source: https://docs.oracle.com/javase/9/nashorn/nashorn-java-api.htm#JSNUG115 However when I try to use the jakarta.servlet.http.Cookie class it does not work. My Code: var Cookie = Java.type("jakarta.servlet.http.Cookie");var newCookie = new Cookie("lastVisit", "Test"); Does anyone know why this is not working or what I`m doing wrong? Best Regards Simon -------------- next part -------------- An HTML attachment was scrubbed... URL: From szegedia at gmail.com Wed Nov 9 12:59:37 2022 From: szegedia at gmail.com (Attila Szegedi) Date: Wed, 9 Nov 2022 13:59:37 +0100 Subject: Accessing Java Classes In-Reply-To: References: Message-ID: How is it not working? What is the exception you?re getting? > On 2022. Nov 9., at 11:48, Simon Besenb?ck wrote: > > You should be able to create Java classes in Nashorn like this: > > var HashMap = Java.type("java.util.HashMap") > var mapDef = new HashMap() > var map100 = new HashMap(100) > source: https://docs.oracle.com/javase/9/nashorn/nashorn-java-api.htm#JSNUG115 > However when I try to use the jakarta.servlet.http.Cookie class it does not work. > > My Code: > > var Cookie = Java.type("jakarta.servlet.http.Cookie"); > var newCookie = new Cookie("lastVisit", "Test"); > Does anyone know why this is not working or what I`m doing wrong? > > Best Regards > Simon -------------- next part -------------- An HTML attachment was scrubbed... URL: From simon.besenbaeck at gmail.com Wed Nov 9 14:55:12 2022 From: simon.besenbaeck at gmail.com (=?UTF-8?Q?Simon_Besenb=C3=A4ck?=) Date: Wed, 9 Nov 2022 15:55:12 +0100 Subject: Accessing Java Classes In-Reply-To: References: Message-ID: I do not get an exception. When opening the JSP in the Browser I see that there is no Cookie created. The Code I am using: var lastVisit; var allCookies = request.getCookies(); if (allCookies != null) { for (var i = 0; i < allCookies.length; i++) { var c = allCookies[i]; if (c.getName() == 'lastVisit') { lastVisit = c.getValue(); } } } if (lastVisit == null) { print("This is your first Visit!"); } else { print("Your last visit was at " + lastVisit); } var Cookie = Java.type("jakarta.servlet.http.Cookie"); var newCookie = new Cookie("lastVisit", "Test"); response.addCookie(newCookie); Using request and response is possible due to this taglib: https://sourceforge.net/projects/bsf4oorexx/files/Sandbox/rgf/taglibs/ga/ Best Regards Simon Am Mi., 9. Nov. 2022 um 13:59 Uhr schrieb Attila Szegedi : > How is it not working? What is the exception you?re getting? > > On 2022. Nov 9., at 11:48, Simon Besenb?ck > wrote: > > You should be able to create Java classes in Nashorn like this: > > var HashMap = Java.type("java.util.HashMap")var mapDef = new HashMap()var map100 = new HashMap(100) > > source: > https://docs.oracle.com/javase/9/nashorn/nashorn-java-api.htm#JSNUG115 > > However when I try to use the jakarta.servlet.http.Cookie class it does > not work. > > My Code: > > var Cookie = Java.type("jakarta.servlet.http.Cookie");var newCookie = new Cookie("lastVisit", "Test"); > > Does anyone know why this is not working or what I`m doing wrong? > Best Regards > Simon > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From simon.besenbaeck at gmail.com Sat Nov 26 07:32:52 2022 From: simon.besenbaeck at gmail.com (=?UTF-8?Q?Simon_Besenb=C3=A4ck?=) Date: Sat, 26 Nov 2022 08:32:52 +0100 Subject: Accessing Java Classes In-Reply-To: References: Message-ID: Here is a short update on the case. Nashorn is working as expected. request.getCookies() works, as well as to create an instance of the class Cookie. For example, after creating the cookie I can use getName() or getValue() and it works perfectly fine. The issue that I had with not seeing the cookie in the browser still exists, but that seems to be unrelated to Nashorn. Best Regards Simon Am Mi., 9. Nov. 2022 um 15:55 Uhr schrieb Simon Besenb?ck < simon.besenbaeck at gmail.com>: > I do not get an exception. When opening the JSP in the Browser I see that > there is no Cookie created. > > The Code I am using: > > var lastVisit; > var allCookies = request.getCookies(); > > if (allCookies != null) { > for (var i = 0; i < allCookies.length; i++) { > var c = allCookies[i]; > if (c.getName() == 'lastVisit') { > lastVisit = c.getValue(); > } > } > } > > if (lastVisit == null) { > print("This is your first Visit!"); > } else { > print("Your last visit was at " + lastVisit); > } > > var Cookie = Java.type("jakarta.servlet.http.Cookie"); > var newCookie = new Cookie("lastVisit", "Test"); > response.addCookie(newCookie); > > Using request and response is possible due to this taglib: > https://sourceforge.net/projects/bsf4oorexx/files/Sandbox/rgf/taglibs/ga/ > > Best Regards > Simon > > Am Mi., 9. Nov. 2022 um 13:59 Uhr schrieb Attila Szegedi < > szegedia at gmail.com>: > >> How is it not working? What is the exception you?re getting? >> >> On 2022. Nov 9., at 11:48, Simon Besenb?ck >> wrote: >> >> You should be able to create Java classes in Nashorn like this: >> >> var HashMap = Java.type("java.util.HashMap")var mapDef = new HashMap()var map100 = new HashMap(100) >> >> source: >> https://docs.oracle.com/javase/9/nashorn/nashorn-java-api.htm#JSNUG115 >> >> However when I try to use the jakarta.servlet.http.Cookie class it does >> not work. >> >> My Code: >> >> var Cookie = Java.type("jakarta.servlet.http.Cookie");var newCookie = new Cookie("lastVisit", "Test"); >> >> Does anyone know why this is not working or what I`m doing wrong? >> Best Regards >> Simon >> >> >> -------------- next part -------------- An HTML attachment was scrubbed... URL: