Servlet forward() from POST to GET - servlets

I'm using a simple servlet that receives a POST request and forwards it to another servlet that expects a GET request.
I'm aware as original HtppServletRequest is having POST as it's method, thus it's failing to forward. Also, the HtppServletRequest method property doesn't have a setter method, only way out i can see is to create a new request object with the method as GET and set the remaining contents same as the original request.
Is there any better way of doing it?

Related

setAttribute() and getParameter(), is this right?

I'm working on an servlet based project where I found that in a place, a value is set using request.setAttribute("") and in another place, that value is retrieved using request.getParameter(""). Is this right?
I know the difference between the getParameter and getAttribute. But the retrieved value intermittently becomes null.
Only those values can be retrieved using request.getAttribute which are set using request.setAttribute. And query string in GET request or request prameters in POST request can be retrieved using request.getParameter. There is no method as request.setParameter in Servlet API. Now coming to the intermittent behavior, check the URL/AJAX request on each request to server and see exactly when it has the property and its value set which you are trying to retrieve using getParameter method. Hope this clarify your issue.

Manually setting the redirectattributes object

I have a POST controller method (say updateRow) that has RedirectAttributes as a parameter, since I am doing a POST to GET redirection after the method is executed. In the redirect GET method, I am accessing the flash attributes using RequestContextUtils.getInputFlashMap(). This works fine.
However, there is a scenario where I need to invoke this POST controller method from within another method. The idea is to pass a RedirectAttributes object that can act as the flash attribute carrier inside the "updateRow" method.
I tried using a new RedirectAttributesModelMap object, which helps in the successful execution of the "updateRow" method, but in the POST-to-GET method, the flash attributes are not available.
Is there a way I can manually define this RedirectAttributesModelMap object and bind it to the request/session?

Does Velocity template has implicit request object?

I'm rephrasing my existing question to a more generic one. I want to know if Velocity has got implicit object references like JSP does.
I'm particularly interested in knowing about the request object.
In JSP we can get the attribute in the request scope like <%= request.getAttribute("req1") %>
I know that JSP is a servlet and <%= request.getAttribute("req1") %> ends up as a part of _jspService() method which has the request object available to it before the scope of the request ends.
I'm not sure how Velocity works behind the scenes (it may be leaving the request object behind by the time it plays it role)
To test that I did the following thing which was a the part of my previous question.
I have a Spring MVC TestController in which I'm setting a request attribute. I'm using Velocity templates for rendering the views.
#RequestMapping(value="/test", method=RequestMethod.GET)
public ModelAndView display(HttpServletRequest req, HttpServletResponse resp){
...
req.setAttribute("req1", "This should be present for first request");
...
}
In the Velocity template I'm doing something like
Request: $request.getAttribute('req1')
but I'm not getting the value of req1. I know I should have put req1 in model map instead of request but I want to know about implicit request object ref.
I tried $req1 as well but its not working.
When I'm doing the same thing with the model and returning it back, everything is working correctly.
Where am I going wrong?
Update: The same thing is happening with req.getSession().setAttribute("req1", testObject) also.
Salaam,
req.getSession().getAttribute("req1", testObject) == $req1
AFAIK, you cannot access the request object at VelocityViewServlet's templates, unless you explicity set the request object in context or use a v-tool .
Take a look at this question: Velocity + Spring. The Spring folks haven't kept the integration with Velocity very up to date.
Once you've created that extension and set it up to be used properly in your servlet configuration, you'd be able to simply put the object on the ModelAndView and from there do whatever you need with it.

How to get request type (master/sub) in Symfony2 controller?

Is there possible get request type in controller? How?
To detect if the request is a master or not requires the use of the RequestStack, which should be injected into your controller. The request stack has 3 useful methods
getCurrentRequest();
getMasterRequest();
getParentRequest();
The getParentRequest() will always return null if the current request is the master.
I was looking for this myself, and it seems it is just passed around, so there doesn't seem to be one single place that knows what it is.
My thought for solving this would be to create a simple kernel.request listener that just adds an attribute to the request. Rough (un-tested) code below:
public function onKernelRequest(GetResponseEvent $event)
{
$event->getRequest()->attributes->set('_request_type', $event->getRequestType());
}
Then in the controller you should be able to do:
$requestType = $this->getRequest()->attributes->get('_request_type');
Again this is untested. You would need to write out the full listener class and add it to the services config file, but other than that I think this will work.
Easy, just call the getMethod() method on your Request object:
$method = $this->get('request')->getMethod();
This will return the HTTP method of the current request, e.g. GET, POST, PUT or DELETE.

ASP.NET MVC - How To Refresh View From Actionmethod as a response to request?

I have an action-method in a controller that takes requests coming from a variety of different views.
It is somewhat of a utility method and I simply want it to accept the parameters it is given - do something - and then refresh the view that sent the request.
Right now, the only way I see to do this is by having the method figure out what view sent it the info and do a:
return RedirectToAction("method", "controller");
For each possibility (or something similar to that).
Is there a more general way I can make my method just re-render the current view without having to explicitly identify it?
-Thanks
Your best bet is to use jQuery to post the data then utilize the results as you see fit. Otherwise you can pass in the action/controller name in the post and use them dynamically to redirect.

Resources