How to setup GWT for Tomcat?

前端 未结 4 541
傲寒
傲寒 2021-02-14 08:43

I am new to GWT and learning it via the the Google documents. How do I setup GWT with Tomcat? I am required to intergrate it with Tomcat for work purposes.

相关标签:
4条回答
  • 2021-02-14 08:56

    There is nothing special to do. You just have to compile your gwt project, and export the war folder to the Tomcats webapps folder.

    What IDE are you using? If you are using eclipse or similar, and a newer version of tomcat, just export the contents of the war folder as a regular zip archive. Then change the extension from .zip to .war, and copy it to the webapps folder. Restart tomcat, and it will automatically unzip the archive and create the appropriate folder. I.E. the contents of the .war archive might look something like the following:

    project_name.war
       - css
       - images
       - WEB-INF
       - gwt compiled_javascript folder
       - index.html
    
    0 讨论(0)
  • 2021-02-14 09:01

    A more convenient method to run GWT in Development mode on external tomcat server is as follows. It involves two parts

    • Point tomcat to war dir of Gwt project in workspace
    • Run GWT in development mode on external server

    I will be using following paths and names in this example

    • Web App Name - Fins
    • Project location - $HOME/workspace/fins
    • Project war directory - $HOME/workspace/fins/war
    • CATALINA_HOME - $HOME/apache-tomcat-7.0.29 (tomcat installation dir)

    First step is to point tomcat to war dir of GWT project. One method to run GWT on external server is to copy static, image files and gwt dir to tomcat/webapps dir. On any changes in server side classes (like rcp etc) or static files we have to copy them again. This will be cumbersome during develpment cycle. Instead we can point tomcat to project's war directory so that tomcat runs the app directly from eclipse workspace. On any changes in project tomcat will do a reload.

    To do this, add <appname>.xml (Fins.xml in this example) to $CATALINA_HOME/conf/Catalina/localhost with following content

    <Context path="/Fins" docBase="/home/m/workspace/fins/war" reloadable="true"> </Context>
    

    This is actually context.xml file found in META-INF of tomcat application but named as <appname>.xml. In case project use any JNDI datasources, they have to be added to this file.

    • DocBase attribute points to project's war dir in eclipse. With this tomcat is able to run the web app directly from eclipse workspace without copying files to tomcat/webapps dir.
    • Reloadable attribute ensures that tomcat reloads the app whenever files are modified in eclipse.

    Now start tomcat. Check that application runs properly. Make some change in eclipse and app will be reload by tomcat. Cross verify the same in tomcat logs.

    Now to second part. We can use GWT code sever feature as detailed in GWT Debug

    To do this go to "Run As" option in project context menu and select "Web Application (Running on external server)". Enter external server root as Fins and give html page as Fins.html. This will run the GWT app in development mode without running embedded Jetty server.

    But it will still point to http://localhost:8888/Fins/Fins.html. We have to edit Run configuration to change Jetty port 8888 to tomcat's 8080.

    Go to run configurations and select Fin.html (external). Change browser field in GWT tab to http://localhost:8080/Fins/Fins.html


    enter image description here


    Run and access the app at

    http://localhost:8080/Fins/Fins.html?gwt.codesvr=127.0.0.1:9997

    Now you will be able to use GWT development mode fully.

    0 讨论(0)
  • 2021-02-14 09:05

    I use GWT-SDK webAppCreator command (from terminal) and it generates all the files required to run on an external server like Tomcat.

    I will provide the steps i followed in order to help others begin with simple GWT-TOMCAT development

    TOMCAT

    1. Download a Tomcat version, i prefer Tomcat 6 http://apache.tsl.gr/tomcat/tomcat-6/v6.0.32/bin/apache-tomcat-6.0.32.zip
    2. Follow the instructions for your specific operation system in order to install it. I followed this tutorial (MAC-OS) http://www.malisphoto.com/tips/tomcatonosx.html

    GWT

    1. Download a GWT-SDK version (i use 2.0.4 which i find more stable and compatible with external sources like EXT-GWT library and TOMCAT) http://code.google.com/p/google-web-toolkit/downloads/list
    2. Open your TERMINAL if you are a UNIX user then change directory to the GWT-SDK you just downloaded
    3. Change permissions to run webAppCreator script. I usually modify permissions of all files by typing - > chmod 750 *
    4. Create a GWT project by executing in terminal -> ./webAppCreator -out MyProject com.myproject
    5. All files are automatically generated
    6. Switch directory to the project you just created, type in terminal -> cd MyProject
    7. Compile your project with the ant script provided by the GWT-SDK, type in terminal -> ant build
    8. Open you project and copy it's build.xml file, war/ contents to Tomcat's webapps/ROOT folder
    9. FINALLY open your browser type localhost:8080/ to run your project (It will open your project .html page by default because it is defined inside /WEB-INF/web.xml file)

    Congratulations!

    0 讨论(0)
  • 2021-02-14 09:16

    I use GWT with Tomcat in hosted mode. I modify ant file generated by WebAppCreator to do the next(target to run-> hosted):

    1. Compile java sources(javac)
    2. Copy war folder to webapps/myapp
    3. Initialize devmode
    4. You must restart manually Tomcat

      <?xml version="1.0" encoding="utf-8" ?>
      <project name="app" default="build" basedir=".">
        <property name="tomcat.home" value="C:/Program Files/Apache Software Foundation/Tomcat 7.0" />
        <!-- Arguments to gwtc and devmode targets -->
        <property name="gwt.args" value="" />
      
        <!-- Configure path to GWT SDK -->
        <property name="gwt.sdk" location="D:/Instaladores/gwt-2.3.0" />
      
        <path id="project.class.path">
          <pathelement location="war/WEB-INF/classes"/>
          <pathelement location="${gwt.sdk}/gwt-user.jar"/>
          <fileset dir="${gwt.sdk}" includes="gwt-dev*.jar"/>
          <!-- Add any additional non-server libs (such as JUnit) -->
          <fileset dir="war/WEB-INF/lib" includes="**/*.jar"/>
        </path>
      
        <target name="libs" description="Copy libs to WEB-INF/lib">
          <mkdir dir="war/WEB-INF/lib" />
          <copy todir="war/WEB-INF/lib" file="${gwt.sdk}/gwt-servlet.jar" />
          <copy todir="war/WEB-INF/lib" file="${gwt.sdk}/gwt-servlet-deps.jar" />
          <!-- Add any additional server libs that need to be copied -->
        </target>
      
        <target name="javac" depends="libs" description="Compile java source to bytecode">
          <mkdir dir="war/WEB-INF/classes"/>
          <javac srcdir="src" includes="**" encoding="utf-8"
      destdir="war/WEB-INF/classes"
      source="1.5" target="1.5" nowarn="true"
      debug="true" debuglevel="lines,vars,source">
            <classpath refid="project.class.path"/>
          </javac>
          <copy todir="war/WEB-INF/classes">
            <fileset dir="src" excludes="**/*.java"/>
          </copy>
        </target>
      
        <target name="mywar" depends="javac,clean" description="Copy war folder">
      <!--   deploy war folder without compile java to js -->    
          <copy todir="${tomcat.home}/webapps/app">
              <fileset dir="${basedir}/war"/>
          </copy>     
          <chmod perm="a+w">
              <dirset dir="${tomcat.home}/webapps/app/files" />          
          </chmod>
        </target>
      
      
        <target name="gwtc" depends="javac" description="GWT compile to JavaScript (production mode)">
          <java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">
            <classpath>
              <pathelement location="src"/>
              <path refid="project.class.path"/>
              <pathelement location="D:/Instaladores/gwt-2.3.0/validation-api-1.0.0.GA.jar" />
              <pathelement location="D:/Instaladores/gwt-2.3.0/validation-api-1.0.0.GA-sources.jar" />
            </classpath>
            <!-- add jvmarg -Xss16M or similar if you see a StackOverflowError -->
            <jvmarg value="-Xmx512M"/>
            <arg line="-war"/>
            <arg value="war"/>
            <!-- Additional arguments like -style PRETTY or -logLevel DEBUG -->
            <arg line="${gwt.args}"/>
            <arg value="com.app.App"/>
          </java>
        </target>
      
        <target name="devmode" depends="mywar" description="Run development mode">
          <java failonerror="true" fork="true" classname="com.google.gwt.dev.DevMode">
            <classpath>
              <pathelement location="src"/>
              <path refid="project.class.path"/>
              <pathelement location="D:/Instaladores/gwt-2.3.0/validation-api-1.0.0.GA.jar" />
              <pathelement location="D:/Instaladores/gwt-2.3.0/validation-api-1.0.0.GA-sources.jar" />
            </classpath>
            <jvmarg value="-Xmx256M"/>
            <arg value="-startupUrl"/>
            <arg value="http://localhost:8080/app/App.html"/>
            <arg line="-war"/>
            <arg value="war"/>
            <!-- Additional arguments like -style PRETTY or -logLevel DEBUG -->
           <arg line="${gwt.args}"/>
            <arg value="com.app.App"/>
            <arg value="-noserver"/>      
          </java>
        </target>
      
        <target name="hosted" depends="devmode" description="Run development mode (NOTE: the 'hosted' target is deprecated)" />
      
        <target name="build" depends="gwtc" description="Build this project" />
      
        <target name="war" depends="build" description="Create a war file">
          <zip destfile="${tomcat.home}/webapps/app.war" basedir="war"/>
        </target>
      
        <target name="clean" description="Cleans this project">    
          <delete dir="${tomcat.home}/webapps/app" />    
        </target>
      
      </project>
      
    0 讨论(0)
提交回复
热议问题