hello servlet

作者在 2011-12-20 15:23:52 发布以下内容
1、采用文本编辑器写程序吧。 将程序写好,文件后缀名为.java,程序:
import java.io.IOException;
import java.io.PrintWriter;

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


public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        
          response.setContentType("text/html");
          PrintWriter out = response.getWriter();
          out.println("<html>");  
          out.println("<head>");   
          out.println("<title>Hello Servlet</title>");   
          out.println("</head>");   
          out.println("<body>");   
          out.println("<h1>Hello Servlet</h1>");  
          out.println("</body>");   
          out.println("</html>");   
    }
}
2、在命令行下编译该java文件,得到生成的.class文件,将该类文件放到tomcat安装目录下的webapps/Root/WEB-INFO/classes下。
3、将webapps/Root/WEB-INFO下的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>

<!-- JSPC servlet mappings end -->
  <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>HelloServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/HelloServlet</url-pattern>
    </servlet-mapping>


</web-app>

粗体字是加上去的。   标记<servlet></servlet>里的<servlet-name>是指servlet程序的名称,<servlet-class>里的内容是类文件的名称。
<servlet-mapping>标记内的内容是匹配url和servlet程序。将<url-pattern>里的内容映射到<servlet-name>的内容上去。
4、在浏览器中输入:http://localhost:8080/HelloServlet,就能运行了。
初学J2EE | 阅读 962 次
文章评论,共0条
游客请输入验证码
浏览29117次