小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

servlet 與web.xml的設(shè)置問題

 tinaroad 2007-08-09

在tomcat下運(yùn)行servlet,需要在web.xml文件中對(duì)servlet進(jìn)行配置,下面用一個(gè)具體的例子一步一步來看一下整個(gè)過程。

1:首先創(chuàng)建一個(gè)web應(yīng)用程序,這里我是用Eclipse建的,就叫web吧,存放路徑C:\eclipse\workspace\web

2:tomcat中,添加conf下的server.xml中的<Context >標(biāo)記

<Context path="/web" reloadable="true" docBase="C:\Eclipse\workspace\web"/>

3:編寫一個(gè)名為ServletTest的servlet程序,具體內(nèi)容如下,應(yīng)該很簡單的,就不多解釋:

package test;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletTest extends HttpServlet{
 protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
  doPost(arg0,arg1);
 }
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  response.setContentType("text/html");
  ServletOutputStream out=response.getOutputStream();
  out.println("<html>");
  out.println("<body>");
  out.println("TEST");
  out.println("</body>");
  out.println("</html>"); 
 }

}

4:將servlet編譯后生成的class文件放到WEB-INF的class目錄下,因?yàn)槲疫@里帶了個(gè)test包,所以生成的文件路徑就是WEB-INF----->class----->test------>ServletTest.class

5:配置web.xml文件,在web應(yīng)用程序的WEB-INF目錄下,新建一個(gè)如下內(nèi)容的web.xml文件

<?xml version="1.0" encoding="Shift_JIS"?>

<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java./dtd/web-app_2_3.dtd">
<web-app>
 <servlet>
  <servlet-name>ServletTest</servlet-name>
  <servlet-class>test.ServletTest</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>ServletTest</servlet-name>
  <url-pattern>/ServletTest</url-pattern>  
 </servlet-mapping>
</web-app>

這里解釋一下這個(gè)文件的內(nèi)容:

<servlet-name>標(biāo)簽指定了servlet的名字,主要是下面的<servlet-mapping>用;

<servlet-class>說明了servlet存放的class目錄下的位置,這里要加上必要的包名;

< servlet-mapping>標(biāo)簽中,<servlet-name>指出要要匹配的servlet的名字,這個(gè)與上邊的< servlet>標(biāo)簽中定義的名字對(duì)應(yīng);<url-pattern>指出了當(dāng)滿足什么條件時(shí),調(diào)用這個(gè)servlet;這里寫的是 /ServletTest

6:啟動(dòng)tomcat,并在瀏覽器中輸入http://localhost:8090/web/ServletTest

則瀏覽器輸出TEST

這里主要想說的就是<url-pattern>,這個(gè)標(biāo)簽指定了servlet的匹配類型,當(dāng)寫成 <url-pattern>/*</url-pattern>時(shí),瀏覽器中輸入http://localhost:8090/web/1111或者http://localhost:8090/web/2222,都會(huì)執(zhí)行這個(gè)servlet
也就是在這個(gè)地方可以用一些通配符表示。

END!

Addationally, some tips should be noted when developing with eclipse:

when you build a web project and edit the web.xml under the WebContent/WEB-INF derectory, the below mapping formet:
   
    <servlet-mapping>
        <servlet-name>DemoServlet</servlet-name>
        <url-pattern>/DemoServlet</url-pattern>
    </servlet-mapping>


corresponsing to the real url:   UrlToYouAppRoot/DemoServlet.
So you should refers to this url (or it‘s relative path) in other pages when you call the servlet.

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多