1. 程式人生 > >java web and vue.js 配合使用---servlet的配置

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許可權,不然呼叫失敗