前提:正确安装JDK和Resin3.0
这个是原始文档,我做一下注释
Configuring the web.xml
The following is a complete working web.xml to run this example.
The servlet-mapping tells Resin that the URL /hello should invoke the hello-world servlet.
The servlet tells Resin that hello-world uses the test.HelloWorld class and that the value of the greeting init parameter is Hello World.
WEB-INF/web.xml
<web-app>
<servlet-mapping url-pattern='/hello'
servlet-name='hello-world'/>
<servlet servlet-name='hello-world'
servlet-class='test.HelloWorld'>
<init-param greeting='Hello, World'/>
</web-app>
WEB-INF/web.xml //web.xml不一定有自己新建一个即可,这个是我改过的!
<web-app>
<servlet servlet-name='hello-world'
servlet-class='test.HelloWorld'/> <servlet-mapping url-pattern='/hello'
servlet-name='hello-world'/>
</web-app>
原始文档中有三个错误,我也不知道是不是我错了!但是我改过三个错误,运行成功!
1。servlet servlet-name标签应该放在servlet-mapping前面。
2。<servlet servlet-name='hello-world' servlet-class='test.HelloWorld'>标签结尾少了一个" / "。
3。<init-param greeting='Hello, World'/> 这句标签我去掉了,不然也无法运行!
The Java code, HelloWorld.java belongs in
$app-dir/WEB-INF/classes/test/HelloWorld.java
Or, if you're compiling the servlet yourself, the class file belongs in
$app-dir/WEB-INF/classes/test/HelloWorld.class
Following is the actual servlet code. It just prints a trivial HTML page filled with the greeting specified in the web.xml.
init() and destroy() are included mostly for illustration. Resin will call init() when it starts the servlet and destroy before Resin destroys it.
package test;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
private String greeting;
public void init()
throws ServletException
{
greeting = getInitParameter("greeting");
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println("<title>" + greeting + "</title>");
out.println("<h1>" + greeting + "</h1>");
}
public void destroy()
{
// nothing to do
}
}
它文档中的这个用了那个<init-param greeting='Hello, World'/>标签,由于我删除了,所以无法使用!
就用着吧!保存为HelloWorld.java放在WEB-INF/classes/test目录下面。
package test;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
pw.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
pw.println("<head>");
pw.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">");
pw.println("<!-- The Servlet expression tags interpolate script variables into the HTML -->");
pw.println("<title>Hello, world!</title>");
pw.println("</head>");
pw.println("<body bgcolor=#cc99dd>");
pw.println("<h1>Hello, world!</h1>");
pw.println("</body>");
pw.close();
}
}
开启Resin服务,在浏览器中输入 http://localhost:8080/hello
即可以正确访问!
查看本文来源