gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:46:00 25 4
gpt4 key购买 nike

在 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)

最佳答案

HttpServletRequest.getRequestDispatcher(String) 采取一条路径。

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

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

//更新

由于您正在使用getRequestDispatcher()必须使用 ServletContextHandler (或者更好,例如 WebAppContext ),因为 getRequestDispatcher()需要有效的ServletContext发挥作用。

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

ServletHandler is really more of an internal class to ServletContextHandler anyway, used to bind the ServletContext to the list of Servlets, along with the call chain for any Filters you have.

注释:

  • 请勿混合 ResourceHandlerServletContextHandler ,他们做同样的事情,但会互相争斗。
  • baseResourceServletContextHandler定义 ServletContext基础资源,由 DefaultServlet 使用提供静态文件,以及用于上下文特定的配置/资源查找
  • 使用DefaultServlet ,它的光年比原始的好 ResourceHandler并且更加可配置。
  • 不要将 servlet 映射到 /* ,否则您将负责从服务器提供所有内容,不允许 Jetty 通过 ServletContextHandler 提供静态内容行为。
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();
}

关于javascript - 在 Jetty 9 中使用 doGET 转发到另一个 html 页面时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37736053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com