Wednesday 25 March 2015

Simple shell script to create and package a Servlet


#!/bin/sh

SERVLET_NAME=$1

rm -rI ${SERVLET_NAME}

# Create WAR file structure
echo "Creating WAR directory structure"
mkdir -p ${SERVLET_NAME}/src/co/uk/bobclarke
mkdir -p ${SERVLET_NAME}/WEB-INF/classes

# Create java file
echo "Creating ${SERVLET_NAME}.java"
cat > ${SERVLET_NAME}/src/co/uk/bobclarke/${SERVLET_NAME}.java <<EOF1
package co.uk.bobclarke;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ${SERVLET_NAME} extends HttpServlet{

        public void doGet ( HttpServletRequest request, HttpServletResponse response) throws IOException {
                PrintWriter out = response.getWriter();
                out.println("<html>");
                out.println("<body>");
                out.println("<p>Hello World</p>");
                out.println("</body>");
                out.println("</html>");
        }
}
EOF1


# Create deployment descriptor
echo "Creating web.xml"
cat > ${SERVLET_NAME}/WEB-INF/web.xml <<EOF2
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
        <servlet>
                <servlet-name>${SERVLET_NAME}</servlet-name>
                <servlet-class>co.uk.bobclarke.${SERVLET_NAME}</servlet-class>
        </servlet>

        <servlet-mapping>
                <servlet-name>${SERVLET_NAME}</servlet-name>
                <url-pattern>/${SERVLET_NAME}</url-pattern>
        </servlet-mapping>
</web-app>
EOF2

# Compile
echo "Compiling ${SERVLET_NAME}.java"
javac -classpath /home/autowas/was/dev/JavaEE/j2ee.jar \
        ${SERVLET_NAME}/src/co/uk/bobclarke/${SERVLET_NAME}.java -d \
        ${SERVLET_NAME}/WEB-INF/classes

# Create WAR file
echo "Creating ${SERVLET_NAME}.war"
jar -cvf ${SERVLET_NAME}.war -C ${SERVLET_NAME} .

1 comment:

  1. Dear Bob, what a wonderful blog! It's a pity i never seen it before. Now if i may to bother you with the following question :
    I am configuring WBM 855 and at some point after I configure federated repository via scripts I observe that my virtual host got missing after cell restart . Not a single one shows up -- admn_host, default_host, proxy_host - all gone from admin console. The file virtualhosts.xml is still present in cells/Cellname folder, but is not visible. Next i am trying to see what happens if i add virtual hosts manually via admin console. What i see is that newly created file gets saved into cells/Cellname/nodes/DMGRCellmanager folder. Can anybody explain to me what is happening? I did observe that once before in the past, even opened PMR, but IBM responded like that was something they never heard of before. I rebuilt cell and it was fine so i thought it was sort of a fluke. Now I see this for a second time with different sequence of actions - so something is happening - I just don't understand what.
    If i generate plugin - given the ports shown in plugin - it looks like the original file from cells/Cellname folder is used to create plugin.
    WBM ver 855 on Linux 64 bit, WAS ND. Have you seen this before or have any idea why is this happening? Thanks in advance.

    I am using my wife's Google account for this post , as i was trying mine and my post disappeared. Full disclosure I posted that same question at developerworks so you may respond at either forum if you would like . Thanks again .VladS vsereb at prolifics dot com

    ReplyDelete