程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
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
  <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
  即可以正確訪問!
 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved