Redirect forward include - novalexei/mod_servlet GitHub Wiki

Redirect, Forward, Include

Sometimes we cannot do all the work in the servlet, we need some help from other resources. Here is how we do it:

1. Redirect

If we want to completely redirect the request to other URL, we can just call http_response::send_redirect

resp.send_redirect("https://www.google.com/search?q=evil+clowns");

Unfortunately, Apache server is trying to be smart about it and it generates "friendly" messages instead of sending unaltered response to the client, so this call will result a small page with the explanation and link to the redirected URL. If you know how to make Apache2 to stop doing this annoying thing let me know.

2. Forward

Sometimes we just think that other servlet will do the requested job better, so we can forward the request to other URL interanally within the same server without even letting the client know. http_request::forward does exactly that.

3. Include

We can also make the response combining multiple resources of our web application. Those "other resources" can be both static and dynamic (other servlets). Call to http_request::include does this.

Now, short demonstration. Let's write a servlet, which first forwards to itself on different URL and while processing forwarded request, includes a static file.

First create include.html file with this content:

<p>Included file. Ta-daaaa!</p>

or some other useful information. Copy this file to the root of your web application directory (so far it was tutorial in webapp root). To the servlet now:

class forward_servlet : public http_servlet
{
public:
    void do_get(http_request& req, http_response& resp) override
    {
        const URI &uri = req.get_request_uri();
        auto path = uri.path();
        // Check if this is forwarded request
        auto found = path.rfind("/forwarded");
        if (found ==URI::string_view::npos || found + 10 != path.length())
        {
            // If it is not forwarded, than forward.
            req.forward("/forwarded");
            return;
        }
        resp.set_content_type("text/html");
        std::ostream &out = resp.get_output_stream();
        out << "<!DOCTYPE html>\n"
               "<html>\n"
               "<head>\n"
               "<title>Forward and Include demonstration</title>\n"
               "</head>\n"
               "<body>\n"
               "<p>My current URL is " << uri << "</p>\n"
               "<p>We now going to include file... Drum roll!</p>\n";
        // Include file
        req.include("/include.html");
        out << "<p>Trumpets go wild and stuff</p>\n"
               "</body>\n"
               "</html>\n";
    }
};

SERVLET_EXPORT(forwardServlet, forward_servlet)

Note that in web.xml you need to configure the servlet so that it would service both it's original URL and also same URL with /forwarded suffix. If you map it to url-pattern / or /* that'll do. Check servlet-mapping reference for more information.

Well, this is it. We just tried request forwarding and file including in one servlet.

Let's go on to HTTP status codes and error handling.

To The Programming Guide

⚠️ **GitHub.com Fallback** ⚠️