淺談Netbeans中Tomcat服務器配置
Netbeans Tomcat服務器實例配置
1。在netbeans-tools-server manerger下添加tomcat服務器, 默認是已經配置好的。
配置用戶:這個要在base directory 下的tomcat-users.xml文件中設置
用戶角色(role):admin 或者 manager 或者 admin,manager
Home Directory.服務器所在目錄, 安裝好以后基本不管它的事了
Base Directory: 用戶配置所在目錄, 設置你的服務器和你的servlets
2。發(fā)布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:
這是以文件夾和文件形式發(fā)布的結構。前面提到的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標簽下添加:
Execute
在source packages下添加NewServlet.class, 代碼附在后面
在web.xmlservlet標簽下配置NewServlet, 指定servlet class和url pattern, 我指定的是/NewServlet, 在http://localhost:8084/WebServletTest/NewServlet下就訪問到了
發(fā)現(xiàn)有時候要編譯兩次才能顯示正確的結果
連接一個圖片時, 文件名竟然是大小寫敏感的
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";
- }
- }
【編輯推薦】