2 nike yunqi nike_yunqi 于 2017.12.11 10:13 提问

模拟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
nike_yunqi   2017.12.11 10:26

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

 if (invoke!=null) {                                        
                                        String jsp = (String) invoke;
                                        System.out.println(jsp);
                                        **request.getRequestDispatcher("/index.jsp").forward(request, response);**
                                    }
nike_yunqi
nike_yunqi   2017.12.11 10:28

我测试了一下,进入doGet以后直接转发跳转页面,服务端会报java.lang.StackOverflowError,我写的没有用到递归为什么会报异常呢?

nike_yunqi
nike_yunqi   2017.12.11 10:37

哪位路过的大神帮帮忙,我浏览器发送的请求是http://localhost:8080/WorkWebOne/list.do ,下面是controller代码

 package com.bw.controller;

import javax.servlet.http.HttpServletRequest;

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

@Lzp
public class WorkController {

    @Mapper(url="test.do")
    public void test() {
        System.out.println("成功");
    }

    @Mapper(url="list.do")
    public String list() {

        System.out.println("列表");

        return "list.jsp";
    }
}

nike_yunqi
nike_yunqi   2017.12.11 10:42

返回控制台打印的是两句话:
列表 ------表示controller已经执行了列表方法
list.jsp ----这是方法返回值的字符串,把这个放入request.getRequestDispatcher("/"+jsp).forward(request, response);中没有反应,写死了request.getRequestDispatcher("/index.jsp").forward(request, response);也不能跳转,list.jsp 与index.jsp都是直接放在/WorkWebOne/WebContent下的。下面是JSP代码,自动生成的,空白JSP,还没来的及写页面渲染,以防有我不知道的问题,也贴出来

 <?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
    list.jsp   啦啦啦啦

</body>
</html>
 <?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
index  啦啦啦啦
</body>
</html>
nike_yunqi
nike_yunqi   2017.12.11 10:45

测试以下代码会报错:

 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());
        request.getRequestDispatcher("/index.jsp").forward(request, response);

    }

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

}

nike_yunqi
nike_yunqi   2017.12.11 10:50

会报以下异常,因为太长无法展示,只展示部分,大部分都是重复的:

 Exception in thread "http-bio-8080-exec-3" java.lang.StackOverflowError
    at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228)
    at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228)
    at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228)
    at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228)
    at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228)

    at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228)
    at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228)
    at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228)
    at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228)
    at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228)
    at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228)
    at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228)
    at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228)
    at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:389)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
    at com.bw.servlet.WorkServlet.doGet(WorkServlet.java:44)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
    at com.bw.servlet.WorkServlet.doGet(WorkServlet.java:44)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
    at com.bw.servlet.WorkServlet.doGet(WorkServlet.java:44)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
    at com.bw.servlet.WorkServlet.doGet(WorkServlet.java:44)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
    at com.bw.servlet.WorkServlet.doGet(WorkServlet.java:44)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)

wb_snail
wb_snail   2017.12.11 10:52

你调试doGet方法 到底是request.getRequestDispatcher 到这里之后没有任何反应,还是说抛出栈溢出

nike_yunqi
nike_yunqi 我发了两个代码,是两次执行,首条消息的执行到request.getRequestDispatcher 没有反应,第二条doGet里面只有request.getRequestDispatcher 时会抛出栈溢出
大约一个月之前 回复
nike_yunqi
nike_yunqi 我调用doGet以后执行controller,执行到request.getRequestDispatcher 没有任何反应,doGet里面直接执行requestrequest.getRequestDispatcher 抛异常
大约一个月之前 回复
fengqingyuebai19
fengqingyuebai19   2017.12.11 11:34

去掉@WebServlet("/workServlet")

在web.xml中配,我测试代码了,没什么问题
另外你配viewresolver了吗?

nike_yunqi
nike_yunqi web.xml已贴出,就是用的自己的servleto(╥﹏╥)o
大约一个月之前 回复
fengqingyuebai19
fengqingyuebai19 把web.xml的servlet改成你的servlet就行啦
大约一个月之前 回复
fengqingyuebai19
fengqingyuebai19 回复nike_yunqi: http://blog.csdn.net/fengqingyuebai19/article/details/78661891
大约一个月之前 回复
nike_yunqi
nike_yunqi viewresolver没有配置啊,这个不懂什么意思,以前MVC都是自动封装的,现在自己写怎么弄呢?
大约一个月之前 回复
dcxy0
dcxy0   Ds   Rxr 2017.12.11 12:44

你估计是判断没进去,你试试下面这样写呢:

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     request.getRequestDispatcher("/index.jsp").forward(request, response);
  }
dcxy0
dcxy0 回复nike_yunqi: 不会把,你是参照什么写的吗
大约一个月之前 回复
nike_yunqi
nike_yunqi 这样写会抛栈溢出o(╥﹏╥)o
大约一个月之前 回复
nike_yunqi
nike_yunqi   2017.12.11 14:56

下面是我的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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
 <servlet>
    <servlet-name>WorkServlet</servlet-name>
    <servlet-class>com.bw.servlet.WorkServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>WorkServlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>Filter</filter-name>
    <filter-class>com.bw.filter.Filter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>Filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
共11条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!