我正在使用像这样的嵌入式码头:
Server server = new Server(7498);
URL url = Main.class.getClassLoader().getResource("html");
URI webRootUri = null;
try {
webRootUri = url.toURI();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ServletContextHandler context = new ServletContextHandler(
ServletContextHandler.SESSIONS);
context.setContextPath("/");
try {
context.setBaseResource(Resource.newResource(webRootUri));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
context.setWelcomeFiles(new String[] { "index.html" });
ServletHolder holderPwd = new ServletHolder("default",
DefaultServlet.class);
holderPwd.setInitParameter("cacheControl", "max-age=0,public");
holderPwd.setInitParameter("useFileMappedBuffer", "false");
holderPwd.setInitParameter("dirAllowed", "true");
context.addServlet(holderPwd, "/");
server.setHandler(context);
try {
server.start();
// server.dump(System.err);
} catch (Exception e1) {
e1.printStackTrace();
}
即我指向
src/main/resources
文件夹中的静态资源。现在如何处理发布参数?我正在发出ajax发布请求。
我知道
ServletContextHandler
具有handle
方法。我需要创建自己的类来扩展ServletContextHandler
吗? 最佳答案
您正在使用ServletContextHandler
,这是一种常见设置。
HttpServlet
ServletContextHandler
中.doPost()
方法POST
请求第2步更改从
main()
变为ServletContextHandler
的样子...context.addServlet(MyPostServlet.class, "/api");
MyPostServlet.java
可能看起来像这样public class MyPostServlet extends HttpServlet
{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
// Get POST parameters
String val = req.getParameter("foo");
// Do something with 'foo'
String result = backend.setValue("foo", val);
// Write a response
resp.setContentType("text/plain");
resp.setCharacterEncoding("UTF-8");
resp.getWriter().printf("foo = %s (result: %s)%n",val,result);
}
}
关于java - 嵌入式 jetty :获取后期参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34310069/