Servlet创建的方式
一、实现Servlet 接口
package com.openlab;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class ServletDemo001 implements Servlet {
@Override
public void destroy() {
System.out.println("我主要用于对servle进行销毁");
}
@Override
public ServletConfig getServletConfig() {
System.out.println("我主要返回封装了servle配置信息的对象");
return null;
}
@Override
public String getServletInfo() {
return "我主要返回servle的相关信息";
}
//servlet初始化只有一次
@Override
public void init(ServletConfig arg0) throws ServletException {
System.out.println("我是初始化servlet的方法");
}
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
arg1.setContentType("text/html;charset=UTF-8");
arg1.getWriter().write("哈哈,我是servlet输出页面的信息");
System.out.println("主要用于对客户端的请求进行接受并处理...");
System.out.println(getServletConfig());
System.out.println(getServletInfo());
}
}
在web.xml中需要添加的配置
<!-- 配置ServletDemo001的相关配置 -->
<!-- 1、配置servlet的名字和创建类 -->
<servlet>
<servlet-name>firstServlet</servlet-name>
<servlet-class>com.openlab.ServletDemo001</servlet-class>
</servlet>
<!-- 2、配置servlet的路由 -->
<servlet-mapping>
<servlet-name>firstServlet</servlet-name>
<url-pattern>/servletDemo001</url-pattern>
</servlet-mapping>
在浏览器搜索框中输入 http://localhost:8080/MyWebDemo002/servletDemo001
二、继承GenericServlet类
package com.openlab;
import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class ServletDemo002 extends GenericServlet {
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
System.out.println("我是通过继承GenericServlet的方式创建的servlet...");
}
@Override
public void destroy() {
System.out.println("我是通过继承的方式创建的servlet被销毁了");
}
@Override
public ServletConfig getServletConfig() {
return super.getServletConfig();
}
@Override
public String getServletInfo() {
return "我是通过继承方式返回的";
}
@Override
public void init() throws ServletException {
System.out.println("我是通过继承的初始化");
}
}
在web.xml中需要添加的配置
<servlet>
<servlet-name>secondServlet</servlet-name>
<servlet-class>com.openlab.ServletDemo002</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>secondServlet</servlet-name>
<url-pattern>/servletDemo002</url-pattern>
</servlet-mapping>
在浏览器搜索框中输入 http://localhost:8080/MyWebDemo002/servletDemo002
三、继承HttpServlet方法(最常用的方式)
package com.openlab;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/servletDemo003") // 可以不创建web.xml,使用注解方式配置也可以。
public class ServletDemo003 extends HttpServlet {
@Override
//专门用于处理客户端的get请求,会自动执行
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("我是doGet方法,我正在处理客户端的get请求...");
}
@Override
//专门用于处理客户端的post请求,会自动执行
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("我是doPost方法,我正在处理客户端的post请求...");
}
}
在web.xml中需要添加的配置
<servlet>
<servlet-name>threeServlet</servlet-name>
<servlet-class>com.openlab.ServletDemo003</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>threeServlet</servlet-name>
<url-pattern>/servletDemo003</url-pattern>
</servlet-mapping>
在浏览器搜索框中输入 http://localhost:8080/MyWebDemo002/servletDemo003
get请求
post请求