Thursday, September 18, 2008

Create new HttpServletRequest object in Servlet

Create new HttpServletRequest object in Servlet

Sometimes you not need to use default object of HttpServletRequest in Servlet, you need to create new object of HttpServletRequest, in this situation you can use listed below code to create new object of HttpServletRequest. in this code just use default object " request" of HttpServletRequest in "doPost" or "doGet" method.



public void doPost(HttpServletRequest request, HttpServletResponse res)
        throws IOException, ServletException {

HttpServletRequest hsr = (HttpServletRequest) request;
// now use hsr

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void doGet(HttpServletRequest request, HttpServletResponse res)
        throws IOException, ServletException {

HttpServletRequest hsr = (HttpServletRequest) request;
// now use hsr

}

1 comment:

  1. That doesn't actually create a new HttpServletRequest object, it just creates a new reference to the existing object.

    ReplyDelete