JavaWEB:多个请求对应一个Servlet的解决方案

方案一:利用+参数请求的方式(存在安全隐患)

示例如下:

第一步:创建带参数的四个方法

JavaWEB:多个请求对应一个Servlet的解决方案_第1张图片

第二部:创建servlet:



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;

/**
 * Servlet implementation class UseServlet
 */
@WebServlet("/useServlet")
public class UseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UseServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String method=request.getParameter("method");
		System.out.println("Method:"+method);
		switch(method) {
		case "add":add(request,response);break;
		case "delete":delete(request,response);break;
		case "update":update(request,response);break;
		case "query":query(request,response);break;
		}
	}

	private void query(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		System.out.println("query");
	}

	private void update(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		System.out.println("update");
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		System.out.println("delete");
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		System.out.println("add");
	}

	/**
	 * @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);
	}

}

存在安全隐患:请求方法为get(),若被获得全部方法,可任意调用。

 

方案二:利用.do+反射机制

示例如下:

第一步:在html页面创建四个.do请求





Insert title here


Add
Update
Delete
Get

第二步:修改web.xml请求路径为*.do

JavaWEB:多个请求对应一个Servlet的解决方案_第2张图片 

第三步:修改Servlet

直接贴代码,代码内有注释

package com;

import java.io.IOException;
import java.lang.reflect.Method;

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


/**
 * Servlet implementation class LoginServlet
 */

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	// TODO Auto-generated method stub
	doPost(req,resp);
}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String contextPath=request.getServletPath();
        //System.out.println(contextPath);
		String methodname=contextPath.substring(1);//从第一个字符开始
		methodname=methodname.substring(0,methodname.length()-3);
		
		
		try {
			//利用反射获取methodname对应的方法
			Method method = getClass().getDeclaredMethod(methodname, HttpServletRequest.class,HttpServletResponse.class);
			//利用反射调对应的方法
			method.invoke(this,request,response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
	}
	public void add(HttpServletRequest request,HttpServletResponse reponse) {
		System.out.println("add");
	}
	public void delete(HttpServletRequest request,HttpServletResponse reponse) {
		System.out.println("delete");
	}
	public void query(HttpServletRequest request,HttpServletResponse reponse) {
		System.out.println("query");
	}
	public void update(HttpServletRequest request,HttpServletResponse reponse) {
		System.out.println("update");
	}

}

代码运行截图:

JavaWEB:多个请求对应一个Servlet的解决方案_第3张图片

你可能感兴趣的:(JavaWEB)