信息化
新闻日历
图片新闻
苹果iPad预订热潮暴涨 人数超当年iPhone
自iPad可以预定以来,顾客对iPad的热情持续高涨。市调...
2010/02/24
奢侈品:侧滑全键盘WM智能手机阿玛尼三星B7620
手机经过十几年的发展已经摆脱了当初笨重、难看的标签...
2010/02/22
Alienware前高管造Origin高端游戏悍本(图)
近日,Origin公司推出了一款游戏笔记本,型号为Eon18。...
2010/02/22
虚幻引擎3驾临iPhone 3GS
近日,Epic Games的副总裁Mark Rein展示了点儿新鲜玩意...
2009/12/23
分析评论
中国移动难逃山寨机杯具:Ophone乃下下签
我认为标准的建立应该按照以下逻辑推动。上策,完全自...
2010/02/01
Twitter网友热论苹果平板电脑 iPad名字遭嘲讽
苹果CEO史蒂夫·乔布斯(Steve Jobs)一揭开iPad的神秘...
2010/01/28

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]
发表您的观点
   姓名:(必填)


   电子邮件:


   验证码:(必填)
 

   正文:(必填)