Link to home
Start Free TrialLog in
Avatar of slavikn
slavikn

asked on

response.sendRedirect

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.
Avatar of thomas908
thomas908

is your log printing line

 Next line here..

in your POST method
The Javascript code used is meant to be used with AJAX technology, wherein the same page is interested in getting some data without refreshing the page, the data written on the response stream is available to the javascript method specified in ''onreadystatechange'' .your case handle method..

For the above code to work, instead of using javascript, use a simple button in your html file & submit the page
<form name="form1" method="POST" action=url >

In case you are actually interested in testing the javascript function, try changing the Servlet code to something like
      PrintWriter out = response.getWriter();
      response.setContentType("text/xml");  
                out.print("SEE THIS DATA IN PAGE");

and in your javascript , write your handle method as :
 if (req.readyState == READY_STATE_COMPLETE) {
       if (req.status == HTTP_OK) {
                     alert(req.responseXML);
                 }
}
Avatar of slavikn

ASKER

> For the above code to work, instead of using javascript, use a simple button in your html file & submit the page
> <form name="form1" method="POST" action=url >

But I also want to pass some parameters (xml) to the servlet when calling it.
How can this be done?
Avatar of slavikn

ASKER

By the way, by "xml" I mean a string (it has the form of an xml, but it doesn't matter).

Also, I want to call the servlet when a link is clicked (don't want any buttons on my page). Is this doable?
/* Function to submit the form */
function submitForm(action)
{
     var form = document.form1;

     form.action = action;
     form.method = "POST";      
     form.submit();
}

And to call this from a link
<A id='link1' href='javascript:submitForm(url)' />Submit Form</A>

To pass parameters define a hidden parameter inside your form
<input type="hidden" name="params" value="&gt;xml&lt;&gt;/xml&lt;" />

isn't there a "neater" way to do it (without any forms). it looks really weird that in order to ask the servlet to open a certain page i need to create a hidden form....
ASKER CERTIFIED SOLUTION
Avatar of reach2piyush
reach2piyush
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial