• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

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
0
aka_damo
Asked:
aka_damo
  • 4
  • 3
  • 2
  • +1
1 Solution
 
bloodredsunCommented:
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>
0
 
aka_damoAuthor Commented:
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
0
 
bloodredsunCommented:
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
hintCommented:
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();
}

0
 
aka_damoAuthor Commented:
bloodredsun,
how would I use this RequestDispatcher, it sounds like it should work
0
 
bloodredsunCommented:
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.


0
 
hintCommented:
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.
0
 
aka_damoAuthor Commented:
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
0
 
KuldeepchaturvediCommented:
you can use bloodred's solution for this quite easily..

say 1.jsp is where you take the data from the user,
2.jsp writes in to the database..
once 2.jsp finishes its task you may want to display
3.jsp saying the data is updated and also pop up a window for this sms thingi...

( 3.jsp can be 1.jsp as well if you want..)

code in 2.jsp..

<jsp:forward page="3.jsp">
<jsp:parm name="newwin" value="Y"/>
</jsp:forward>

in 3.jsp.

<%
String newwin=request.getParameter("newwin");
if("Y".equals(newwin)
{%>
<SCRIPT type="text/javascript" >
        window.open(''http://www.vgsmail.com/cgi/sendsms.cgi?userid=user&password=password&to=<%=request.getParameter("sms")%>&text=Expense+needs+approval")";, 'My title', config='height=300,width=300')
    </SCRIPT>
<%
}%>

//rest of the page...

0
 
bloodredsunCommented:
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 :-)
0
 
KuldeepchaturvediCommented:
I wd have considered a split. After all I just expanded what Bloodredsun was suggesting...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now