0

So, I have a url that I get from the database inside my java code.

public ActionForward executeAjaxShowTab(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {

    String url = getUrl(); //returns something like https://foo.bar
    request.setAttribute("form", url); 
    return mapping.findForward(ForwardConstants.FORWARD_TAB); //goes back to alert tab on the page, not a modal dialog
}

What I'm confused about is how to pass the url above to the client side to open the url in a new tab or browser window. How can I do that? Do I need to setup a new jsp page and then set the url on that jsp page or..? I have a separate js file for all js functions, so I'm confused on how to send it to the js and then link the js file so the jsp knows about that function.

EDIT: Here's how I'm calling the above method.

current jsp (which is a modal dialog) has a button:

<button onclick="launchForm()"> </button> 

js:

launchForm= function() { var params=""; yuiPost("foo.do?subaction=ajaxShowTab", "content", params);}

So if I need to put it on the original jsp:

<script> var newTabUrl =${url}; window.open(newTabUrl); </script>

^That goes after the button?

Euridice01
  • 2,510
  • 11
  • 44
  • 76

1 Answers1

0

You can't do it server side. What you can do is return it as part of your original page (through JSP) as part of some Javascript snippet (maybe just <script> var newTabUrl = <your url> </script>)

And then you can use window.open() if you find the newTabUrl set to some value.

Open a URL in a new tab (and not a new window) using JavaScript

UPDATE

Well I doubt ${url} is even valid JSP. Are you sure you are not using some templating engine? If it were pure classic JSP it would be something like <% out.println(getUrl()); %>

The name of the javascript parameter you used for window.open() is also wrong.

<script> var newTabUrl = <% out.println(getUrl()); %>; window.open(newTabUrl); </script>

I don't know what you're doing, or from where getUrl() is coming from, so fix this according to your needs.

jbx
  • 21,365
  • 18
  • 90
  • 144
  • I edited my post. Can you confirm if I understood you correctly? I'm just wondering how does the original jsp page know about url? Actually, how do I get url from html to js script tag above? – Euridice01 Feb 01 '19 at 15:54
  • Updated a bit my answer, maybe it helps. Remeber the JSP is being generated serverside, so you might be able to use any Java function in there, as long as you want it to end up in the page (HTML or JS or CSS). – jbx Feb 01 '19 at 17:53
  • @jbx `${url}` is valid JSP since ~2.0, ~2013 or so. JSP EL expressions follow normal scoping lookup. `out.println` etc. has been unnecessary since well before that, it's why `<%= ... %>` exists, although scriptlets have been largely deprecated for a decade+ in favor of JSP tags and EL. – Dave Newton Feb 01 '19 at 20:23
  • @DaveNewton how do I resolve this? I am able to get the url using <%..%> as suggseted above but the scripts inside the script . tag don't get invoke when I go back to the page from the action above. – Euridice01 Feb 01 '19 at 21:03
  • @DaveNewton ok, good to know. Haven't used JSP since like 2006 now. :) Obsolete technology, which I never particularly liked. Modern approaches use templating (like thymeleaf), MVC (although you could say JSP could fit) and MVVM (angular etc). – jbx Feb 01 '19 at 21:29
  • @jbx JSP *is* templating, MVC is completely unrelated to view-layer *implementations* (which is what JSP is), and "obsolete" is in the eye of the beholder--it's more than adequate for a range of applications. – Dave Newton Feb 01 '19 at 21:47
  • @DaveNewton Yeah but most people do not use JSP as a view. They used it like PHP, a kitchen sink of crap. According to Oracle "JSP is considered to be a deprecated view technology" http://docs.oracle.com/javaee/6/tutorial/doc/javaeetutorial6.pdf (page 111). Personally I don't like the JSF approach either. I found the templating approach much cleaner, and separates what web designers maintain with their tools and controllers logic more cleanly, but yes it's my opinion. – jbx Feb 01 '19 at 22:26
  • @jbx My experience has been far different; around a decade ago people realized putting logic in JSP was generally awful and stopped doing it. Sure, people still do bad things, but nothing like it was 10-15 years ago. – Dave Newton Feb 01 '19 at 22:44
  • @DaveNewton sure, fair enough. I still think using things like Thymeleaf (it's transparent templating, so the html template actually renders like a real page in the browser) and Spring MVC controllers is far cleaner and superior. My opinion of course. I moved on from JSP over a decade ago and never looked back :) – jbx Feb 01 '19 at 22:53
  • @jbx Almost anything is better than JSP-but most Java houses don’t have in-house UX/design people, so the separation of HTML/JSP isn’t too relevant. *shrug* All I do is React/Vue/Elm now anyway, so doesn’t matter to me anyway. – Dave Newton Feb 01 '19 at 23:37
  • @jbx getUrl() and the ActionForward method above is java code not JavaScript.... so my question is I'm not sure how to reference the url in the JavaScript code (particularly when the JS code is in a separate file away from the jsp code/file.... so I can't use <%...%> tag. I'm still stuck on this issue. – Euridice01 Feb 04 '19 at 03:11
  • @Euridice01 what you put between <% %> is Java code. You will need to import the right package, if getUrl() is provided by some class, you didn't provide any details about that method do it don't know much else. You put Java code inside the tag, which is generated serverside. The generated HTML will have the actual URL instead. That's how `newTabUrl` will get initialised to the right URL. – jbx Feb 04 '19 at 03:32
  • @jbx, getUrl() is in another class - delegate class. So, I need to include import of that class and then add getUrl(). It's really something like foobar.getUrl() where FooBar is the delegate class. – Euridice01 Feb 04 '19 at 12:12
  • @Euridice01 Yes its just normal Java code. If it is not a static method, then somehow you must provide an instance of `FooBar` or initialise it somehow. You didn't include any other details about how you are rendering your JSP, whether you have a controller backing it through which you can inject the URL directly or an instance of `FooBar`. You could simply look at other places where you are rendering dynamic information in your JSPs, such as the user's username, or something. The concept is the same. – jbx Feb 04 '19 at 12:39
  • Like I know how to render it but I think this scenario is unique (as I need to use js to handle dynamic data from Java whereas I normally have it in the jsp). Maybe we should move to discuss thread to discuss further. – Euridice01 Feb 04 '19 at 14:36
  • Your scenario is not really unique. You are just getting a bit confused. Just remember, the browser will render whatever you send it, irrespective of whether it is plain HTML, CSS, or JavaScript. The JavaScript can be dynamically generated just like your HTML. Stop thinking about it magically interacting with JavaScript, it is just text on the HTML page. – jbx Feb 04 '19 at 14:47
  • @jbx The thing is when I add <%...%> inside of my js file, it doesn't recognize it needs to get the java value. When I add it to the jsp page, it recognizes it as proper syntax and renders the url. So... what do I do in this scenario? Why is it not being recognizing in the js file? – Euridice01 Feb 04 '19 at 15:19
  • The `.js` file is not a `.jsp` file. Tomcat (or whatever servlet container you are using) will only process JSP files. If you have a function in your `.js` file that you want to use, make it take a `url` as a parameter, and then from your JSP file, which gets the Javascript variable initialised from the JSP's dynamic value, and then calls your function from your .js file. – jbx Feb 04 '19 at 16:02
  • @jbx ok that's fine. I think the issue is more complicated in the flow and forwarding of the pages and when the url data is available. Maybe we can discuss in chat. Sorry to bother you. – Euridice01 Feb 04 '19 at 16:14
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/187872/discussion-between-euridice01-and-jbx). – Euridice01 Feb 04 '19 at 16:14