Solved

JSP Redirection

Posted on 2004-08-03
10
4,348 Views
Last Modified: 2013-11-24
Hello everybody,

I'm working with servlets in a project. I have 5 servlets and I need the comunication between them. I am communicate them with the context and for initialize the servlets I 'm doing with the response.sendredirect(path).
The trouble that I have its that I need to redirect the servlet to a jsp. I tried with getServletContext().getRequestDispatcher("/SG_ConfigServCentral.jsp").forward(request, response); But I get an exception (java.lang.IllegalStateException: WEB2645: Cannot forward after response has been committed) and I tried with include instead of forward but didn't work too.

Can anyone help me please?

Thanks a lot
0
Comment
Question by:epadilla
  • 3
  • 2
  • 2
  • +1
10 Comments
 
LVL 7

Accepted Solution

by:
mark-b earned 34 total points
ID: 11709874
It does not sounds like you are using the request dispatcher incorrectly.  More than likely, this error message is correct in stating that you have committed the response already BEFORE your try to forward the request.

You should be able to test this by forwarding the request immeadiately (before you process any additional information) and returning.  If it works then you are indeed commiting the reponse somewhere where you should not be.

Good luck,

-Mark
0
 
LVL 1

Assisted Solution

by:wolfc
wolfc earned 33 total points
ID: 11709901
For me the following works on JBoss 3.2.2 (Tomcat 4.1):

<%@ page language="java" %>
<% response.sendRedirect(response.encodeRedirectURL("/index2.html")); %>

Make sure nothing (and I mean nothing) does something with the response before the redirect.

See Java(tm) Servlet Specification 2.3 FCS paragraph SRV.8.4
0
 

Author Comment

by:epadilla
ID: 11710486
Hello again,

Yes I did something with the response, because I did the response.sendRedirect(path of the other servlets), so my question is if I can redirect a jsp afterI worked with the response. Here is my code

public void doGet(HttpServletRequest request, HttpServletResponse response) throws
        ServletException, IOException
    {
        String stOption;
        int inOption;
        ServletContext scContext;
        HttpServletResponse htResponse;

        scContext = this.getServletConfig().getServletContext();

        stOption = request.getParameter("param0");
        inOption = Integer.parseInt(stOption);
       
        switch (inOption)
        {
            case 0:

                bsInitHostServlets(response);

                if (inServletsCounter == -1)
                {
                    asSpooler = (SG__Servlet) scContext.getAttribute("Servlet");
                    bosSpooler = (SG__Servlet2) scContext.getAttribute("Servlet2");
                    sb_InitCloseSockets();
                    tbThreadVector.inInterfaceServlet = asSpooler;
                    tbThreadVector.inInterfaceServlet2 = bosSpooler;
                }
                break;

            case 2:
                bs_voPrintSpooler();
                break;

        }
        response.setContentType(CONTENT_TYPE);

        PrintWriter out = response.getWriter();
       
        scContext.getRequestDispatcher("/jsp/SG_ConfigServCentral.jsp").include(
                request, response);


public void bsInitHostServlets(HttpServletResponse response) throws
        IOException
    {
        String stPath = "";

        stPath = bsReturnServletPath(inServletsCounter, true);

        if (stPath != "")
        {
            try
            {
                response.sendRedirect(stPath);
                inServletsCounter++;
            }
            catch (Exception e)
            {
                System.out.println(e.getMessage());
            }
        }
        else
            inServletsCounter = -1;
      }

public String bsReturnServletPath(int inServletNumber, boolean boAllSer)
    {
        String stPath = "";
        String stParam;

        if (boAllSer == true)
            stParam = "param0=0";
        else
            stParam = "param0=1";

        switch (inServletNumber)
        {
            case 0:
                stPath = "/sg_comms_servlet?" + stParam;
                break;

            case 1:
                stPath = "/sg_servlet1?" + stParam;

                break;

            case 2:
                stPath = "/sg_servlet2?" + stParam;

                break;
        }
        return stPath;
    }

Thanks a lot.
0
 

Author Comment

by:epadilla
ID: 11710560
Another comment, I am using the JBuilder and this exception only appear when I upload my project to iPlanet Web Server 6.0. Because when I compile in the JBuilder it works fine.

Thanks
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 92

Assisted Solution

by:objects
objects earned 33 total points
ID: 11710625
>  so my question is if I can redirect a jsp afterI worked with the response.

no, you cannot redirect to *anything* after writing to the response.

you appear to also process the response after a redirect has been done. You cannot do that.
0
 

Author Comment

by:epadilla
ID: 11710853

Well I need to do the following:

Servlet1 is the base of all my project, and I want that Servlet1 communicate with Servlet2 for initialize some proccess, and I need that Servlet2 return the control to Servlet1, I did it with parameters, like this:


SERVLET 1

response.sendRedirect(Path of servlet2?param=0);

SERVLET 2

response.sendRedirect(Path of Servlet1?param=0);

After that I need to display a JSP for the user.


Any other idea for try to do this??
0
 
LVL 92

Expert Comment

by:objects
ID: 11710941
doesn't sound like its a redirect you need, you cannot share processing between servlets.
once a servlet has redirected it should return.
0
 
LVL 1

Expert Comment

by:wolfc
ID: 11713011
At what line to you get the IllegalStateException, because after a sendRedirect you're not allowed to do anything as well. :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 81
solarwind tftp server 2 45
listing all functions in JavaScript 19 180
JavaScript/Java - Changing an image background color 4 56
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now