• Post Reply Bookmark Topic Watch Topic
  • New Topic

importing classes into a function  RSS feed

 
Michael ness
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
I am declaring a JSP function as follows
<%!
void a_function()
{
response.sendRedirect("somewhere.jsp");
}
%>
My problem is I get an error variable response not found in class ....
Does anyone know how I can import classes so they can be used within the function.
Thanks.
 
Chris Baron
Ranch Hand
Posts: 1061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,
i didn't test it, but...
void a_function( HttpServletResponse res )
{
res.sendRedirect("somewhere.jsp");
}
...should work if you call it with:
a_function( response );
cb
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In general this is not recommended, since a sendRedirect (and forward, for that matter) will only work if the response has not been comitted. This means you should not send any data to the client before attempting either of these calls.
In theory you can rely on the buffer to prevent the response from committing until you are ready, but in practice it is not something I like.
Dave
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris' solution should work (and David's suggestions are right on the money regarding using redirects). The reason your function didn't work is that you are relying on the implicit request variable to be defined within the body of your method.
The implicit variables (request, respose, and so on) are only defined for the body of the JSP (that part that ends up being invoked by the service() method of the JSP servlet). Any Java that you delcare using the <%! mechanism is added to this servlet at the class level; be they variable or method declarations.
hth,
bear
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. I make this suggestion often, but I think it is worthwhile: find the corresponding Java file generated by your container for the JSP page and study how your JSP is transformed into its corresponding servlet. There is a world of useful information on the how-and-why's of JSP to be learned.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
P.S. I make this suggestion often...

Absolutely.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!