troubleshooting Question

response.sendRedirect

Avatar of slavikn
slavikn asked on
JavaJava EE
7 Comments1 Solution966 ViewsLast Modified:
Hi,

I want my servlet to redirect to another page. If I do it from doGet it works. Same code in doPost doesn't work. Why??

To call the servlet with doGet I just go to the address in an Internet browser.
To call the servlet with doPost I use an html file with a small java script.

###################################################################

Here is the code of the servlet (java-script's code follows):

public class HelloWorldExample extends HttpServlet {
    private static final long serialVersionUID = 1L;
   
    public static PrintWriter logFile = null;
   
    static {
        try {
            logFile = new PrintWriter(new FileOutputStream("log.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
   
    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);
    }
   
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        logFile.println("New requst (GET):");
        logFile.println("   Time: " + new Date());
        logFile.flush();
        response.sendRedirect("http://www.mignews.com");
        logFile.println("   Next line here...");
        logFile.println();
        logFile.flush();
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        logFile.println("New requst (POST):");
        logFile.println("   Time: " + new Date());
        logFile.flush();
        response.sendRedirect("http://www.mignews.com");
        logFile.println("   Next line here...");
        logFile.println();
        logFile.flush();
    }
}

###################################################################

Here is the code of the java-script (url, handle and message are parameters):

    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.open("POST", url, true);
    req.onreadystatechange = handle;
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    req.send(escape(message));

###################################################################

By the way, if both lines are printed to the log file by bot doGet and doPost.



Any help will be very much appreciated.
ASKER CERTIFIED SOLUTION
reach2piyush

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros