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.
Java EEJava

Avatar of undefined
Last Comment
reach2piyush

8/22/2022 - Mon
thomas908

is your log printing line

 Next line here..

in your POST method
reach2piyush

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);
                 }
}
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?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
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?
reach2piyush

/* 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;" />

leonidn

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....
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
reach2piyush

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question