java web and vue.js 配合使用---servlet的配置

这篇文章主要记录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权限,不然调用失败

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值