使用Resin3.0配置运行Java Servlet
2008-01-05 19:40:27 来源:WEB开发网核心提示:前提:正确安装JDK和Resin3.0这个是原始文档,我做一下注释Configuring the web.xmlThe following is a complete working web.xml to run this example.The servlet-mapping tells Resin that the
前提:正确安装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
即可以正确访问!
- ››使用word强大的搜索和替换功能
- ››使用Win7自带屏幕录制功能的方法
- ››使用linux中的quota教程
- ››使用jxl生成带动态折线图的excel
- ››使用mysql mysqldump进行数据库迁移
- ››使用jquery是新tab形式
- ››配置MySQL出错The service could not be started....
- ››使用QUnit进行Javascript单元测试
- ››使用UITextFieldDelegate来隐藏键盘
- ››使用公式提取Excel中的日期后发现格式不对
- ››使用SQL Azure 的BI 解决方案
- ››使用PLSQL Developer工具导出sql文件
更多精彩
赞助商链接