#第一个 Servlet # Servlet是 Sun公司提供的一门用于开发动态web资源的技术。
Sun 公司在其API中提供了一个servlet接口,用户若想开发一个动态web资源,须要完成如下两个步骤:java
###快速入门,用servlet向浏览器输出“hello servlet”###web
* 1.建立Web资源目录 day04
-- WEB-INF
---- classes web.xml
------ FirstServlet.javaapi
* 2.编写类浏览器
package com.lynn import java.io.*; import javax.servlet.*; public class FirstServlet extends GenericServlet{ public void service (ServletRequest req,ServletResponse res) throws ServletException,IOException{ OutputStream out = res.getOutputStream(); out.write("hello servlet".getBytes()); } }
3.编译 将servletapi的jar包设置到 classpath中
set classpath=%classpath%;你的tomcat lib路径/servlet-api.jar
javac -d . FirstServlet.java
看到编译成功后,查看classes目录会多出 com/lynn/FirstServlet.class文件tomcat
4.配置Servlet容器和映射服务器
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>com.lynn.FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/FirstServlet</url-pattern> </servlet-mapping> </web-app>
###图解 Servlet### app