File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes  response.sendRedirect is not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark " response.sendRedirect is not working" Watch " response.sendRedirect is not working" New topic
Author

response.sendRedirect is not working

Maria Peter
Greenhorn

Joined: Oct 27, 2003
Posts: 29
I'm doing page that redirect users to a login page when:
session is null
<%@ page import="java.io.*,test.session.*"%>
<%
TestSession testSession = (TestSession) session.getAttribute ("INFO");

if(testSession != null)
{
-------------
}
else {


String url = request.getScheme() + "://" + request.getServerName() + ":" +
request.getServerPort()+ "/Test/jsp/login.jsp";

response.sendRedirect(url);
}

%>

But this doesn't work. Then i also tried to use forward

<%@ page import="java.io.*,test.session.*"%>

<%
TestSession testSession = (TestSession) session.getAttribute ("INFO");

if(testSession != null)
{
-------------
}
else {

%>

<jsp:forward page="/jsp/login.jsp">
<jsp aram name="param1" value="value2"/>
</jsp:forward>

<%
}

%>
But even this one is not working. Am i missing something here
Guru's Please help

Thanks in advance
Maria
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61450
    
  67

Please define "doesn't work"


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Maria Peter
Greenhorn

Joined: Oct 27, 2003
Posts: 29
It doesn't work in the sence it doesn't forward request to the
login page. If i try to access one of the jsp page without loging in(using login.jsp)
it just displays the banner and nothing happens after that.
Maria Peter
Greenhorn

Joined: Oct 27, 2003
Posts: 29
I tried this
response.sendRedirect(url);

System.out.println("TEST");
it prints "TEST" on the console but the request is not forwarded to
login.jsp.
Correct if i am wrong
I was expecting after calling response.sendRedirect(), the
code after it will NOT be executed. But looks like that is not true.
Is there any other way to forward the request to login.jsp page if
session is null.
I really appreciate your help
Maria
dee kal
Greenhorn

Joined: Oct 08, 2003
Posts: 10
Hi Maria,
I think the problem is with u'r TestSession.I don't see anything wrong with u'r sendRedirect() or <jsp:forward>.
bye.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61450
    
  67

I was expecting after calling response.sendRedirect(), the
code after it will NOT be executed. But looks like that is not true.

Correct, it is not true. What the sendRedirect will do is to set the response headers in such a way as to cause the browser to request the redirect URL. That is why it is illegal to call sendRedirect after a buffer flush -- it's too late at that point to set headers.
Maria Peter
Greenhorn

Joined: Oct 27, 2003
Posts: 29
My “testSeesion” is working properly.
Because If I tried to access any other jsp page without logging
in through login.jsp page testSession returns null and
program control goes to else block.
it executes all the statement in the else block.
But the request is not getting forwarded to login page.
I am using a header.jsp file in every jsp
which has following snippet of code in it

<%@ include file="/jsp/import.jsp" %>

<jsp:include page="/jsp/bannerPage.jsp" flush="true"/>
<jsp:include page="/jsp/testSecurity.jsp" flush="true"/>
testSecurity.jsp page contains the code from my previous
posting.
Is there any way I can make this one work.
Please help me
Ola Agiv
Greenhorn

Joined: Feb 05, 2004
Posts: 5
hi,
try this javascript code
instead of resonse.sendRedirect():
<% some jsp code %>
<script>window.location.href='http://someurl';</script>
<% some jsp code %>
or try put return word after resonse.sendRedirect():
<%
resonse.sendRedirect("someurl");
return;
%>
good luck
[ February 29, 2004: Message edited by: Ola Agiv ]
Maria Peter
Greenhorn

Joined: Oct 27, 2003
Posts: 29
<script>window.location.href='http://someurl';</script>
I used this code with absolute path(http://localhost:8080/test/login.jsp' and working now.
Thank you so much for your help. I really appreciate it.
can you please tell me how can i use this java script for relative URL
<a href=<%= request.getScheme() + "://" + request.getServerName() + ":" +
request.getServerPort() %>/test/login.jsp>


Thank you once again
Maria
Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
i will advice you to use jsp:forward, its better and faster.
As you have mentioned earlier that jsp:forward is not working in your jsp.
Here is how you have called jsp:forward
<jsp:forward page="/jsp/login.jsp">
And here is where your jsp is situated
http://localhost:8080/test/login.jsp
I think you should be calling
<jsp:forward page="/test/login.jsp">


Prakash Dwivedi (SCJP2, SCWCD, SCBCD)
"Failure is not when you fall down, Its only when you don't get up again"
Maria Peter
Greenhorn

Joined: Oct 27, 2003
Posts: 29
My login.jsp is under /test/jsp/login.jsp
In my previous posting by mistake i typed /test/login.jsp.
Thank you for pointing out the error.
As much as i would love to use jsp:forward or response.sendRedirect
in my jsp to forward the request back to to login page
it is not working. May be because i am using
<%@ include file %> in my jsp.
Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
My login.jsp is under /test/jsp/login.jsp
And i hope test folder is directly under root directory.
So the directory structure should be
root/WEB-INF/web.xml
root/test/jsp/login.jsp
If this is correct, you should use
<jsp:forward page="/test/jsp/login.jsp"/>
And if this is not working as well, check whether you are writing something to the output stream, before calling forward. Check for the stacktrace at server.

As much as i would love to use jsp:forward or response.sendRedirect
response.sendRedirect is not a very good option. Try to make jsp:forward working. In response.sendRedirect your response goes back to the client' browsser, from there it s redirected to login.jsp. This extra trip makes response.sendRedirect slower.
Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
Sorry, but i missed a very important information, you provided.
it just displays the banner and nothing happens after that.
How is this banner displayed?
If i am not mistaken this is displayed by <%@ include file %> in your jsp.
Which means your response has already been committed, and you can not use, jsp:forward or sendRedirect.
Also as you are writing all the code in scriplets, i think you are using Model 1 architecture and not MVC. In this case you are left with two options to achieve your goal.
1. Use javascript. Not something which i will recommend.
2. Use Filter. This is a good option according to me. rather than checking for session in your jsp, you can check it in filter. It is very easy to develop and configure. Also you dont have to change your existing code.
Maria Peter
Greenhorn

Joined: Oct 27, 2003
Posts: 29
Thank you so much for your response
I am not sure how to use filter. Can you please direct me to any tutorial or website
Thanks
Maria
Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
http://java.sun.com/products/servlet/Filters.html
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
I have one bookmark about the tutorial in Filter... It's from Orion...Here it is...
http://www.orionserver.com/tutorials/filters/


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Maria Peter
Greenhorn

Joined: Oct 27, 2003
Posts: 29
Thank you so much I really appreciate it
Maria
 
 
subject: response.sendRedirect is not working