Servlet体系结构
我们将来开发B/S架构的web项目,都是针对HTTP协议,所以我们自定义Servlet,会继承HttpServlet
案例演示1
新建ServletDemo04
package com.taotao.web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/demo4")
public class ServletDemo04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("get...");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("post...");
}
}
run maven运行测试
打开项目访问路径
输入demo4
演示service方法处理get和post
新建ServletDemo05
package com.taotao.web;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
public class ServletDemo05 implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
@SuppressWarnings({"all"})
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
String method = request.getMethod();
if("GET".equals(method)){
}else{
}
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
新建工具类
新建MyHttpServlet
package com.taotao.web;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
public class MyHttpServlet implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
String method = request.getMethod();
if("GET".equals(method)){
doGet(servletRequest,servletResponse);
}else{
doPost(servletRequest,servletResponse);
}
}
protected void doPost(ServletRequest servletRequest, ServletResponse servletResponse) {
}
protected void doGet(ServletRequest servletRequest, ServletResponse servletResponse) {
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
新建测试类,继承工具类
ServletDomo05
package com.taotao.web;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
@WebServlet("/demo5")
public class ServletDemo05 extends MyHttpServlet{
@Override
protected void doGet(ServletRequest servletRequest, ServletResponse servletResponse) {
System.out.println("get...");
}
@Override
protected void doPost(ServletRequest servletRequest, ServletResponse servletResponse) {
System.out.println("post...");
}
}
测试运行
总结
|