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的很好的例子
实例代码:
-------------------------------------
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
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("");
*/
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:
// 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(
// 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";
}
}