how to over ride request object in ServletRequestWrapper? - java

I want to over ride the default getParameter() method of ServletRequestWrapper with getParameter() method of SecurityRequestWrapper.
For example, if I am using a simple jsp form to take the name of a person,
String name = request.getParameter("firstName");
I want the above getParameter() method to be from the SecurityRequestWrapper class. I am not able to understand how the request object is over riden since the getParameter method is mostly called on it by default in any jsp form.

I understand that the SecurityRequestWrapper you're talking about already implements HttpServletRequestWrapper? If so, then just create a Filter which is mapped on an url-pattern of *.jsp (or whatever you'd like to invoke this Filter for) and does basically the following in the doFilter() method.
chain.doFilter(new SecurityRequestWrapper((HttpServletRequest) request, response));

I might be wrong, but I do not think this is possible. Because request and response objects are created by the container and passed onto the servlet's process method. The very reason these objects are created by the container, because they want to flush the output and would like to control that. I will be interested to know however if it is possible to pass our own request / response objects.

Related

How to remove a parameter from HttpServletRequest request?

In my web application I am sending two parameters: action and productCode from JSP to Servlet. Based on that action some processing will happen.
Now after action is performed I am forwarding control to a JSP. The problem is that when the new JSP is opened the URL still contains the name of Servlet and the Parameters. So in case if someone refreshes the page, the same action will be performed again and again.
If somehow I am able to remove the parameters from URL then I handled a no parameter situation in servlet.
Can anyone please tell me how can I remove the parameter from request object?
You can't remove a parameter from a HttpServletRequest. The very definition of a parameter is that it came from the client (browser).
Perhaps you mean a request attribute ?
For that you can use:
request.getAttribute(String name)
request.setAttribute(String name, Object o)
request.removeAttribute(String name)
A forward operation is transparent to the client and forwards the request on to another handler for processing. Perhaps a forward is not exactly what you want to do.
For the record (if someone does not find the other questions/answers):
The good practice is to wrap the request object in another object
using a servlet filter. (...)
Subclass HttpServletRequestWrapper and override the getParameter
methods. (...)
You can't remove a parameter from a HttpServletRequest - but you can change its value by passing new values for this parameter.
For example, after login to hide a password value you can forward to next servlet/page this way:
"*/PAM_show_orders?orderDate=2020-04-16&**password=+++***".
What you can do is set that parameter to null and check before performing any operation if that attribute is set to null. This way you still will be able to use request forwarding.
For example:
request.setAttribute("Your_attribute",null);
checking for not null while performing action can be done using
String para=request.getAttribute("Your_attribute");
if(para.equals(null)){
//do this
}
else{
//do something else
}
When your request handler is over you can just use:
response.setParameter("action") = "";
response.setParameter("productCode") = "";
Hope this helps.

Why setParameter() is not available for ServletRequest?

I have read about Servlet's in many tutorials. The Servlet parameters has getParameter() method. As the parameter has return type as only String. Why the ServletRequest do not have setParameter() method ?
I have read about that only attributes in Servlet's can be changed, and the parameters can not. Can anyone explain the basic concept why there is no provision for updating the parameter in request with method like setParameter()?
As per ServletRequest documentation, it is mentioned that :
Defines an object to provide client request information to a servlet.
That means ServletRequest object is used to transfer client side information to server methods. For ex. text field values from jsp page to servlet's doGet or doPost method.
ServletRequest Object is an client request to a Servlet, and ServletResponse object is the response sent to the client so always you get the required information from the request and you set the information in the response............!
Application developers use parameter for getting information from client, where as attributes are used by application developer for overall internal management purposes. You can not change (setParameter()) the values given to you by clients in parameters; you can simply get those values using getParameter()

How to pass ModelMap from one controller to other in Spring

I would like to pass one ModelMap object from one controller to another one, but the problem is
1. one controller (say /upload) has been assigned to POST method i.e. #RequestMapping(value="/upload", method = RequestMethod.POST).
2. another controller (say /display) has been assigned to GET method i.e.
#RequestMapping(value="/display", method = RequestMethod.GET).
Flow of calling the contoller is from /upload to /display/. I mean after uploading the files I am redirecting it to /display controller. But as expected it give 405 error i.e. Method Not Supported Error. If both would have assigned to any one method i.e. either RequestMethod.POST or RequestMethod.GET so it would have been easier for me to pass the ModelMap object, by using forward in return statement.
So is there any approach so that I can fulfill my purpose. Passing object or value from Post to GET or vice versa. Any help would be appriciated. Thanks
First approach
Since the second method supports get request, why not use querystring like
/display?queryparam1=Hello&queryparam2=world
and you can get these values, using request object or QueryParam or PathParam
Second approach would be to put the objects in session under some pre-defined keys. And when the control falls on the second controller, the values from session can be fetched.

Java Servlet and Jquery

Im currently making a Java Servlet that can respond to jquery calls and send back data for my web page to use. But this is only a response using the doGet method.
Is there a way to have multiple methods in a Servlet and call them each with JQuery?
i.e. have a method called Hello and it returns a String "Hello" and another method called Bye and it returns a String "Bye". Is there a way using Jquery or some other technology to do this kind of thing?
Im quite new to servlets so Im still not sure what they are fully capable of. So is the doGet the only method to 'get in' and I just branch responses from there?
With Servlet you can either call the service method, so may be for your scenario you could pass the parameter to decide which method to invoke from doGet()
also you could identify if request is coming from AJAX using header check
There are other technologies available which will allow you directly invoke method See JSF, DWR
See
How to invoke a method with Openfaces/JSF without rendering page?
How to call a java method from jsp by clicking a menu in html page?
Personally I use reflection in my controllers(servlets) which basically let me achieve this.
If I have a servlet called UserController
The main url to call the servlet will be /user.
Knowing this, I always pass my first parameter as ?action=add
Then in my servlet I have a method called add or actionAdd. Whichever you prefer.
Then I use the following code;
String str = String str = request.getParameter("action").toLowerCase();
Method method = getClass().getMethod(str, HttpServletRequest.class, HttpServletResponse.class);
method.invoke(this, request, response);
Explanation:
str will have the action parameters value, add in this case.
Method method will be a reference to a method with the given name(str) and its expected parameter types.
I then invoke the method, passing the context, request and response.
The add method would look something like this;
public void add(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//do add stuff
String url = "/user/index.jsp";
RequestDispatcher dispatcher = context.getRequestDispatcher(url);
request.setAttribute("User", user);
dispatcher.forward(request, response);
}
I don't know about only passing back a string. But this should get you a basic idea.
Do note that reflection can cost you, altohugh it shouldnt really affect you much like this. And it is error prone as method names/signatures need to match perfectly.
So from jquery you would do an ajax request to the url:
localhost/projectname/user/add (if you use urlrewrite)
or
localhost/projectname/user?action=add (if you dont)
Servlet Container supports Custom Http methods since Servlet 3.0. For Ex,
public void doHello(HttpServletRequest req, HttpServletResponse res) {
//implement your custom method
}
The above method in Servlet can be invoked using hello http method.
But i am not sure if jquery has the support to invoke custom HTTP methods.
If it does not have, then the only option you have.
Invoke Servlet using GET and action parameter.
Read the action parameter and invoke the method using reflection.

When is doPut() called in a servlet?

Hi I was just curious when is the doPut() method in a servlet called. I know that if the form on a jsp/html page has a "post" method then the doPost() is called otherwise if it has a "GET" then the doGet() is called.When is the doPut() called ??
When an HTTP PUT request is received, naturally.
Can a page do a PUT request by code?
The only valid method attribute values of a <form> are get and post, according to the HTML5 spec. I assume that's what you're asking.
The doPut() method handles requests send by using the HTTP PUT method. The PUT method allows a client to store information on the server. For an example, you can use it to post an image file to the server. As the above answer says, goGet() and doPost() are in use, mostly. In my case, I use only these two, and I am getting only get requests, so I simply transfer the get request to doPost() and do my job easily.
if you want to send some confidential values in url via form you must use the post method, If you will use the get method for the form like login the values parameters like userid and password will be visible in url and anyone can hack that thing. So better to use post method in forms. By default it will call get method.
in get the url is like http://url?method=methodname&userid=123&password=123
so if you use post method the url will be like this http://url/methodname.do

Categories

Resources