简体   繁体   English

从servlet转发到servlet

[英]Forward from servlet to servlet

Working on Front Controller for servlet-based application but cant find out how to forward from front contoller to regular controllers. 在基于servlet的应用程序的Front Controller上工作,但无法找到如何从Front contoller转发到常规控制器的方法。

Here is my web.xml: 这是我的web.xml:

<servlet>
    <servlet-name>FrontServlet</servlet-name>
    <servlet-class>FrontServlet</servlet-class>
</servlet>
<servlet>
    <servlet-name>IndexServlet</servlet-name>
    <servlet-class>application.controllers.IndexServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>FrontServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

FrontServlet FrontServlet

public class FrontServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context= getServletContext();
        RequestDispatcher rd = context.getRequestDispatcher("IndexServlet");
        rd.forward(request, response);
    }
}

This code returns: java.lang.NullPointerException . 这段代码返回: java.lang.NullPointerException I'm using an WebLogic server. 我正在使用WebLogic服务器。

  1. do you have a servlet mapping for IndexServlet 你有IndexServlet的servlet映射吗
  2. to send to a servlet you need path like "/IndexServlet.do" 要发送到servlet,您需要类似“ /IndexServlet.do”的路径

    this.getServletContext ( ) .getRequestDispatcher ( "/IndexServlet.do" ) .forward ( request , response ) ; this.getServletContext()。getRequestDispatcher(“ / IndexServlet.do”)。forward(请求,响应); or response.sendRedirect ( "/IndexServlet.do" ); 或response.sendRedirect(“ /IndexServlet.do”);

    assuming your mapping was like 假设您的映射就像

    <servlet-mapping> <servlet的映射>
    <servlet-name>IndexServlet</servlet-name> <servlet的名称> IndexServlet </ servlet的名称>
    <url-pattern>/IndexServlet.do</url-pattern> <URL模式> /IndexServlet.do </ URL模式>
    </servlet-mapping> </ servlet的映射>

    I have not tried using no extension at all instead of .do or . 我没有尝试过不使用任何扩展名来代替.do或。 but I would get it working with .do then experiment on changing it 但是我会让它与.do一起工作,然后尝试对其进行更改

You have not given servletMapping for the IndexServlet and requestDispatcher works on URLs. 您尚未为IndexServlet提供servletMapping,并且requestDispatcher可以在URL上使用。

Give a proper servletMapping in the web.xml, something like this: 在web.xml中提供适当的servletMapping,如下所示:

<servlet>
    <servlet-name>FrontServlet</servlet-name>
    <servlet-class>FrontServlet</servlet-class>
</servlet>
<servlet>
    <servlet-name>IndexServlet</servlet-name>
    <servlet-class>application.controllers.IndexServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>FrontServlet</servlet-name>
    <url-pattern>/frontServlet.html</url-pattern>
</servlet-mapping>


<servlet-mapping>
    <servlet-name>FrontServlet</servlet-name>
    <url-pattern>/indexServlet.html</url-pattern>
</servlet-mapping>

And in your FrontServlet.java, give the url as "/indexServlet.html". 然后在您的FrontServlet.java中,将网址指定为“ /indexServlet.html”。 It will work. 它会工作。 NullPointerException will be thrown if the view you are forwarding is not accessible. 如果您正在转发的视图不可访问,则将抛出NullPointerException。 Is your IndexServlet.java forwarding request to any jsp/html? 您的IndexServlet.java转发请求到任何jsp / html吗? In that case, check if the jsp/html is accessible using http://{webapp}/loginjsp.jsp. 在这种情况下,请使用http:// {webapp} /loginjsp.jsp检查jsp / html是否可访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM