Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

response.sendRedirect is not working

 
Prateek Sharma
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Prateek Sharma
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic