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

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.

Related

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

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");

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

Java Servlet doPost() returning null from Postman

I am doing a simple Java Servlet POST request without using any HTML and only using Postman. And the response from getParameter() is always null.
Here is the servlet:
#WebServlet("/api/form")
public class FormServlet extends HttpServlet {
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String orderNumber = req.getParameter("testString");
System.out.println(orderNumber);
resp.getWriter().print(orderNumber);
}
}
And a picture with responses and how I am doing it:
EDIT
As was commented by Mukesh Verma.
All I had to do was add #MultipartConfig Annotation and I got the data.
This is not how method getParameter works. As stated in this question, you should call the servlet with the following URL:
http://localhost:8080/api/form?testString=test
Try using #MultipartConfig Annotation. It handles form-data mime type.
Changing Postman's radiobutton from form-data to x-www-form-urlencoded also does the trick and I am able to get the data.

Extract URL from HttpServletRequest

I am maintaining a Java servlet application and now have to extract the URL from the web service request to determine what action to take depending on what URL called the web service. I have found that it is something to do with HttpServletRequest which I have imported to the class. I have tried setting up the following inside the web service end point but it keeps telling me that the urlrequest is not initialised. What am I doing wrong?
HttpServletRequest urlrequest;
StringBuffer url = urlrequest.getRequestURL();
The HttpServletRequest you are using should be the input parameter HttpServletRequest of either doGet,doPut,doPost or doDelete.
Then Surely HttpServletRequest.getRequestURL will reconstruct the URL used by the client,excluding the query string parameters.
Your code is correct but it has to be accessed within the doPost(request, response), doGet(request, response) etc. methods of a class extending HttpServlet.
The reason for this is the when the service() method of HttpServlet is called, it populates the request and response objects for you given the client who prompted the request to your servlet.
You cannot define a variable in java and call a method on it without initializing it beforehand.
In the first line: HttpServletRequest urlrequest; you are just defining a variable. Since it is not initialized it is null and you cannot use it.
Remove this line and use the argument passed to the doGet (or doPost) method in your Servlet.
For example if your servlet is like this:
public class MyServlet extends HttpServlet {
...
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws Exception {
...
}
Instead of your code just add below line in the body of the doGet method:
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws Exception {
...
StringBuffer url = request.getRequestURL();
...
}
After this line you should be able to use the url variable.

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