CNETNews 科技资讯网
首页信息化jdk5.0 tomcat5.0配置全攻略(2)

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

中国IT实验室时间2007-08-23 13:56:11作者:中国IT实验室
本文关键词:

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容器也没问题了!

查看本文来源
您看到此篇文章时的感受是:
支持
愤怒
无聊
暴汗
养眼
炒作
不解
标题党
搞笑
用户评论
  • 评论列表
  • 本篇文章共有 0 条评论
用户名
评论内容
发表时间
- 发表评论 -
匿名
注册用户


AMD新品新策 虎跃2010
精彩推荐