Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从servlet内部转发到web.xml路径?_Java_Servlets_Url Mapping - Fatal编程技术网

Java 如何从servlet内部转发到web.xml路径?

Java 如何从servlet内部转发到web.xml路径?,java,servlets,url-mapping,Java,Servlets,Url Mapping,在我的Servlet中,我希望通过调用web.xml中定义的路径来转发到另一个Servlet 例如,在我的web.xml中,我定义了: <servlet-mapping> <servlet-name>User</servlet-name> <url-pattern>/user/*</url-pattern> </servlet-mapping> 但是,当我转到应该启动此路径

在我的Servlet中,我希望通过调用
web.xml
中定义的路径来转发到另一个Servlet

例如,在我的
web.xml
中,我定义了:

    <servlet-mapping>
        <servlet-name>User</servlet-name>
        <url-pattern>/user/*</url-pattern>
    </servlet-mapping> 
但是,当我转到应该启动此路径的Servlet时,它返回的页面是空白的。也就是说,路径有问题。我知道它正在到达这段代码,因为我在它前面放了Sysout语句

有人知道为什么映射不正确吗?我尝试了每一种逻辑组合

已添加

这是应该处理此路径的Servlet代码

        String pathInfo = request.getPathInfo();            
        Pattern urlPattern = Pattern.compile("^/([^/]+)$");
        Matcher matcher = urlPattern.matcher(pathInfo);
        if (matcher.matches()){
            String userPath = matcher.group(1);
            my_proj.components.User user = UserManager.getUserByUsername(userPath);
            request.getSession().setAttribute("user", user);
            request.getRequestDispatcher("/resources/jsp/user_home.jsp").forward(request, response);
        }

通过使用
sendRedirect
而不是转发解决或至少避免了该问题


尝试远程调试:另外,尝试添加一些日志。@Arvind否,只是一个空白的白色页面,好像什么都没有发生(但调度上方的Sysout代码在控制台中打印)@Imray,sop行不会打印在页面上,请使用response.getWriter()从servlet(视图)打印页面上的内容。那么…您的模式匹配吗?调试您的应用程序。添加日志语句。验证变量的各种值。
        String pathInfo = request.getPathInfo();            
        Pattern urlPattern = Pattern.compile("^/([^/]+)$");
        Matcher matcher = urlPattern.matcher(pathInfo);
        if (matcher.matches()){
            String userPath = matcher.group(1);
            my_proj.components.User user = UserManager.getUserByUsername(userPath);
            request.getSession().setAttribute("user", user);
            request.getRequestDispatcher("/resources/jsp/user_home.jsp").forward(request, response);
        }
        String userPage = "/my_proj/user/" + username;
        response.sendRedirect(userPage);