Advertisement
Guest User

Set up servlets!

a guest
Jul 27th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.43 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. case "$1" in
  4. */)
  5.     directory=$1
  6.     ;;
  7. *)
  8.     directory="${1}/"
  9.     ;;
  10. esac
  11. name=$2
  12. classpath=$3
  13.  
  14. project_root="$directory$name"
  15. config_folder="$project_root/WEB-INF/"
  16. source_folder="$project_root/src/"
  17. class_folder="${config_folder}classes/"
  18.  
  19. echo $directory
  20. echo $name
  21. echo $project_root
  22. echo $config_folder
  23. echo $source_folder
  24. echo $class_folder
  25.  
  26. mkdir -p $project_root
  27. mkdir -p $config_folder
  28. mkdir -p $class_folder
  29.  
  30.  
  31. # Creates a basic web.xml file that describes a hello-world servlet
  32. echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"                                 > "$config_folder/web.xml"
  33. echo "<web-app version=\"2.4\" xmlns=\"http://java.sun.com/xml/ns/j2ee\" "       >> "$config_folder/web.xml"
  34. echo "   xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "               >> "$config_folder/web.xml"
  35. echo "   xsi:schemaLocation=\"http://java.sun.com/xml/ns/j2ee "                  >> "$config_folder/web.xml"
  36. echo "               http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd\">" >> "$config_folder/web.xml"
  37. echo ""                                                                          >> "$config_folder/web.xml"
  38. echo "  <servlet>"                                                               >> "$config_folder/web.xml"
  39. echo "    <servlet-name>Hello-World</servlet-name>"                              >> "$config_folder/web.xml"
  40. echo "    <servlet-class>HelloWorld</servlet-class>"                             >> "$config_folder/web.xml"
  41. echo "  </servlet>"                                                              >> "$config_folder/web.xml"
  42. echo ""                                                                          >> "$config_folder/web.xml"
  43. echo "  <servlet-mapping>"                                                       >> "$config_folder/web.xml"
  44. echo "    <servlet-name>Hello-World</servlet-name>"                              >> "$config_folder/web.xml"
  45. echo "    <url-pattern>/hello</url-pattern>"                                     >> "$config_folder/web.xml"
  46. echo "  </servlet-mapping>"                                                      >> "$config_folder/web.xml"
  47. echo "</web-app>"                                                                >> "$config_folder/web.xml"
  48.  
  49. # Creates the directory the Java® source file lives in.
  50. mkdir -p $source_folder
  51.  
  52. # Creates the source-file
  53. echo ""                                                                                                     > "${source_folder}HelloWorld.java"
  54. echo ""                                                                                                    >> "${source_folder}HelloWorld.java"
  55. echo "import java.io.IOException;"                                                                         >> "${source_folder}HelloWorld.java"
  56. echo "import java.io.PrintWriter;"                                                                         >> "${source_folder}HelloWorld.java"
  57. echo "import javax.servlet.http.HttpServlet;"                                                              >> "${source_folder}HelloWorld.java"
  58. echo "import javax.servlet.http.HttpServletRequest;"                                                       >> "${source_folder}HelloWorld.java"
  59. echo "import javax.servlet.http.HttpServletResponse;"                                                      >> "${source_folder}HelloWorld.java"
  60. echo "  /**"                                                                                               >> "${source_folder}HelloWorld.java"
  61. echo "   * A simple hello-world servlet, responding to HTTP GET"                                           >> "${source_folder}HelloWorld.java"
  62. echo "   */"                                                                                               >> "${source_folder}HelloWorld.java"
  63. echo "public class HelloWorld extends HttpServlet {"                                                       >> "${source_folder}HelloWorld.java"
  64. echo "  @Override"                                                                                         >> "${source_folder}HelloWorld.java"
  65. echo "  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{"   >> "${source_folder}HelloWorld.java"
  66. echo "    PrintWriter out = response.getWriter();"                                                         >> "${source_folder}HelloWorld.java"
  67. echo "    out.println(\"<!DOCTYPE html>\");"                                                               >> "${source_folder}HelloWorld.java"
  68. echo "    out.println(\"<html lang=\\\"en\\\">\");"                                                        >> "${source_folder}HelloWorld.java"
  69. echo "    out.println(\"    <head>\");"                                                                    >> "${source_folder}HelloWorld.java"
  70. echo "    out.println(\"        <meta charset=\\\"utf-8\\\">\");"                                          >> "${source_folder}HelloWorld.java"
  71. echo "    out.println(\"                <title>Hello World</title>\");"                                    >> "${source_folder}HelloWorld.java"
  72. echo "    out.println(\"        </head>\");"                                                               >> "${source_folder}HelloWorld.java"
  73. echo "    out.println(\"        <body>\");"                                                                >> "${source_folder}HelloWorld.java"
  74. echo "    out.println(\"                <h1>Hello World</h1>\");"                                          >> "${source_folder}HelloWorld.java"
  75. echo "    out.println(\"        </body>\");"                                                               >> "${source_folder}HelloWorld.java"
  76. echo "    out.println(\"</html>\");"                                                                       >> "${source_folder}HelloWorld.java"
  77. echo "  }"                                                                                                 >> "${source_folder}HelloWorld.java"
  78. echo "}"                                                                                                   >> "${source_folder}HelloWorld.java"
  79.  
  80. # Moves us to the project root. This is not really necessary, but gives us a convenient place to put error logs.
  81. # No, I don't log errors. Why do you ask? :p
  82. pushd $project_root
  83. # Compiles the files. This will give an error if the classpath is wrong.
  84. javac src/HelloWorld.java -classpath ${classpath} -d ${class_folder}
  85.  
  86. # Goes back to where we came from.
  87. popd
  88.  
  89. # TODO:
  90. # Log errors.
  91. # Make an ant-build script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement