在Jetty 9中使用doGET转发到另一个html页面时出错

时间:2016-06-09 20:58:56

标签: javascript servlets get server jetty

在Jetty 9上,我尝试使用 Javascript 向服务器发送一些数据:

var logInBtn = new sap.ui.commons.Button({text: "LogIn", press:function(){

        // Retrieve the information from the text fields
        var oParameters = {
              "userIDInput" : sap.ui.getCore().getElementById('userIDInput').getValue(),
              "passwordInput" : sap.ui.getCore().getElementById('passwordInput').getValue()
        };

        var oModel = new sap.ui.model.json.JSONModel();
        //console.log(oParameters);
        // Post data to the server
        oModel.loadData("10.161.34.8:8080", oParameters, true, 'GET');
        } 
    });

然后我想根据该数据转发当前页面。我可以收到数据,但我根本无法转发页面。 这是 servlet 中的 doGet 方法:

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//***I have tried the relative address as well***
         request.getRequestDispatcher("http://10.161.34.8:8080/UserAccount.html").forward(request, response);
    }

这是我的处理程序

    Server server = new Server(8080);

    ServletHandler ServHandler = new ServletHandler();
    ServHandler.addServletWithMapping(Servlet.class, "/*");

    FilterHolder cors = new FilterHolder(CrossOriginFilter.class);
    cors.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*");
    cors.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
    cors.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,POST,HEAD");
    cors.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "X-Requested-With,Content-Type,Accept,Origin");
    cors.setName("cross-origin");
    FilterMapping fm = new FilterMapping();
    fm.setFilterName("cross-origin");
    fm.setPathSpec("*");
    ServHandler.addFilter(cors, fm );

    ResourceHandler resource_handler1 = new ResourceHandler();
    resource_handler1.setDirectoriesListed(true);
    resource_handler1.setWelcomeFiles(new String[]{ "index.html"});//webpage to show up
    resource_handler1.setResourceBase("../../Documents/GitHub/ProjectASB/WebContent/");//The address of the content(. must be there)
    ContextHandler contextHandler1= new ContextHandler("/");
    contextHandler1.setHandler(resource_handler1);

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { contextHandler1, ServHandler, new DefaultHandler() });
    server.setHandler(handlers);

    server.start();
    server.join();

但是我得到了错误

2016-06-09 13:48:51.629:INFO:oejs.Server:main: jetty-9.1.1.v20140108
2016-06-09 13:48:51.640:INFO:oejsh.ContextHandler:main: Started o.e.j.s.h.ContextHandler@6e06451e{/,null,AVAILABLE}
2016-06-09 13:48:51.714:INFO:oejs.ServerConnector:main: Started ServerConnector@710726a3{HTTP/1.1}{0.0.0.0:8080}
2016-06-09 13:48:59.919:WARN:oejur.FileResource:qtp400136488-28: bad alias for C:\Users\i853260\Documents\GitHub\ProjectASB\WebContent\10.161.34.8:8080: java.io.IOException: The filename, directory name, or volume label syntax is incorrect (path C:\Users\i853260\Documents\GitHub\ProjectASB\WebContent\10.161.34.8:8080, working dir C:\Users\i853260\hdbstudio_rev\NoiseServer)
2016-06-09 13:48:59.926:WARN:oejs.ServletHandler:qtp400136488-28: /10.161.34.8:8080
java.lang.NullPointerException: while trying to invoke the method javax.servlet.RequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse) of a null object returned from javax.servlet.http.HttpServletRequest.getRequestDispatcher(java.lang.String)
    at servletPackage.Servlet.doGet(Servlet.java:35)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:717)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1644)
    at org.eclipse.jetty.servlets.CrossOriginFilter.handle(CrossOriginFilter.java:248)
    at org.eclipse.jetty.servlets.CrossOriginFilter.doFilter(CrossOriginFilter.java:211)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1615)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:550)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:481)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.HandlerList.handle(HandlerList.java:52)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.Server.handle(Server.java:459)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:281)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:232)
    at org.eclipse.jetty.io.AbstractConnection$1.run(AbstractConnection.java:505)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:607)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:536)
    at java.lang.Thread.run(Thread.java:812)

1 个答案:

答案 0 :(得分:1)

HttpServletRequest.getRequestDispatcher(String)走了一条路。

相对于当前servlet的路径,而不是完整的URL。

您可能还想在尝试使用getRequestDispatcher(String)之前在.forward()返回时测试null

//更新

由于您使用getRequestDispatcher()必须使用 ServletContextHandler(或更好,例如WebAppContext),因为getRequestDispatcher()需要有效的ServletContext才能运作。

使用原始ServletHandler对你来说不是一个好选择(你的需求已经超出了那个类)

  

ServletHandler实际上更像是ServletContextHandler的内部类,用于将ServletContext绑定到Servlet列表,以及您拥有的任何过滤器的调用链。< / p>

注意:

  • 不要混用ResourceHandlerServletContextHandler,他们做同样的事情,但会互相争斗。
  • baseResource中的ServletContextHandler定义ServletContext基本资源,DefaultServlet用于提供静态文件,以及用于特定于上下文的配置/资源查找
  • 使用DefaultServlet,它的光年好于原始ResourceHandler,并且可以更加配置。
  • 不要将servlet映射到/*,否则您将负责提供服务器中的所有内容,而不允许Jetty通过ServletContextHandler行为提供静态内容。< / LI>
public static void main(String args[]) throws Exception {
    Server server = new Server(8080);

    File webroot = new File("Documents/Project/WebContent");
    if (!webroot.exists())
        throw new FileNotFoundException("Unable to find webroot: " + webroot);

    ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/");
    context.setBaseResource(Resource.newResource(webroot));
    context.setWelcomeFiles(new String[] {"index.html"});

    context.addServlet(DummyServlet.class, "/dummy/");

    FilterHolder cors = new FilterHolder(CrossOriginFilter.class);
    cors.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*");
    cors.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
    cors.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,POST,HEAD");
    cors.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM,
        "X-Requested-With,Content-Type,Accept,Origin");
    context.addFilter(cors, "/dummy/*", EnumSet.of(DispatcherType.REQUEST));

    // Lastly, the default servlet for root content
    // It is important that this is last.
    ServletHolder holderPwd = new ServletHolder("default", DefaultServlet.class);
    holderPwd.setInitParameter("dirAllowed", "true");
    context.addServlet(holderPwd, "/");

    HandlerCollection handlers = new HandlerCollection();
    handlers.addHandler(context);
    handlers.addHandler(new DefaultHandler());

    server.setHandler(handlers);

    server.start();
    server.join();
}