开发者

how to execute c++ code at server side using tomcat server?

I am a beginner in writing web application, so please co-operate if its a silly question. Our web application is hosted using tomcat 6. I have some C++ code to be executed in server when user click on corresponding button. Client side is written in html/JS and hosted using tomcat.

So, My problem is I dont know how this C++ code will be executed when a button is clicked in html page. Can anyone please help me?

[updated]

I can change from tomcat to any ot开发者_开发技巧her server but code has to be in c++. So if you have any other server(wamp or smthing) or links to do the same. Please let me know


Tomcat, a Java Servlet container is definitely not the most appropriate vehicle to execute C++ code in. You could try to use JNI to make a servlet run the C++ code, but it seems to me that there are much easier and reliable ways, like good old CGI's. Tomcat can do CGI, as explained here, with some limitations and restrictions.

Update: I think we can agree that the CGI route is the way to go. Most webservers allow you to run cgi's, and it will definitely be simpler than with Tomcat. I also suggest you delegate the work of connecting your code to the web server to a library, like gnu cgicc (nice tutorial here) or cgic. A plain old WAMP (you'll just use the WA part here) and that sample code should get you up to speed in no time. The rest will be pretty standard Web development.


https://stackoverflow.com/questions/175507/c-c-web-server-library answers may well help you out.

Given that Tomcat is no longer a requirement, using a different http front end may well make your life easier.

If you do decide to use Tomcat Which C++ Library for CGI Programming? may help you pick a library.

Barring that, if you use Apache, you can write a plugin module itself, instead of CGI, which will give you much better performance. (Other web servers generally have similar plug-in methodologies also...)

Good Luck


I'm not sure any of these answers addressed the question. Coding a CGI using C++ would mean reading environment variables that are set by the web server, regardless of whether or not you use a third party library or which web server is run, including tomcat. The following example is a quick-and-dirty way to grab the most interesting input, the query string. If you are starting out, it's I think better to start with basics so if you decide to use some sort of external library it will seem less mystical. This should give you enough to hit google and work out what's happening.

#include <stdlib.h>
#include <iostream>

using namespace std;

int
main(int argc, char** argv)
{
    string method = getenv("REQUEST_METHOD");
    string query;
    if (method == "GET")
        query = getenv("QUERY_STRING");
    else if (method == "POST")
        cin >> query;
    else
        query = "Not sure what to do with method " + method;

    cout << "Content-Type: text/html" << endl << endl
         << "<html>" << endl
         << query << endl
         << "</html>" << endl;
}

Note Content-Type in the output. That's a HTTP header. You can add any number of headers before the double endl. For a light bulb moment try changing Content-Type to text/plain.

Compile the example code to shiney_cpp_cgi, copy it to your cgi dir (for tomcat that's generally tomcat_root/webapps/ROOT/WEB-INF/cgi), then hit it with your browser as such to use the GET method:

myserver.mydomain:myport/cgi-bin/shiney_cpp_cgi?foo=bar

To send in a post request, use CURL as such:

curl --data 'foo=bar' myserver.mydomain:myport/cgi-bin/shiney_cpp_cgi

To serve C++ from tomcat, you can edit tomcat_root/conf/web.xml and change executable to an empty string. By default, tomcat will try to run your C++ as a perl script, which perl will (hopefully!) not be able to parse.

<servlet>
    <servlet-name>cgi</servlet-name>
    <servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
    ...
    <init-param>
        <param-name>executable</param-name>
        <param-value></param-value>
    </init-param>
    ...
</servlet>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜