Java

Java

BlogJava 首页 新随笔 联系 聚合 管理
  3 Posts :: 12 Stories :: 7 Comments :: 0 Trackbacks

第一种方法:通过实现servlet接口的方式来开发。

1、配置WEB-INF文件夹,内再建立classes和lib两个文件夹
2、开发servlet(引入servlet-api.jar)
 1 //Hello.java
 2 
 3 package com.keer2345;
 4 
 5 import javax.servlet.*;
 6 import java.io.*;
 7 import javax.servlet.ServletConfig;
 8 import javax.servlet.ServletException;
 9 import javax.servlet.ServletRequest;
10 import javax.servlet.ServletResponse;
11 import java.io.IOException;
12 
13 public class Hello implements Servlet{
14 
15     public void init(ServletConfig parm1) throws ServletException {
16         // TODO: Add your code here
17     }
18 
19     public ServletConfig getServletConfig() {
20         // TODO: Add your code here
21         return null;
22     }
23 
24     public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
25         // TODO: Add your code here
26         PrintWriter pw = response.getWriter();
27         pw.println("Hello World!");
28     }
29 
30     public String getServletInfo() {
31         // TODO: Add your code here
32         return null;
33     }
34 
35     public void destroy() {
36         // TODO: Add your code here
37     }
38     
39 }

3、部署servlet(web.xml)
 1 <?xml version="1.0" encoding="ISO-8859-1"?>
 2 
 3 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
 4    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 6    version="2.5">
 7 
 8     <servlet>
 9         <servlet-name>hello</servlet-name>
10         <servlet-class>com.keer2345.Hello</servlet-class>
11     </servlet>
12     
13     <servlet-mapping>
14         <servlet-name>hello</servlet-name>
15         <url-pattern>/hello</url-pattern>
16     </servlet-mapping>
17 
18 </web-app>


4、启动tomcat,访问http://127.0.0.1:8080/keer2345/hello。


第二种方法:通过继承GenericServle开发servlet

HelloGen.java
 1 package com.keer2345;
 2 
 3 import javax.servlet.*;
 4 import javax.servlet.GenericServlet;
 5 import java.io.*;
 6 
 7 public class HelloGen extends GenericServlet {
 8 
 9     public HelloGen() {
10     }
11     
12     public void service(ServletRequest request, ServletResponse response) {
13         try {
14             PrintWriter pw = response.getWriter();
15             pw.println("Hello GenericServlet!");
16         }
17         catch(Exception ex) {
18             ex.printStackTrace();
19         }
20     }    
21     
22 }


第三种方法:通过继承HttpServlet开发Servlet

HelloHttp.java
 1 package com.keer2345;
 2 
 3 import javax.servlet.http.*;
 4 import java.io.*;
 5 
 6 public class HelloHttp extends HttpServlet {
 7     
 8     public void doGet(HttpServletRequest request, HttpServletResponse response) {
 9         
10         try {
11             PrintWriter pw = response.getWriter();
12             pw.println("Hello HttpServlet!");
13         }
14         catch(Exception ex) {
15             ex.printStackTrace();
16         }
17         
18     } 
19     
20     public void doPost(HttpServletRequest request, HttpServletResponse response) {
21         
22         this.doGet(request, response);
23         
24     }  
25 }

web.xml
 1 <?xml version="1.0" encoding="ISO-8859-1"?>
 2 
 3 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
 4    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 6    version="2.5">
 7 
 8     <servlet>
 9         <servlet-name>hello</servlet-name>
10         <servlet-class>com.keer2345.Hello</servlet-class>
11     </servlet>
12     
13     <servlet-mapping>
14         <servlet-name>hello</servlet-name>
15         <url-pattern>/hello</url-pattern>
16     </servlet-mapping>
17     
18     <servlet>
19         <servlet-name>helloGen</servlet-name>
20         <servlet-class>com.keer2345.HelloGen</servlet-class>
21     </servlet>
22     
23     <servlet-mapping>
24         <servlet-name>helloGen</servlet-name>
25         <url-pattern>/helloGen</url-pattern>
26     </servlet-mapping>
27     
28         <servlet>
29         <servlet-name>helloHttp</servlet-name>
30         <servlet-class>com.keer2345.HelloHttp</servlet-class>
31     </servlet>
32     
33     <servlet-mapping>
34         <servlet-name>helloHttp</servlet-name>
35         <url-pattern>/helloHttp</url-pattern>
36     </servlet-mapping>
37 
38 
39 </web-app>





posted on 2008-07-20 14:16 keer 阅读(3606) 评论(0)  编辑  收藏 所属分类: Servlet 笔记

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问