service
method 4.Servlet的基本结构
package cn.dragon.servlet;
//下面是导入相应的包
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;
/**
* 这是第一个Servlet的例子
* @author cn.dragon
*/
public class ServletDemoFirst extends HttpServlet {
//用于处理客户端发送的GET请求
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GB2312"); //这条语句指明了向客户端发送的内容格式和采用的字符编码.
PrintWriter out = response.getWriter();
out.println(" 您好!"); //利用PrintWriter对象的方法将数据发送给客户端
out.close();
}
//用于处理客户端发送的POST请求
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response); //这条语句的作用是,当客户端发送POST请求时,调用doGet()方法进行处理
}
}
5、Servlet的部署
以下截取部分
<servlet>
<description>任意</description>
<display-name>任意</display-name>
<servlet-name>ServletDemoFirst</servlet-name>
<servlet-class>cn.dragon.servlet.ServletDemoFirst</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletDemoFirst</servlet-name>
<url-pattern>/servlet/ServletDemoFirst</url-pattern>
</servlet-mapping>
【注意】
① 上面的两个<servlet-name>必须相同
② <servlet-class>后面指在对应的类上面. 技巧:你可以直接在你的servlet类中复制过来,这样可以避免出错!
③ <url-pattern> 必须是/servlet 再加servlet名字.大家现在就这么记.
6、Servlet实例演示
package cn.dragon.servlet;
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 ServletDemoSecond extends HttpServlet {
//初始化
public void init() throws ServletException {
System.out.println("我是init()方法!用来进行初始化工作");
}
//处理GET请求
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("我是doGet()方法!用来处理GET请求");
response.setContentType("text/html;charset=GB2312");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<BODY>");
out.println("这是Servlet的例子");
out.println("</BODY>");
out.println("</HTML>");
}
//处理POST请求
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
//销毁实例
public void destroy() {
super.destroy();
System.out.println("我是destroy()方法!用来进行销毁实例的工作");
}
}
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
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">
<servlet>
<servlet-name>ServletDemoSecond</servlet-name>
<servlet-class>cn.dragon.servlet.ServletDemoSecond</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletDemoSecond</servlet-name>
<url-pattern>/servlet/ServletDemoSecond</url-pattern>
</servlet-mapping>
</web-app>