第一种
//使用实现servlet接口的方法来实现
package com.sina;import javax.servlet.*;
import java.io.*;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
public class Hello implements Servlet{
//初始化servlet
public void init(ServletConfig parm1) throws ServletException {
// TODO: Add your code here
System.out.println("init!");
}
public ServletConfig getServletConfig() {
// TODO: Add your code here
return null;
}
//完成servlet服务
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
// TODO: Add your code here
System.out.print("Service it!");
PrintWriter pw = res.getWriter();
pw.print("HelloWorld!");
}
public String getServletInfo() {
// TODO: Add your code here
return "";
}
//撤销servlet 清理内存
public void destroy() {
// TODO: Add your code here
System.out.println("Destroy!");
}
}
第二种
//使用继承GenericServlet开发servlet
package com.sina;
import javax.servlet.GenericServlet;
import javax.servlet.*;
import java.io.*;
public class HelloGen extends GenericServlet{
public void service(ServletRequest req,ServletResponse res){//只要重写service方法,完成自己想做的工作
try{
PrintWriter pw = res.getWriter();
pw.println("HelloWorld!Geniric!");
}catch(Exception ex){
ex.printStackTrace();
}
}
}
第三种,目前使用最多的
//通过继承HttpServlet来开发servlet
package com.sina;
import javax.servlet.http.*;
import java.io.*;
import javax.servlet.*;
public class HelloHttp extends HttpServlet{//必须重写doGet(),doPost()方法,但是两个方法通常写成一样的,也就是只要写其中的一个,另一个直接传参,调用已经写过的那个方法
public void doGet(HttpServletRequest req, HttpServletResponse res) {
doPost(req,res);
}
public void doPost(HttpServletRequest req,HttpServletResponse res){
try{
PrintWriter pw = res.getWriter();
pw.println("Hello,http!");
}catch(Exception ex){
ex.printStackTrace();
}
}
}
//配置xml文件,完成servlet部署
<?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_3_0.xsd"
version="3.0"
metadata-complete="true">
<display-name>Tomcat Manager Application</display-name>
<description>
A scriptable management web application for the Tomcat Web Server;
Manager lets you view, load/unload/etc particular web applications.
</description>
<servlet>
<servlet-name>Hello</servlet-name>//servlet/的名字
<servlet-class>com.sina.Hello</servlet-class>//类的位置,也就是包名加类名
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>//servlet的名字,必须同上
<url-pattern>/hot</url-pattern>//在浏览器中访问这个servlet的地址如:http://localhost:8080/myWebSite/hot
</servlet-mapping>
<servlet>
<servlet-name>hellogen</servlet-name>
<servlet-class>com.sina.HelloGen</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hellogen</servlet-name>
<url-pattern>/hotg</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>hellohttp</servlet-name>
<servlet-class>com.sina.HelloHttp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hellohttp</servlet-name>
<url-pattern>/hoth</url-pattern>
</servlet-mapping>
</web-app>