• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

response.sendRedirect is not working

 
Maria Peter
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please define "doesn't work"
 
Maria Peter
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Posts: 452
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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">
 
Maria Peter
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 452
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 452
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 452
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/products/servlet/Filters.html
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one bookmark about the tutorial in Filter... It's from Orion...Here it is...
http://www.orionserver.com/tutorials/filters/
 
Maria Peter
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much I really appreciate it
Maria
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic