这篇文章主要记录java中使用servlet做服务器端编程,为vue.js等前端构建接口.能够在一个servlet中根据请求参数处理多个请求.(并不需要写jsp页面)


步骤如下

  1. 基类servlet,使用反射,用来被其他servlet继承,这样子类servlet就能具备根据请求参数处理处理请求的功能.
  2. 子类,继承自基类servlet.
  3. web.xml设置
1.基类baseServlet.java, 重写service方法即可
 @Override
    protected void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
        //得到请求url的参数信息
        String name=request.getParameter("method");
        if(name==null || name.isEmpty()){
            throw new RuntimeException("请传入请求的方法名成");
        }
        //得到这个类的对象实例
        Class class1=this.getClass();

        Method method=null;

            //得到方法
            method = class1.getMethod(name,HttpServletRequest.class,HttpServletResponse.class);
        } catch (Exception e) {
            throw new RuntimeException("没有"+name+"方法");
        }

        try {
            //调用方法
            method.invoke(this,request,response);
        } catch (Exception e) {
            throw new RuntimeException("方法运行中出错");
        }
    }
2.子类ChildServlet.java, 可以在这个方法里面定义自己的处理的方法
package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class InheritReflectServlet
 */
@WebServlet("/InheritReflectServlet")
public class InheritReflectServlet extends BaseServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public InheritReflectServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter pWriter=response.getWriter();
        pWriter.println("hello doGet");
    }

    /**
     * 这些方法要是公共的
     */
    public void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html");
        PrintWriter pWriter=response.getWriter();
        pWriter.println("hello addUser");
    }

    /**
     * 这些方法要是公共的
     */
    public void deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html");
        PrintWriter pWriter=response.getWriter();
        pWriter.println("hello deleteUser");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
3.web.xml设置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>ChildServlet</servlet-name>
    <servlet-class>servlet.ChildServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ChildServlet</servlet-name>
    <url-pattern>/myServlet/ChildServlet</url-pattern>
  </servlet-mapping>
</web-app>

子类中自己的方法应该是public权限,不然调用失败

Logo

前往低代码交流专区

更多推荐