servlet开发笔记(一)

1.servlet类是一个Interface,
A servlet is a small Java program that runs within a Web server. Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol.

To implement this interface, you can write a generic servlet that extends javax.servlet.GenericServlet or an HTTP servlet that extends javax.servlet.http.HttpServlet.

This interface defines methods to initialize a servlet, to service requests, and to remove a servlet from the server. These are known as life-cycle methods and are called in the following sequence:

The servlet is constructed, then initialized with the init method.
Any calls from clients to the service method are handled.
The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized.
In addition to the life-cycle methods, this interface provides the getServletConfig method, which the servlet can use to get any startup information, and the getServletInfo method, which allows the servlet to return basic information about itself, such as author, version, and copyright.

接口的特性:
①.接口是一种特殊抽象类,这种抽象类中只包含常量和方法的定义,而没有变量和方法的实现
②.接口可以多重实现,一个类可以实现多个无关的接口
③.接口中声明的属性默认为public static final

2.GenericServlet是一个abstract class,它实现了servlet接口
我们可以写一个servlet继承GenericServlet(抽象类必须被继承),重写service()方法
public void service(ServletRequest req,ServletResponse res)throws ServletException,java.io.IOException{ 
}

req - the ServletRequest object that contains the client's request
res - the ServletResponse object that contains the servlet's response

例如:

package edu.cn.hil;
import java.io.*;
import javax.servlet.*;
public class HelloServlet extends GenericServlet{
 public void service(ServletRequest req,ServletResponse res)throws ServletException,java.io.IOException{
  OutputStream out = res.getOutputStream();
  out.write("helloServlet!!!".getBytes());
  
 }
}

3.执行该servlet步骤
a.在tomcat/webapps下新建一个web应用TestServlet,在TestServlet文件夹下新建WEB-INF/calsses文件夹
b.在WEB-INF/calsses文件夹下创建java类HelloServlet.java
c.编译该java类
①进入cmd命令行提示符cd D:\Tomcat6\webapps\day04\WEB-INF\classes
②set classpath=%classpaht%;D:\Tomcat6\lib\servlet-api.jar
③javac -d . HelloServlet.java
d.在WEB-INF文件夹下新建web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>edu.cn.ahtcm.hil.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/HelloServlet</url-pattern>
  </servlet-mapping>
</web-app>


e.启动tomcat,在浏览器地址栏输入http://localhost:8080/TestServlet/HelloServlet

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值