Link to home
Start Free TrialLog in
Avatar of aka_damo
aka_damo

asked on

Redirecting To New Browser Window

Hey,
I have a jsp website that is redirecting to another web page with

response.sendRedirect("another_page.jsp");

As it is, it just goes to this new page but I want it to open up in a new browser. Help greatly appreciated

aka_damo
Avatar of bloodredsun
bloodredsun
Flag of Australia image

I think you'll have t use Javascript for that along the lines of:

<SCRIPT type="text/javascript" >
    window.open('popup.html', 'My title', config='height=300,width=300')
</SCRIPT>

but many people have new windows turned off in their Javascript so you might just have to use

<a href="popup.html" target="new">Click here</a>
Avatar of aka_damo
aka_damo

ASKER

I don't think that will work because the way I've it set up in a way that the page that is interacting with the public has a html form in it calls another jsp page that updates the database and then redirects to the new page using

response.sendRedirect("another_page.jsp");

so there won't be any opportunity for a user to click
Using JavaScript doesn't require a click but if you use a RequestDispatcher rather than sendRedirect, you can put a flag in the request which then tells the page you are linking to whether to spawn a new window or not.
If you are sure that a new window will always be opened. Then you can open the window first before submitting the form.

e.g.

<input type="button" onclick="doSubmit(this.form)" value="submit">

function doSubmit(aForm){
  window.open("about:blank","my_new_win");
  aForm.target="my_new_win";
  aForm.submit();
}

bloodredsun,
how would I use this RequestDispatcher, it sounds like it should work
in your redirecting page

redirector.jsp
-----------------
request.setAttribute("flag" , "true");//set variable in request
RequestDispatcher rd = new RequestDispatcher("base.jsp") ;//an example page
rd.forward(request, response);//forward request and response onto the specified page

base.jsp
-----------------
<%
String strFlag =(String) request.getAttribute("flag"); //get our variable back
if ( strFlag.equals("true")){
    //if it's true output the javascript otherwise ignore this section
%>
    <SCRIPT type="text/javascript" >
        window.open('popup.html', 'My title', config='height=300,width=300')
    </SCRIPT>
%}%>

This means that you can send the user back to a normal page but only when there is a variable in the request called flag that equals true will the pop-up window appear.


within the redirector.jsp, you can use  

<jsp:forward page="base.jsp">

directly which performs the RequestDispatcher and forward.

In base.jsp, you should change the checking from
    if (strFlag.equals("true")
to
   if ("true".equals(strFlag))

Otherwise, you will get a NullPointerException if the attribute is not set.

At any rate, the problem with this approach is that you will not be able to pass form data into this popup.html. e.g. If you are submitting some form data and performing some processing in redirector.jsp and you want to have some confirmation message appear in the new window with the original information available, you cannot do so unless you specify all the parameters in the window.open() call. e.g. window.open("popup.html?xxx=yyyy&zzzz=000") etc.

If you want to be able to show the submitted form data, you should open the window first, then set the target of your form as I've illustrated in my previous comment, unless there is server side logic involved which determines whether a new window should be opened or not, in such case, you may not want to always open a new window before submitting.

if you still want to keep the form data in the above scenario, your base.jsp could parse the request object and reproduce the original form, then open a new window and submit the form to it using javascript.
I'm not sure if you understand my problem.
I have a jsp page taking form details from a user.
When this is submitted another jsp page is called that writes data to a database.
After the data is written I want to open a browser and direct it using

response.sendRedirect("http://www.vgsmail.com/cgi/sendsms.cgi?userid=user&password=password&to="+sms+"&text=Expense+needs+approval");

As it is when I redirect, it just brings the browser to the above url. The url lets me send an sms using vgsmails service. I want to be able to call this url behind the scenes preferably or if not just in a new browser. There is just one piece of data that's coming from the original form, the variable sms
ASKER CERTIFIED SOLUTION
Avatar of Kuldeepchaturvedi
Kuldeepchaturvedi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yep, you're right about the possible null pointer exception and also about the myriad ways than example could be explained but it was just somehting to demonstrate the underlying principles :-)
I wd have considered a split. After all I just expanded what Bloodredsun was suggesting...