Is the HttpServletRequest-object unique for each request? [duplicate] - java

This question already has answers here:
How do servlets work? Instantiation, sessions, shared variables and multithreading
(8 answers)
Closed 2 years ago.
I have implemented with a servlet with below doGet & doPost methods:
public void doGet(HttpServletRequest request, HttpServletResponse response){
// code
}
public void doPost(HttpServletRequest request, HttpServletResponse response){
// code
}
My question: Is the HttpServletRequest object in these methods unique for every unique ?
I am using request.setAttribute("att","value"); method. to save some values.
I want to know if I save an attribute in
first request, will be it present in the next request object. (Provided both requests are received at
almost same time)

No - its a new request every time - any attributes set in one request, will not be there when the next request comes in.
If you want to set attributes that are persistent across requests, you can use:
request.getServletContext().setAttribute("att","value");

Related

I'm trying to pass array parameters in query to servlet. But, I'm getting java.lang.IllegalArgumentException [duplicate]

This question already has answers here:
Send an Array with an HTTP Get
(2 answers)
Closed 2 years ago.
I'm refreshing my servlet knowledge by creating a simple api after many days.
While trying to supply array parameters in query for get request, I'm getting java.lang.IllegalArgumentException: Invalid character found error. I tried the same thing with spring framework before & was working perfectly. So, whats problem with servlet code.
Here is the request:
http://localhost:8080/HelloServlet/welcome?name[]=akshay,barpute.
Below is the servlet code for your reference.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// TODO Auto-generated method stub
Map<String, String[]> data = request.getParameterMap();
this.s = data.get("name")[0];
response.getWriter().append("Hello ").append(s);
}
Don't know about that spring feature, but the correct way to do this with a servlet is to repeat the name parameter:
http://localhost:8080/HelloServlet/welcome?name=akshay&name=barpute

I didn't success to call a servlet inside another servlet [duplicate]

This question already has answers here:
Sending redirect to another servlet/JSP without loosing the request parameters.
(2 answers)
doGet and doPost in Servlets
(5 answers)
Closed 2 years ago.
I have servlet A where there is this code :
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("gestion-avis");
rd.forward(request, response);
}
And then another servlet B which refers to "gestion-avis" :
#WebServlet("/gestion-avis")
public class GestionAvisServlet extends HttpServlet
The code of the A servlet normally redirect to the doGet method of the servlet B.
But this redirect me to an empty html page called "gestion-avis".
Thank you for your help !
This could be the reason for your issue.
Since you are forwarding request to another servlet mapped at url gestion-avis, the request goes to your GestionAvisServlet and its doGet method.
Now if your GestionAvisServlet.doGet method is not writing any data to HttpServletResponse, you will get a blank screen.
So to see some data either you need to write data to HttpServletResponse like this:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.getWriter().println("Hello");
}
Otherwise you can forward to another page.

Why someone calls post() method inside get() method in Servlets?

why we call post() method inside get() method in servlets?
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
Simply only because someone wants to have the same behavior disregarding the HTTP method whether it was POST or GET. So requesting resource with POST does the same as GET.
BUT: doing this - doing the same action - is quite propably wrong. Someone who does this might do it for convenience - for example wants to provide more means to access resource but does not fully understand the difference of GET vs. POST.
It is a matter of idempotency. Good explanation here.
In a nutshell GET should be used when GETting stuff and POSTing when you need to change stuff on the server side.
But what I have experienced some people use GET as long as there too much data for GET and then switch to POST without further thinking about the real difference.

Is this servlet thread safe?

I have passed the HttpServletRequest to another method in the servlet. Could I keep the servlet thread-safe? Is the below code thread-safe with er() method?
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Your session Id: ").append(er(request));
}
public String er(HttpServletRequest request){
return request.getSession().getId();
}
it is perfectly fine, you are not modifying any state within the Servlet itself, the servlet lifecycle creates one instance of the servlet and calls the init() method, any additional requests come through the same instance. so if you don't have any unprotected instance variables, you should be fine.
Because request.getSession() optionally creates a session, the answer is that you may have a race condition where to "simultaneous" calls from the same client may result in different session objects created for that client and consequently, different IDs returned.
See also here

servlet which can call a method and pass the value extracted from the web request

i have a question on how a servlet call a method and pass the value extracted from the web request. A scenario in which a web request is processed in a web request, i need to call a method and pass in the values extracted from the web request. When the method returns a value, send the value in the web response. thanks
If I understand you right you need something like this:
public class MyNewServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String param = request.getParameter("paramname");
String result = MyBusinessClass.myBusinessMethod(param);
response.getWriter().append("The answer is: " + result);
}
}
When request comes from browser, it will first read web.xml and call service method of appropriate servlet. Then service method decide to call doPost or doGet method on the basis of request. Once you get parameter , you need to create object of requestDispatcher and call forward method which will send your response.

Categories