Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2007

Tip: Looking for answers? Try searching our database.

JSP/sendRedirect() problem...

Thread view: 
maya - 04 Jan 2007 15:27 GMT
I have a response.sendRedirect() in a JSP, to be triggered if a param
passed in request evaluates to "" or null.. however, sendRedirect is
being ignored; also tried <jsp:forward..>, is also ignored..  would
appreciate some help..  thank you..   code is as follows:

<%
theSponsor = request.getParameter("sponsor");
if (theSponsor.equals("") || theSponsor == null) {
    %>
         // following test alert triggers fine, the rest is ignored..
    <script language="JavaScript" type="text/javascript">
    alert("test");
    </script>

    <%
    //  following ignored...
    response.sendRedirect("/ws/sponsors/index.jsp");
    %>

    <%--    // following also ignored..
    <jsp:forward page="/ws/sponsors/index.jsp" >  --%>
    <%

    return;
}  else  if (!theSponsor.equals("") || theSponsor != null) {
%>

   <%-- code for a custom tag here, using "theSponsor" var as one of
the parameters inside the tag...  --%>
<% } %>

thank you very much...
Paul - 04 Jan 2007 17:12 GMT
Do you have any content being sent to the user before this code gets
executed?

> I have a response.sendRedirect() in a JSP, to be triggered if a param
> passed in request evaluates to "" or null.. however, sendRedirect is
[quoted text clipped - 28 lines]
>
> thank you very much...
maya - 04 Jan 2007 17:45 GMT
thank you for your response, Paul...   this is actually inside a struts
tile..   entire code for the tile is (directives omitted):

===========================
<%
String theSponsor = "";
%>
<table><tr><td><img src="..."></td></tr><tr><td>

    <%
    theSponsor = request.getParameter("sponsor");
    if (theSponsor.equals("") || theSponsor == null) {
        response.sendRedirect("/ws/sponsors/index.jsp");
        %>
        <script language="JavaScript" type="text/javascript">
        alert("param has no value");
        </script>
<%--        <jsp:forward page="/ws/sponsors/index.jsp" >  --%>
        <%
        return;
    }  else  if (!theSponsor.equals("") || theSponsor != null) {
    %>

<%-- CUSTOM TAG (to include param in question..) HERE... --%>
   
<% } %>

</td></tr></table>

===========================

thank you very much..

> Do you have any content being sent to the user before this code gets
> executed?
[quoted text clipped - 31 lines]
>>
>> thank you very much...
Aneesh - 05 Jan 2007 09:58 GMT
Can you print something inside the condition, like:

 if (theSponsor.equals("") || theSponsor == null) {
        System.out.prinln("Entered condition 1 ");
        response.sendRedirect("/ws/sponsors/index.jsp");
  %>

so that we can check if it really enters the condition.

Best Regards
Aneesh

> thank you for your response, Paul...   this is actually inside a struts
> tile..   entire code for the tile is (directives omitted):
[quoted text clipped - 64 lines]
>>>
>>> thank you very much...
Andrew Thompson - 05 Jan 2007 10:46 GMT
Methinks the confusion in this thread is being compuonded
by the top-posting style of each of the respondents, but note that..

>   if (theSponsor.equals("") || theSponsor == null) {

If theSponsor *is* null, this should throw an NPE
before it hits the second test.

I might suggest instead..
  if ( theSponsor == null || theSponsor.equals("") ) {

..or another alternate..
 if  ( theSponsor == null || theSponsor.trim().equals("") ) {

Andrew T.
maya - 05 Jan 2007 15:52 GMT
> Methinks the confusion in this thread is being compuonded
> by the top-posting style of each of the respondents, but note that..
[quoted text clipped - 11 lines]
>
> Andrew T.

hi Andrew, long time no talk, this is Frances, posting under a different
 name (for various reasons..  all innocuous ...;)

first of all, I did change my if-clause to your above line,
 if (theSponsor == null || theSponsor.trim().equals("") ) {
thank you..  (I had forgotten that null-test should always go first..)

it turns out that, just as I thought, this problem has to do w/tiles and
variable scope; don't know if you know struts/tiles, I just learned,
after talking to a knowledgeable java guy here, that each tile gets
converted into a different servlet, which raises a whole lot of
variable-scope issues (since then presumably there is a different
jsp_service() method for EACH TILE...)

what I ended up doing:

in controller page (page that calls the tiles, don't know if this
correct terminology, this is what we call them here):

<%
  String theSponsor = "";
  theSponsor = request.getParameter("sponsor");   
   if (theSponsor == null || theSponsor.trim().equals("") ) {
    response.sendRedirect("/ws/sponsors/index.jsp");
}
%>

tiles-code here...

in tile itself then, had to declare a new var to grab same param but
with a different name, thus:

<%
  String theSponsorA = "";
  theSponsorA = request.getParameter("sponsor");
  if (theSponsorA != null || !theSponsorA.trim().equals("") ) {
    %>

    CUSTOM-TAG HERE, with var above as a param-value..

<% } %>

hope this makes sense, at any rate this is what worked...

thank you for your help..
maya - 05 Jan 2007 16:11 GMT
>> Methinks the confusion in this thread is being compuonded
>> by the top-posting style of each of the respondents, but note that..
[quoted text clipped - 57 lines]
>
> thank you for your help..

btw, whole reason I had to do what I ended up doing is that java guy
here told me redirects don't work in tiles, only in controller pages..

I have a WHOLE LOT of struts still to learn (as this is all we do here,
different job, started here last August..)
Lew - 06 Jan 2007 02:56 GMT
>> <%
>>   String theSponsor = "";
[quoted text clipped - 5 lines]
>
> I have a WHOLE LOT of struts still to learn (as this is all we do here,

To start with, you need to learn to let Struts handle the navigation instead
of hard-coding it into the JSP.

The whole point of Struts is that it implements the Model-View-Controller
(MVC) paradigm, also called "Model 2 architecture" by Sun. The controller
servlet should handle *all* navigation, not the JSPs. JSPs are supposed to be
for view only, no model code, no navigation.

- Lew


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.