程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 使用Resin3.0配置運行Java Servlet

使用Resin3.0配置運行Java Servlet

編輯:關於JAVA

前提:正確安裝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

servlet-name="hello-world"/>

servlet-class="test.HelloWorld">

WEB-INF/web.xml //web.XML不一定有自己新建一個即可,這個是我改過的!

servlet-class="test.HelloWorld"/> 

servlet-name="hello-world"/>

原始文檔中有三個錯誤,我也不知道是不是我錯了!但是我改過三個錯誤,運行成功!

1。servlet servlet-name標簽應該放在servlet-mapping前面。

2。標簽結尾少了一個" / "。

3。 這句標簽我去掉了,不然也無法運行!

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("" + greeting + "");

out.println("" + greeting + "");

}

public void destroy()

{

// nothing to do

}

}

它文檔中的這個用了那個標簽,由於我刪除了,所以無法使用!

就用著吧!保存為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("");

pw.println("");

pw.println("");

pw.println("");

pw.println("Hello, world!");

pw.println("");

pw.println("");

pw.println("Hello, world!");

pw.println("");

pw.close();

}

}

開啟Resin服務,在浏覽器中輸入 http://localhost:8080/hello

即可以正確訪問!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved