首页 > 代码库 > Servlet入门

Servlet入门

1、在tomcat中新建一个day01web应用,然后在web应用中新建一个web-inf/classes目录;
2、在classes目录中新建一个FirstServlet.java文件:
package cn.itcast
import java.io.*;
import javax.servlet.*;
public class Firstservlet extends GenericServlet{
public void service(ServletRequest req, ServletResponse res)throws ServletException,java.io.IOException
{
OutputStream out=res.getOutputStream();
out.write("Hello Servlet!!".getBytes());
}
}
3、打开dos命令窗,set classpath=%classpath%;......servlet.jar,编译servlet
4、在web-inf目录中新建一个web.xml文件,配置servlet的对外访问路径
<?xml version="1.0" encoding="ISO-8859-1"?>
  <web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0">
<servlet>
  <servlet-name>Firstservlet</servlet-name>
  <servlet-class>cn.itcast.Firstservlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>Firstservlet</servlet-name>
  <url-pattern>/Firstservlet/*</url-pattern>
</servlet-mapping>
</web-app>

Servlet入门