nike_yunqi 2017-12-11 02:13 采纳率: 100%
浏览 1699

模拟MVC框架,servlet转发失效,急求原因

做了一个MVC模拟运行的程序,最后转发到JSP时明月任何反应,求解决方案!

 package com.bw.servlet;

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;

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

import com.bw.annontation.Lzp;
import com.bw.annontation.Mapper;

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

    private static final String PACKAGENAME = "com.bw.controller";

    /**
     * @see HttpServlet#HttpServlet()
     */
    public WorkServlet() {
        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
        response.getWriter().append("Served at: ").append(request.getContextPath());

        //获得请求路径
        String[] uri = request.getRequestURI().substring(1).split("/");
        //检索文件获得类名
        String path = request.getRealPath("/");

        String[] split = PACKAGENAME.split("\\.");
        File files = new File(path+"WEB-INF\\classes\\"+split[0]+"\\"+split[1]+"\\"+split[2]);

        //获得controller
        File[] listFiles = files.listFiles();

        File file = null;
        for (int i = 0; i < listFiles.length; i++) {
            file = listFiles[i];

            String name = file.getName();
            name = name.substring(0, name.length()-6);

            try {
                Class<?> loadClass = Class.forName(PACKAGENAME+"."+name);
                Object newInstance = loadClass.newInstance();
                Lzp lzpAnnotation = loadClass.getAnnotation(Lzp.class);
                //判断是否是控制器
                if ( lzpAnnotation != null ) {
                    Method[] methods = loadClass.getMethods();
                    for (Method method : methods) {
                        Mapper mapperAnnotation = method.getAnnotation(Mapper.class);
                        if ( mapperAnnotation != null ) {
                            String val = mapperAnnotation.url();
                            if(val.equals(uri[1])) {
                                try {
                                    Object invoke = method.invoke(newInstance);
                                    if (invoke!=null) {                                     
                                        String jsp = (String) invoke;
                                        System.out.println(jsp);
                                        request.getRequestDispatcher("/index.jsp").forward(request, response);
                                    }

                                } catch (IllegalArgumentException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                } catch (InvocationTargetException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        }
                    }

                }
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InstantiationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }


    }

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

}

  • 写回答

11条回答

  • nike_yunqi 2017-12-11 02:26
    关注

    在转发的这一行没有任何反应,不跳转页面,不报错

     if (invoke!=null) {                                        
                                            String jsp = (String) invoke;
                                            System.out.println(jsp);
                                            **request.getRequestDispatcher("/index.jsp").forward(request, response);**
                                        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型