weixin_33690963 2015-12-21 11:53 采纳率: 0%
浏览 58

Servlet出现EOF异常

I sending an Ajax request from a html to a servlet and returning a sample output to test the entire back end code. The front end is able to send the ajax request properly but the back end is not able to process the request properly. I have put print statements at various levels in java code to facilitate de bugging.

The doGet, doPost methods are in 1 file & service method is in another file for easier maintainability.

When i run the code on Tomcat server none of the print statements in the java code are getting executed. Instead i'm getting EOF exception in the tomcat log files.

Ajax code...

      send = function(req) {
    alert("Inside send function.");
    alert(JSON.stringify(req));
    $.ajax({
        url: '/flsv2/GetRequestsByUser',
        type:'GET',
        data: req,
        contentType:"application/json",
        dataType: "json",

        success: function(response) {
            alert("working");
        },

        error: function() {
            alert("not working");
            //alert(data);
        }
    });
};

Main servelet code(GetRequestsByUser.java)...

        @WebServlet("/GetRequestsByUser")
        public class GetRequestsByUser extends HttpServlet {
        private static final long serialVersionUID = 1L;
         /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */

         protected void doGet(HttpServletRequest httprequest,  HttpServletResponse httpresponse) throws ServletException, IOException {
          httpresponse.setContentType("application/json");
          System.out.println("Inside GET Method");

          doPost(httprequest,httpresponse);
          }

          protected void doPost(HttpServletRequest httprequest,  HttpServletResponse httpresponse) throws ServletException, IOException {
          // TODO Auto-generated method stub

        //HTTP request to Service request pojo 
        ObjectMapper mapper = new ObjectMapper();
        GetRequestsByUserReqObj request = mapper.readValue(httprequest.getInputStream(), GetRequestsByUserReqObj.class);
        httpresponse.setContentType("application/json");

        // application logic comes here --------        
        GetRequestsByUserResObj getResponse = null;

        try {
            //App handler to process request and create Service response pojo
            getResponse = (GetRequestsByUserResObj)  GetRequestsByUserHandler.getInstance().process(request);

            //Service response pojo to JSON

            PrintWriter out = httpresponse.getWriter();

            ObjectMapper mapper1 = new ObjectMapper();
            mapper1.writeValue(httpresponse.getOutputStream(), getResponse);
            httpresponse.setContentType("application/json; charset=UTF-8");

            } catch (NotImplementedException e) {
            e.printStackTrace();
            System.out.println("GetRequestsByUser process method not implemented");
            httpresponse.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, "GetRequestsByUser process method not implemented");
            } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }       
           }
         }

Service Method Code(GetRequestsByUserHandler.java)..

          public class GetRequestsByUserHandler implements AppHandler {

private String Id=null,operation,message;
private int Code,itemId,check=0,token=0;
private StoreModel sm;
private Response res = new Response();

private static GetRequestsByUserHandler instance = null;

public static GetRequestsByUserHandler getInstance() {
    if (instance == null)
        instance = new GetRequestsByUserHandler();
    return instance;
}

@Override
public ResObj process(ReqObj req) throws Exception {
    // TODO Auto-generated method stub

    GetRequestsByUserReqObj r = (GetRequestsByUserReqObj) req;

    System.out.println(r);
    //TODO: Core of the processing takes place here

    //Create the response
    GetRequestsByUserResObj response=new GetRequestsByUserResObj();

    //Populate the response

    response.setTitle("Sample");;
    response.setDesc("Sample Desc");
    response.setOwneruserId("");
    response.setRequestId(0);
    response.setDate("1/12/2015");
    response.setToken(0);


    //return the response
    return response;
}   

}

Tomcat Error Log...

          Dec 21, 2015 05:22:32 PM   org.apache.catalina.core.StandardWrapperValve invoke
           SEVERE: Servlet.service() for servlet [services.GetRequestsByUser] in context with path [/flsv2] threw exception
           java.io.EOFException: No content to map to Object due to end of  input
 at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2766)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2709)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1900)
at services.GetRequestsByUser.doPost(Unknown Source)
at services.GetRequestsByUser.doGet(Unknown Source)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
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)

I have gone through multiple tutorials & articles on servlets, jackson Mapper but still not able to figure out where I'm going wrong.. Please Help

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥25 matlab数值溢出该怎么解决?
    • ¥15 大家帮我看看为什么错了
    • ¥15 unity互动琴弦抖动效果
    • ¥15 做了个的二极管反向饱和电流测量电路,但是测试达不到效果
    • ¥15 nginx无证书访问https失败
    • ¥15 树莓派启动AP热点传入数据
    • ¥15 multisim中关于74ls192n和DSWPK开关的问题(相关搜索:计数器)
    • ¥15 在误装Windows server2019 后如何利用Windows.old恢复?
    • ¥20 代码实现状态连接包过滤防火墙的设计与实现
    • ¥15 vscode的红色箭头爆红和has no default export报错