Tag: servlet 3.0

从AJAX调用异步Servlet

我想要完成的并不是太复杂,但是由于我不精通AJAX,所以我遇到了一些麻烦。 当它实现时,我将有一个JSP,它有一个调用异步Servlet的按钮。 servlet将运行一个长时间运行的任务,并通过在完成部分任务时向表中添加行来向用户提供动态反馈。 在我尝试编写最终版本之前,我正在做一个概念validation,以了解它将如何工作。 但是,我遇到了麻烦。 当我在单击按钮时使用AJAX调用时,该函数在调用常规同步servlet时按预期工作。 但是,只要我使servlet异步,就不会显示更新。 是否有人能够提供一些有关出错的信息? 我的JSP看起来像这样: $(document).ready(function() { $(‘#mybutton’).click(function() { $.get(‘someservlet’, function(responseJson) { $.each(responseJson, function(index, item) { $(”).appendTo(‘#somediv’); $(”).text(item.row1).appendTo(‘#somediv’); $(”).text(item.row2).appendTo(‘#somediv’); $(”).text(item.row3).appendTo(‘#somediv’); $(”).text(item.row4).appendTo(‘#somediv’); }); }); }); }); Click to add things 我的异步Servlet doGet()方法如下所示: response.setContentType(“application/json”); response.setCharacterEncoding(“UTF-8”); final AsyncContext asyncContext = request.startAsync(); final PrintWriter writer = response.getWriter(); asyncContext.setTimeout(10000); asyncContext.start(new Runnable() { @Override public void run() […]

从HttpServletRequest获取AsyncContext

我正在使用Spring的OncePerRequestFilter重写shouldNotFilterAsyncDispatch方法来返回false。 这样它就可以处理异步请求。 在filter中我试图执行以下操作: if (isAsyncDispatch(request)) { request.getAsyncContext().addListener(new AsyncListener() { @Override public void onComplete(AsyncEvent event) throws IOException { System.out.println(“onComplete”); } @Override public void onTimeout(AsyncEvent event) throws IOException { } @Override public void onError(AsyncEvent event) throws IOException { System.out.println(“onError”); } @Override public void onStartAsync(AsyncEvent event) throws IOException { } }); } 所以isAsyncDispatch按预期返回true。 但是,当我尝试getAsyncContext它失败并出现以下exception: IllegalStateException: It is illegal […]

JAX-RS和长轮询

我正在尝试使用JAX-RS(Jersey实现)进行长轮询,但它不能像我预期的那样工作。 也许我误会了什么。 我很感激任何建议。 请注意,出于安全原因,使用反向连接(类似Atmosphere,Comet等)不是一种选择。 并不是说我目前正在开发Tomcat 7。 从JQuery Ajax调用(使用$.ajax )调用以下方法。 @Path(“/poll”) @GET public void poll(@Suspended final AsyncResponse asyncResponse) throws InterruptedException { new Thread(new Runnable() { @Override public void run() { this.asyncResponse = asyncResponse; // wait max. 30 seconds using a CountDownLatch latch.await(getTimeout(), TimeUnit.SECONDS); } }).start(); } 从我的应用程序调用另一个方法(在JMS调用之后): @POST @Path(“/printed”) public Response printCallback() { // … // […]

如何配置maven以使用servlet 3

我想升级我的webapp以使用servlet 3.0(插入2.5)。 我使用的是WebLogic Server版本:12.1.1.0(12c),maven,java 7_10和NetBeans 7.3.1 由于某种原因,唯一可用的servlet-api是3.0-alpha-1而不是3.0 4.0.0 javax javaee-web-api 6.0 provided javax.servlet servlet-api 3.0-alpha-1 provided javax.servlet.jsp jsp-api 2.1 provided jstl jstl 1.2 provided org.apache.myfaces.core myfaces-api 2.1.8 compile org.apache.myfaces.core myfaces-impl 2.1.8 compile

从java main方法调用servlet

import java.net.*; import java.io.*; public class sample { public static void main (String args[]) { String line; try { URL url = new URL( “http://localhost:8080/WeighPro/CommPortSample” ); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); line = in.readLine(); System.out.println( line ); in.close(); } catch (Exception e) { System.out.println(“Hello Project::”+e.getMessage()); } } } 我的Servlet正在调用另一个Jsp页面,如下所示, RequestDispatcher rd=request.getRequestDispatcher(“index.jsp”); rd.forward(request, response); 我没有在浏览器中得到任何反应/输出,一旦调用它就必须执行servlet。 […]

Glassfish – 上传图片 – 做得对

我是最新的glassfish(3.1.2) – 因此不需要apache FileItem和getPart()没有错误。 我读到上传图像的最佳做法是将它们保存在文件系统中(例如,请参见此处 )。 我正在编辑现有的代码 – 闻到了 – 所以我有想法: Part p1 = request.getPart(“file”); System.out.println(“!!!!!P1 : ” + p1); 印刷品: !!!!!P1 : File name=DSC03660.JPG, StoreLocation=C:\_\glassfish3\glassfish\domains\domain1\generated\jsp\elkethe\upload_7cb06306_138b413999a__7ffa_00000000.tmp, size=2589152bytes, isFormField=false, FieldName=file 我的新线。 在代码中人们正在做: if (request.getParameter(“crop”) != null) { // get path on the server String outputpath = this.getServletContext().getRealPath( “images/temp/” + session.getId() + “.jpg”); // store photo InputStream […]

java.lang.NoSuchMethodError:javax.servlet.http.HttpServletRequest.startAsync

知道为什么我在运行时遇到这个错误吗? 我正在尝试在Jetty上部署AsyncServlet。 java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.startAsync(Ljavax/servlet/ServletRequest;Ljavax/servlet/ServletResponse;)Ljavax/servlet/AsyncContext; at my.server.SlowServlet.doGet(SlowServlet.java:16) at javax.servlet.http.HttpServlet.service(HttpServlet.java:705) at javax.servlet.http.HttpServlet.service(HttpServlet.java:814) at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:547) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:480) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:520) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:227) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:941) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:409) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:186) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:875) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117) at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:250) at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:149) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:110) at org.eclipse.jetty.server.Server.handle(Server.java:345) at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:441) at org.eclipse.jetty.server.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:919) at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:582) at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:218) at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:51) at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:586) at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:44) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:598) at […]

基于注释的Spring / JAX-RS集成,没有web.xml

我正在尝试制作一个没有web.xml的Servlet 3.0 REST服务器,即仅依赖于java类和注释。 我不确定如何同时使用Spring和servlet映射注册Resource类。 我目前有: public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class[] getRootConfigClasses() { return new Class[]{RootConfiguration.class}; } @Override protected Class[] getServletConfigClasses() { return new Class[]{RestServletConfiguration.class}; } @Override protected String[] getServletMappings() { return new String[] {“/*”}; } } @Configuration @ComponentScan({“com.my.spring.beans”, “com.my.spring.services”}) public class RootConfiguration { } @Configuration @ComponentScan(“com.my.resource.classes”) public class RestServletConfiguration { } […]

在Servlet 3.0中以编程方式访问MultiPartConfig

我使用Servlet 3 @MultiPartConfig注释在我的应用程序中实现文件上传。 我需要在运行时设置multipart-config位置参数(而不是annotaion参数中的硬编码)。 是否有任何API可以编程访问servlet的multipart-config? 谢谢

Java – Servlet 3.0中的异步与Servlet 3.1中的NIO

直到现在,因为它适用于提供http请求,我认为术语 – asynchronous和non-blocking i/o意味着同样的事情。 但显然,它们分别在servlet 3.0和3.1中单独实现。 我在努力理解这里的差异…… 有人可以对这个话题有更多了解吗? 具体来说,我正在寻找一个服务器的servlet 3.0实现如何异步,但在一个线程上阻塞的例子? 我想如果我理解这一点,可能更容易理解servlet 3.1中的非阻塞i / o试图解决的确切问题。