1。在netbeans-tools-server manerger下添加tomcat伺服器, 預設是已經配置好的。
- 配置使用者:這個要在base directory 下的tomcat-users.xml檔案中設定
- 使用者角色(role):admin 或者 manager 或者 admin,manager
- Home Directory.伺服器所在目錄, 安裝好以後基本不管它的事了
- Base Directory: 使用者配置所在目錄, 設定你的伺服器和你的servlets
2。發布servlet 建立一個工程, samples下有tomcat servlet example.命名為TomcatServletExample
- 在base directory下有apache-tomcat-5.5.17_base/work/Catalina/localhost/servlets-examples/tldCache.ser
- 在apache-tomcat-5.5.17_base/conf/Catalina/localhost下有depth#examples.xml:
這是以檔案夾和檔案形式發布的結構。前面提到的servlets-examples.xml檔案中的docBase屬性就是我工程檔案的目錄, path是制定的訪問路徑, 比如,我這裡可以通過http://localhost:8084/servlets-examples/訪問, 用戶端的訪問就是通過這個檔案來轉向的
docBase的典型結構是:
*.html, *.jsp, etc :頁面
/WEB-INF/web.xml :he
Web Application Deployment Descriptor for your application. This is an XML file describing the servlets and other components that make up your application, along with any initialization parameters and container-managed security constraints that you want the server to enforce for you.
/WEB-INF/classes/ :編譯後的.class檔案
/WEB-INF/lib/ :This directory contains JAR files that contain Java class files (and associated resources) required for your application, such as third party class libraries or JDBC drivers. 3. 弄清了結構,然後自己動手寫一個簡單
- 建立一個web application, 預設有一個index.jsp作為首頁, 首頁可以在web.xml的pages標籤下修改
- index.jsp在body標籤下添加:
- 在source packages下添加NewServlet.class, 代碼附在後面
- 在web.xmlservlet標籤下配置NewServlet, 指定servlet class和url pattern, 我指定的是/NewServlet, 在http://localhost:8084/WebServletTest/NewServlet下就訪問到了
- 發現有時候要編譯兩次才能顯示正確的結果
- 串連一個圖片時, 檔案名稱竟然是大小寫敏感的
netbeans的web sample下有tomcat servlet的例子, 是學習servlet的很好的例子 執行個體代碼:------------------------------------- import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;public class NewServlet extends HttpServlet { /** Initializes the servlet. Connections to databases belong here !
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
/** Destroys the servlet. Close connections, files, etc.
*/
public void destroy() {
}
// Form values can be passed by 2 methods, GET or POST
// the doGet() (resp. doPost()) method is called when the method is
// GET (resp POST).
// The following trick allows us to process both with one function
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("");
out.println("");
out.println(""); // The real work of the servlet begins here
// A servlet does nothing else than outputing HTML code
// to the webserver. It can output the HTML code corresponding
// to a form. The user fills this form and, when the Submit button
// is clicked, the values are sent to the appropriate program on the
// webserver (in this case, our servlet) which then uses these values
// to `calculate' what it should display this time
// If the servlet is simply
// called by visiting an url or clicking a link, all parameters
// will have null values. This is what happens when you type
// `www.google.com' in your browser. We can detect this and
// print out a default `welcome' message (as below).
// If the servlet is called by clicking a submit
// button, no parameter will have null values (fields not filled
// by the user will return empty strings, not null).
if (request.getParameter("myparam") != null)
// the request.getParameter function allows us to obtain the
// values entered by the user in the various input fields
out.println("Your parameter is "+request.getParameter("myparam")+"
");
else
out.println("Hello, please enter a parameter !
");
out.println(" Enter your new parameter here:
");
out.println(
// The `action` field of the `form` tag indicates which program
// should be called by the webserver when the user clicks `submit'
// in this case, we tell it to call the same servlet again
" "+
// The 'name' of the input field corresponds to the name of the
// parameter which will contain the value
// entered in this input field (here, it is 'myparam' - see above)
"
"+
// The special `submit` input field generates a submit button
""
// When the user clicks the submit button, the browser sends a
// request to the servlet whose name is contained in the `action`
// field of the `` tag, which in this case is the present
// servlet. This request includes the values entered by the user
// in the different input fields as parameters.
+""
);
out.println("");
out.println("");
out.close();
}
public String getServletInfo() {
return "Short description";
}
}