三个写servlet方法的实例
来源:互联网 发布:js冒泡排序代码 编辑:程序博客网 时间:2024/05/18 00:56
第一个通过实现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");
}
}
* @(#)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");
}
}
第二个通过继承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();
}
}
}
* @(#)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();
}
}
}
第三种,也是常见的一种,通过继承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);
}
}
* @(#)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"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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中的三个方法
- 得到Class实例的三个方法是什么?
- Servlet的三个名字
- JDBC实例 Statement,PreparedStatement,CallableStatement三个方法的实例
- JDBC实例 Statement,PreparedStatement,CallableStatement三个方法的实例
- java 使用class进行对象实例化的三个方法
- Servlet(四)--Servlet的三个变量
- Servlet的三个域对象
- 写博客的三个阶段
- servlet几种跳转方法的路径怎么写
- 致富的三个方法
- ViewGroup的三个方法
- 提高工作效率的三个方法
- servlet_05 servlet使用表单的GET方法实例
- php解析url三个方法与实例
- 简单的servlet实例
- 简单的servlet实例
- .......
- Java初学者都必须理解的六大问题
- 主流RIA技术的简单比较:来自JavaFX、Flex和Silverlight的Hello World(二)
- Borland 賣掉 CodeGear
- Benihana 料理
- 三个写servlet方法的实例
- 使用SQL Server提供者的简单控制台应用程序
- office 2003漏洞打不上,360不能更新的解决方法
- dwr路径问题
- 改变导入JS文件的编码
- 为Vista修复Grub修改过的MBR
- [转]C中static的使用
- 四个作用域
- Adobe Acrobat 8 Professional(PDFMaker文件遗失的解决方法)