您的位置:首页 > 其它

一个servlet中通过反射动态调用多个方法

2014-07-16 15:49 357 查看
后台servlet3.0

package com.saic.grape;

import java.io.IOException;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.annotation.MultipartConfig;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.Part;

import org.springframework.util.StringUtils;

@WebServlet(name="UploadServlet" ,urlPatterns={"/upload"})

@MultipartConfig

public class UploadServlet extends HttpServlet{

/**

*

*/

private static final long serialVersionUID = 1L;

public void init(ServletConfig config)throws ServletException{

super.init(config);

System.out.println("init()方法被调用>>>>>");

}

/**

* servlet方法

*/

public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{

System.out.println("service()方法被调用>>>>>");

String opType = request.getParameter("opType");

try {

Class upload = Class.forName("com.saic.grape.UploadServlet");

/*第一种方式实现反射动态调用方法

* Method[] mes = upload.getDeclaredMethods();

for(Method method: mes){

String name = method.getName();

if(!StringUtils.isEmpty(opType) && name.equals(opType)){

method.invoke(upload.newInstance(),request,response);

break;

}

}*/

/**

* 第二种方式实现反射动态调用方法

*/

Method method = upload.getDeclaredMethod(opType, HttpServletRequest.class,HttpServletResponse.class);

String name = method.getName();

if(!StringUtils.isEmpty(opType) && name.equals(opType)){

method.invoke(upload.newInstance(),request,response);

}

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SecurityException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

} catch (InstantiationException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

}

}

/**

* 登录方法

* @param request

* @param response

* @throws IllegalStateException

* @throws IOException

* @throws ServletException

*/

public void login(HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException, ServletException {

System.out.println("login()登录方法被反射动态调用>>>>>");

Part part = request.getPart("file");

part.write("F:\\login."+UploadUtil.getFileType(part));

}

/**

* 注册方法

* @param request

* @param response

* @throws IllegalStateException

* @throws IOException

* @throws ServletException

*/

public void reg(HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException, ServletException {

System.out.println("reg()注册方法被反射动态调用>>>>>");

Part part = request.getPart("file");

part.write("F:\\reg."+UploadUtil.getFileType(part));

}

public void destroy() {

System.out.println("destroy()方法>>>>>");

}

}

html页面

<html>

<body>

<form method="post" enctype="multipart/form-data" action="upload">

<input type="file" id="file" name="file"/>

<input type="text" id="opType" name="opType"/> <!--指定方法-->

<input type="submit" value="上传"/>

</form>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: