| 1 | {{{ |
| 2 | #!html |
| 3 | <div style="text-align: center; color:#151B8D"><big style="font-weight: bold;"><big><big> |
| 4 | Servlet |
| 5 | </big></big></big></div> <div style="text-align: center; color:#7E2217"><big style="font-weight: bold;"><big> |
| 6 | tomcat |
| 7 | </big></big></div> |
| 8 | }}} |
| 9 | [[PageOutline]] |
| 10 | |
| 11 | * 測試tomcat 的執行權限 |
| 12 | * 如何讓 Servlet 執行 shell 或其他指令 |
| 13 | * 如何編譯 Servlet 與執行 |
| 14 | |
| 15 | = 如何讓 Servlet 執行 shell 或其他指令 = |
| 16 | servlet 就是 java ,因此呼叫shell 或指令用同一個語法 |
| 17 | {{{ |
| 18 | #!java |
| 19 | Runtime.getRuntime().exec( "/path/my_shell.sh"); |
| 20 | }}} |
| 21 | |
| 22 | = 如何編譯 Servlet 與執行 = |
| 23 | |
| 24 | 需要額外引入 servlet-api.jar 與 jsp-api.jar (此兩個檔可以到 tomcat_home/lib/) |
| 25 | |
| 26 | = 測試tomcat 的執行權限 = |
| 27 | |
| 28 | {{{ |
| 29 | #!jsp |
| 30 | /* |
| 31 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 32 | * contributor license agreements. See the NOTICE file distributed with |
| 33 | * this work for additional information regarding copyright ownership. |
| 34 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 35 | * (the "License"); you may not use this file except in compliance with |
| 36 | * the License. You may obtain a copy of the License at |
| 37 | * |
| 38 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 39 | * |
| 40 | * Unless required by applicable law or agreed to in writing, software |
| 41 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 42 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 43 | * See the License for the specific language governing permissions and |
| 44 | * limitations under the License. |
| 45 | */ |
| 46 | /* $Id: HelloWorldExample.java 500674 2007-01-27 23:15:00Z markt $ |
| 47 | * |
| 48 | */ |
| 49 | |
| 50 | import java.io.*; |
| 51 | import java.util.*; |
| 52 | import javax.servlet.*; |
| 53 | import javax.servlet.http.*; |
| 54 | |
| 55 | /** |
| 56 | * The simplest possible servlet. |
| 57 | * |
| 58 | * @author James Duncan Davidson |
| 59 | */ |
| 60 | |
| 61 | public class HelloWorldExample extends HttpServlet { |
| 62 | |
| 63 | |
| 64 | public void doGet(HttpServletRequest request, |
| 65 | HttpServletResponse response) |
| 66 | throws IOException, ServletException |
| 67 | { |
| 68 | ResourceBundle rb = |
| 69 | ResourceBundle.getBundle("LocalStrings",request.getLocale()); |
| 70 | response.setContentType("text/html"); |
| 71 | PrintWriter out = response.getWriter(); |
| 72 | |
| 73 | Runtime.getRuntime().exec( "/home/waue/a.sh"); |
| 74 | |
| 75 | out.println("<html>"); |
| 76 | out.println("<head>"); |
| 77 | |
| 78 | String title = rb.getString("helloworld.title"); |
| 79 | title="waue test!"; |
| 80 | |
| 81 | out.println("<title>" + title + "</title>"); |
| 82 | out.println("</head>"); |
| 83 | out.println("<body bgcolor=\"white\">"); |
| 84 | |
| 85 | // note that all links are created to be relative. this |
| 86 | // ensures that we can move the web application that this |
| 87 | // servlet belongs to to a different place in the url |
| 88 | // tree and not have any harmful side effects. |
| 89 | |
| 90 | // XXX |
| 91 | // making these absolute till we work out the |
| 92 | // addition of a PathInfo issue |
| 93 | |
| 94 | out.println("<a href=\"../helloworld.html\">"); |
| 95 | out.println("<img src=\"../images/code.gif\" height=24 " + |
| 96 | "width=24 align=right border=0 alt=\"view code\"></a>"); |
| 97 | out.println("<a href=\"../index.html\">"); |
| 98 | out.println("<img src=\"../images/return.gif\" height=24 " + |
| 99 | "width=24 align=right border=0 alt=\"return\"></a>"); |
| 100 | out.println("<h1>" + title + "</h1>"); |
| 101 | out.println("</body>"); |
| 102 | out.println("</html>"); |
| 103 | } |
| 104 | } |
| 105 | }}} |
| 106 | |
| 107 | 編譯過後需將class 檔 放到 $tomcat_home/webapps/examples/WEB-INF/classes/ 資料夾下 |
| 108 | |
| 109 | 重新啟動 tomcat 或 用 manager/的 reload examples 資料夾 |