Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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 Android Security Essentials Live Lessons this week in the Android 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

Prateek Sharma
Greenhorn

Joined: Jul 20, 2008
Posts: 23
response.sendRedirect("login.jsp");

this code is not working, its giving an exception "java.lang.IllegalStateException null"
what might be the reason and how to avoid it..

I am trying to using sendRedirect on my first line of JSP Page but its not working,

here is the sample code


<%
if(session.getAttribute("logonForm")==null || session.isNew()){
response.sendRedirect("login.jsp");
}
%>
<%@ page language="java" import="java.sql.*,java.util.Hashtable,java.util.Date,java.text.DateFormat,java.text.SimpleDateFormat" %>
<%@ taglib uri="struts-html" prefix="html" %>
<%@ taglib uri="struts-bean" prefix="bean" %>
<%@ taglib uri="struts-logic" prefix="logic" %>
<%

if(session.getAttribute("dateBefore30Days")==null){

DaoService ds2 = new DaoService();
ResultSet rs2;

try{...............
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

I'm not sure exactly what's causing your exception, but rather than checking for a logged in session at the start of each JSP page why aren't you using a servlet filter?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Prateek Sharma
Greenhorn

Joined: Jul 20, 2008
Posts: 23
Hi Bear, Thank for replay,

But can you please tell me how can I use Servlet Filter as I am using Struts Framework. If U provide me these detail that would be a great help of me.........
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

Originally posted by Prateek Sharma:
If U provide me these detail

Please use real words when posting to the forums. Abbreviations such as "U" in place of "you" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please read this for more information.

thanks,
bear
JavaRanch sheriff
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

Look up servlet filter. You can configure one to be called prior to any protected page in your site by associating with a specific servlet mapping (*.do for example). The filter can do the session checking so that you only have to write it once and not worry about it in any of your servlets, actions or JSPs.
[ July 20, 2008: Message edited by: Bear Bibeault ]
kelby zorgdrager
Greenhorn

Joined: Feb 05, 2008
Posts: 12
without seeing your entire jsp it's kind of hard to say why you are getting this exception.

that being said, the most common case is you are performing some work on the output stream after the response header is sent back.

in your code, right after your response.sendRedirect("...") add a return statement. the return will "stop" the processing of the jspService method, preventing any further manipulation of the output stream by the jsp to occur.

if this solves the problem, then you need to go back into your code and change the logic so that after you call sendRedirect the flow control stops naturally.


<a href="http://www.J2EETraining.com" target="_blank" rel="nofollow">J2EE Training / Java EE Training ... Learn Java EE </a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: response.sendRedirect is not working
 
Similar Threads
File is Not Displaying
stratup Exception
Tag libraries
"WEB-INF/lib/struts-html.tld" not Found
struts forward link?