信息化
新闻日历
图片新闻
Meat Card:饿了就吃点你的名片吧(图)
你的名片够特别吗?可以吃吗?Meat Cards是100%的牛肉...
2009/05/31
Q-Sound太阳能蓝牙耳机 你准备好迎接了吗(图)
所有为了环保节能尽份心力的产品都都值得嘉许,更何况...
2009/05/31
科学家将公布证明黑洞存在照片 揭秘神秘阴影(图)
美国一个研究小组日前表示,他们有望在未来几个月公布...
2009/05/31
地球磁气圈是"汪洋大盗" 偷走大气层气体
科学家通常认为对地球具有防护屏作用的磁气圈能够保护...
2009/05/31
分析评论
惠普让计算器在iPhone、Windows重生(图)
这项要价14.99美元的应用程序还可搭配一个29.99美元的...
2009/06/30
投资者看好没有乔布斯的苹果
自苹果CEO史蒂夫-乔布斯休病假以来,苹果股价已经上涨...
2009/06/22

jdk5.0 tomcat5.0配置全攻略(2)




作者: 中国IT实验室
CNETNews.com.cn
2007-08-23 13:56:11

2.servlet

      现在,我们该看一下servlet了!

      同样,编写一个程序HelloServlet.java


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
//** Simple servlet used to test server.*/

public class HelloServlet extends HttpServlet
{
 public void doGet(HttpServletRequest request,
                   HttpServletResponse response)
 throws ServletException,IOException
 {
  response.setContentType("text/html");
  PrintWriter out=response.getWriter();
  String docType="<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0"+
  "Transitional//EN"> ";
  out.println(docType+
  "<html> "+
  "<head><title>Hello</title></head> "+
  "<body bgcolor="#FFFF99"> "+
  "<h1>Hello</h1> "+
  "</body></html>");
 }

      编译还像上面HelloWorld.java的那样,把编译得到的.class文件copy到ROOT/WEB-INF/classes目录下,然后在ROOT/WEB-INF/下找到web.xml文件,打开编辑:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
  Copyright 2004 The Apache Software Foundation

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>


<!-- JSPC servlet mappings start -->

    <servlet>
        <servlet-name>org.apache.jsp.index_jsp</servlet-name>
        <servlet-class>org.apache.jsp.index_jsp</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>org.apache.jsp.index_jsp</servlet-name>
        <url-pattern>/index.jsp</url-pattern>
    </servlet-mapping>
        
    <servlet>
     <servlet-name>HelloServlet</servlet-name>
     <servlet-class>HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/servlet/HelloServlet</url-pattern> 
    </servlet-mapping>
    
 <!-- JSPC servlet mappings end -->

</web-app> 

      红色的部分就是我们添加进去的,<url-pattern>/servlet/HelloServlet</url-pattern>是影射到那个目录!保存!

      然后重新启动你的tomcat,在浏览器中输入: http://localhost:8080/servlet/HelloServlet/

      你看到什么了???相信你能看到米黄色的背景上有一个很大的Hello。 现在,你的servlet容器也没问题了!

查看本文来源

[an error occurred while processing this directive]
发表您的观点
   姓名:(必填)


   电子邮件:


   验证码:(必填)
 

   正文:(必填)