tomcat - Java converting Jar project to War Project -
i've built java program logs game server , asks user input id , sends packer game server , parses , prints out reply.
i need convert api run on tomcat assume? i've installed tomcat on server not sure , correct way convert be.
any appreciated.
i have done similar things in past. turn regular applications web applications. or more put, wrapping application inside servlet web application can controlled via http api.
by way there multiple ways this. many in fact. 1 way. servlets java interface allows developers create server, http server, though not limited that. tomcat servlet container. means can create servlet, register servlet container, either using special file called web.xml or annotations. in example below use webservlet annotation register servlet tomcat. once registered, tomcat send requests destined application (the name of war file) , specific servlet (the registered urlpatterns, see example below). if war file named "mywebserver", , servlet has registered urlpattern "getsomething", requests tomcat url mywebserver/getsomething, directed servelet's doget or dopost command. if simple browser request (or curl on linux) no msg body, default http request. stick requests until hang of it.
a couple of things need.
1) know how package java application war file. war file go tomcat's webapp folder. when tomcat starts unzip war files in directory , host web application implemented in war file. please read on war files. easiest way use ide, prefer netbeans, , click on new project. in netbeans can select new maven->web application project -or- java->web application. set directory structure.
2) need servlet. create servet, please read on it. once again ide make things easy you. in servlet 3.x can configure servlet using annotations. servlet httpservet. here simple example, have various components (like httpservletrequest , httpservletresponse) full blown details. stackoverflow has treasure trove of information on how 2 work.
@webservlet(name = "mywebserver", urlpatterns = {"/getsomething", "/postsomething"}, loadonstartup = 1) public class mywebserver extends httpservlet { @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //get input web service httpservletrequest. string someparam = request.getparameter("someparamname"); //process request string output = yourexistingclass.processsomething(someparam); //set response try (printwriter out = response.getwriter()) { out.println(output); } } @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { } /////////////////////////////////////////////////////////////////////////// //public functions @override public void init() { //put code want executed when application comes in here. } @override public void destroy() { //put code want executed when application comes down in here. } }
3) need include jar in application. once again ide can add jar project. jar, , other dependency jar's packaged inside war, inside web-inf/lib folder.
i hope helps started.
Comments
Post a Comment