浅谈Netbeans中Tomcat服务器配置

开发 后端
本文详细介绍了在Netbeans下配置Tomcat。首先需要添加Tomcat服务器,并发布servlet。文章最后还附加了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。发布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标签下添加: 

Execute

在source packages下添加NewServlet.class, 代码附在后面
在web.xmlservlet标签下配置NewServlet, 指定servlet class和url pattern, 我指定的是/NewServlet, 在http://localhost:8084/WebServletTest/NewServlet下就访问到了
发现有时候要编译两次才能显示正确的结果
连接一个图片时, 文件名竟然是大小写敏感的
 netbeans的web sample下有tomcat servlet的例子, 是学习servlet的很好的例子
 
实例代码:

  1. -------------------------------------  
  2.    
  3. import java.io.*;  
  4. import java.net.*;  
  5. import javax.servlet.*;  
  6. import javax.servlet.http.*;  
  7. public class NewServlet extends HttpServlet {  
  8.     /** Initializes the servlet. Connections to databases belong here !  
  9.      */  
  10.     public void init(ServletConfig config) throws ServletException {  
  11.         super.init(config);  
  12.           
  13.     }  
  14.       
  15.     /** Destroys the servlet. Close connections, files, etc.  
  16.      */  
  17.     public void destroy() {  
  18.           
  19.     }  
  20.      
  21.     // Form values can be passed by 2 methods, GET or POST  
  22.     // the doGet() (resp. doPost())  method is called when the method is   
  23.     // GET (resp POST).  
  24.     // The following trick allows us to process both with one function  
  25.       
  26.     protected void doGet(HttpServletRequest request, HttpServletResponse response)  
  27.     throws ServletException, IOException {  
  28.         processRequest(request, response);  
  29.     }  
  30.       
  31.     protected void doPost(HttpServletRequest request, HttpServletResponse response)  
  32.     throws ServletException, IOException {  
  33.         processRequest(request, response);  
  34.     }  
  35.       
  36.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)  
  37.     throws ServletException, IOException {  
  38.         response.setContentType("text/html");  
  39.         PrintWriter out = response.getWriter();  
  40.           
  41.         out.println("");  
  42.         out.println("");  
  43.         out.println("");  
  44.         out.println("");  
  45.         out.println("");  
  46.  // The real work of the servlet begins here  
  47.    
  48.  // A servlet does nothing else than outputing HTML code  
  49.  // to the webserver. It can output the HTML code corresponding  
  50.  // to a form. The user fills this form and, when the Submit button   
  51.  // is clicked, the values are sent to the appropriate program on the   
  52.  // webserver (in this case, our servlet) which then uses these values  
  53.  // to `calculate' what it should display this time  
  54.    
  55.  // If the servlet is simply   
  56.  // called by visiting an url or clicking a link, all parameters  
  57.  // will have null values. This is what happens when you type  
  58.  // `www.google.com' in your browser. We can detect this and   
  59.  // print out a default `welcome' message (as below).  
  60.  // If the servlet is called by clicking a submit  
  61.  // button, no parameter will have null values (fields not filled  
  62.  // by the user will return empty strings, not null).  
  63.    
  64.         if (request.getParameter("myparam") != null)   
  65.      // the request.getParameter function allows us to obtain the   
  66.      // values entered by the user in the various input fields  
  67.             out.println("Your parameter is "+request.getParameter("myparam")+"  
  68. ");  
  69.         else   
  70.             out.println("Hello, please enter a parameter !  
  71. ");  
  72.  out.println(" Enter your new parameter here:  
  73. ");  
  74.         out.println(  
  75.  // The `action` field of the `form` tag indicates which program   
  76.  // should be called by the webserver when the user clicks `submit'  
  77.  // in this case, we tell it to call the same servlet again  
  78.         " "+  
  79.  // The 'name' of the input field corresponds to the name of the  
  80.  // parameter which will contain the value  
  81.  // entered in this input field (here, it is 'myparam' - see above)  
  82.         "  
  83. "+  
  84.  // The special `submit` input field generates a submit button  
  85.  ""  
  86.  // When the user clicks the submit button, the browser sends a   
  87.  // request to the servlet whose name is contained in the `action`   
  88.  // field of the `` tag, which in this case is the present   
  89.  // servlet. This request includes the values entered by the user   
  90.  // in the different input fields as parameters.  
  91.         +""  
  92.         );  
  93.           
  94.         out.println("");  
  95.         out.println("");  
  96.           
  97.         out.close();  
  98.     }  
  99.      
  100.     public String getServletInfo() {  
  101.         return "Short description";  
  102.     }  
  103.       
  104. }   

【编辑推荐】

  1. 让Eclipse和NetBeans共享同一个项目
  2. 使用NetBeans和Eclipse开发PHP应用程序
  3. NetBeans 6.0预览版发布 Sun再引惊呼
  4. 使用Netbeans操作MySQL数据库
  5. 八大技术牛人点评NetBeans 6.5
责任编辑:张燕妮 来源: csdn
相关推荐

2009-06-11 09:04:00

2019-01-09 13:07:26

Tomcat服务器优化

2010-09-29 16:12:09

cisco交换机DHC

2009-06-11 10:03:57

NetBeans代码

2010-08-25 14:40:49

DHCP服务器故障

2009-06-15 13:46:00

netbeans配置hibernate

2011-07-04 17:48:16

IBM服务器

2011-09-29 13:52:57

服务器HPC浪潮TS850

2009-07-09 10:25:05

Servlet的Web

2010-01-06 09:19:45

2009-07-17 12:44:01

NetBeans开发S

2019-04-23 10:48:55

HTTPTomcat服务器

2011-06-24 17:23:18

主服务器从服务器同步

2020-02-12 13:58:24

服务器高级优化

2013-07-23 09:51:32

Tomcat性能优化服务器性能优化

2019-02-21 14:10:34

Tomcat服务器Server.xml

2011-07-27 15:11:02

2010-09-27 11:16:27

DHCP服务器动态分配

2013-08-12 10:14:42

服务器虚拟化虚拟化

2009-01-16 14:37:57

TomcatWeb服务器应用服务器
点赞
收藏

51CTO技术栈公众号