是否可以将HttpServlet Request对象的内容复制到数组列表中?

时间:2015-09-29 06:56:07

标签: java web-services arraylist

我想访问HttpServlet Request的内容。因为我正在将HttpServlet Request对象分配到一个arraylist中。这是我写的网络服务代码,请在下面找到我的代码:

@Path("/login")
public class Login
{
    DBConnection dBConnection = new DBConnection();
     static  long logintimemilli;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String checkLogin(@QueryParam("username")String username,@QueryParam("password")String password,@QueryParam("clientid")String clientno,@QueryParam("callback")String callback,@Context HttpHeaders headers,@Context HttpServletRequest request)
    {

        JSONObject loginresult = new JSONObject();

        try
        {
            String query = QuerySelector.getQuery("user.checklogin");
            loginresult = dBConnection.queryForJSONObject(query, username,password,clientno);


                 ArrayList<String> user= new ArrayList <String>();
        HttpSession session = request.getSession();

      user   =  (ArrayList<String>) (List) session.getAttribute("USER");               
                insertauditList(user,ip);


        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return callback + "(" + loginresult.toString() + ")" ;
    }

   public void insertauditList(ArrayList<String> user,String ip)
    {     

       try
        {    


   for (String s : user) { System.out.printl(s);
}
  //(for the above for loop i am getting a runtime error)      

        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

    }







}

以下是我在运行程序后得到的异常:

29-Sep-2015 12:42:18.597 INFO [http-nio-8084-exec-16] org.apache.catalina.startup.HostConfig.undeploy Undeploying context [/MobileService]
java.lang.IllegalStateException: getAttribute: Session already invalidated
    at org.apache.catalina.session.StandardSession.getAttribute(StandardSession.java:1204)
    at org.apache.catalina.session.StandardSessionFacade.getAttribute(StandardSessionFacade.java:108)
    at com.zimmer.mobileservice.resources.Login.checkLogin(Login.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

根据问题标题,您要将HttpServletRequest内容复制到ArrayList,但在您的代码中,您尝试获取HttpSession并从HttpSession对象获取属性。在下面的代码中,您可以将请求参数映射复制到ArrayList

private void showRequestParamaters(HttpServletRequest req) {
    Map<String, String[]> parameterMap = req.getParameterMap();
    List<String> requestParams = new ArrayList<>();
    for (Entry<String, String[]> entry : parameterMap.entrySet()) {
        String[] paramValues = entry.getValue();
        if (paramValues != null) {
            for (String p : paramValues) {
                requestParams.add(p);
            }
        }
    }
    for (String s : requestParams) {
        System.out.println(s);
    }
}