Servlet方法实现***个通过实现servlet接口的方式来开发
- /** *//**
- * @(#)Hello.java
- *
- *
- * @author shiyi05
- * @version 1.00 2008/5/9
- */
- //这是我的***个servlet,使用 implements Servlet实现servlet接口的方式来开发
- package com.rao;
- import javax.servlet.*;
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import java.io.IOException;
- import javax.servlet.*;
- import java.io.*;
- public class Hello implements Servlet
- ...{
- public Hello()
- ...{
- }
- /** *//**
- * Method init
- *
- *
- * @param parm1
- *
- @throws ServletException
- *
- */
- //该函数用于初始化该servlet,该函数只会被调用一次(当用户***次访问该servlet时)
- public void init(ServletConfig parm1) throws ServletException
- ...{
- // TODO: Add your code here
- System.out.println("init");
- }
- /** *//**
- * Method getServletConfig
- *
- *
- * @return
- *
- */
- public ServletConfig getServletConfig()
- ...{
- // TODO: Add your code here
- return null;
- }
- /** *//**
- * Method service
- *
- *
- * @param req 用于获得客户端的信息
- * @param res 用于向客户端返回信息
- *
- @throws ServletException
- @throws IOException
- *
- */
- //该函数用于处理业务逻辑,当用户每访问该servlet时,都会被调用
- public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
- ...{
- // TODO: Add your code here
- System.out.println("service");
- PrintWriter pw=res.getWriter();
- pw.println("Hello world!");
- }
- /** *//**
- * Method getServletInfo
- *
- *
- * @return
- *
- */
- public String getServletInfo()
- ...{
- // TODO: Add your code here
- return "";
- }
- /** *//**
- * Method destroy
- *
- *
- */
- public void destroy()
- ...{
- // TODO: Add your code here
- System.out.println("destroy");
- }
- }
Servlet方法实现第二个通过继承GenericServlet开发
- /** *//**
- * @(#)HelloGen.java
- *
- *
- * @author shiyi05
- * @version 1.00 2008/5/9
- */
- //这是第二种开发servlet的方法,是通过继承GenericServlet开发
- package com.rao;
- import javax.servlet.GenericServlet;
- import java.io.*;
- import javax.servlet.*;
- public class HelloGen extends GenericServlet
- ...{
- public HelloGen()
- ...{
- }
- //重写service()方法
- public void service(ServletRequest req,ServletResponse res)
- ...{
- try
- ...{
- PrintWriter pw=res.getWriter();
- pw.println("Hello World,Generic");
- }
- catch(Exception ex)
- ...{
- ex.printStackTrace();
- }
- }
- }
Servlet方法实现第三种,也是常见的一种,通过继承HttpServlet类实现
- /**
- * @(#)HelloHttp.java
- * @author shiyi05
- * @version 1.00 2008/5/9
- */
- //这是第三种开发servlet的方法,是通过继承HttpServlet类实现
- package com.rao;
- import javax.servlet.http.*;
- import java.io.*;
- public class HelloHttp extends HttpServlet
- ...{
- public HelloHttp()
- ...{
- }
- //处理Get请求
- public void doGet(HttpServletRequest req,HttpServletResponse res)
- ...{
- try
- ...{
- PrintWriter pw=res.getWriter();
- pw.println("Hello World,HttpServlet");
- }
- catch(Exception ex)
- ...{
- ex.printStackTrace();
- }
- }
- public void doPost(HttpServletRequest req,HttpServletResponse res)
- ...{
- this.doGet(req,res);
- }
- }
***把这三个servlet在web.xml文件中部署
- ﹤?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_2_5.xsd"
- version="2.5"﹥
- ﹤display-name﹥Welcome to Tomcat﹤/display-name﹥
- ﹤description﹥
- Welcome to Tomcat
- ﹤/description﹥
- ﹤servlet﹥
- ﹤!--servlet取名,是任意的--﹥
- ﹤servlet-name﹥Hello﹤/servlet-name﹥
- ﹤!--指明servlet路径,包名+类名--﹥
- ﹤servlet-class﹥com.rao.Hello﹤/servlet-class﹥
- ﹤/servlet﹥
- ﹤servlet-mapping﹥
- ﹤!--跟上面一样--﹥
- ﹤servlet-name﹥Hello﹤/servlet-name﹥
- ﹤!--浏览器中访问该servlet的URL,是任意的--﹥
- ﹤url-pattern﹥/Hello﹤/url-pattern﹥
- ﹤/servlet-mapping﹥
- ﹤servlet﹥
- ﹤!--servlet取名,是任意的--﹥
- ﹤servlet-name﹥HelloGen﹤/servlet-name﹥
- ﹤!--指明servlet路径,包名+类名--﹥
- ﹤servlet-class﹥com.rao.HelloGen﹤/servlet-class﹥
- ﹤/servlet﹥
- ﹤servlet-mapping﹥
- ﹤!--跟上面一样--﹥
- ﹤servlet-name﹥HelloGen﹤/servlet-name﹥
- ﹤!--浏览器中访问该servlet的URL,是任意的--﹥
- ﹤url-pattern﹥/HelloGen﹤/url-pattern﹥
- ﹤/servlet-mapping﹥
- ﹤servlet﹥
- ﹤!--servlet取名,是任意的--﹥
- ﹤servlet-name﹥HelloHttp﹤/servlet-name﹥
- ﹤!--指明servlet路径,包名+类名--﹥
- ﹤servlet-class﹥com.rao.HelloHttp﹤/servlet-class﹥
- ﹤/servlet﹥
- ﹤servlet-mapping﹥
- ﹤!--跟上面一样--﹥
- ﹤servlet-name﹥HelloHttp﹤/servlet-name﹥
- ﹤!--浏览器中访问该servlet的URL,是任意的--﹥
- ﹤url-pattern﹥/HelloHttp﹤/url-pattern﹥
- ﹤/servlet-mapping﹥
- ﹤/web-app﹥
Servlet方法实现的使用三种方法都开发完成!!希望对你在Servlet方法实现上有所帮助。
【编辑推荐】