I'm using cross-context to call a servlet in another server application: Servlet /bar from server application 'A' calls /foo servlet on server application 'B'.
I'm using this very nice solution, just as in the Abhijeet Ashok Muneshwar answer, I forward the request from server application A to the /foo servlet on server application B.
I'm using the class RequestDispatcher () to send a request, but the response is returned in the same call?
RequestDispatcher rd = context.getRequestDispatcher("/Servlet2");
rd.forward(request, response);
How can I process and return the response from server application B in A's servlet.
Thanks.
If you use a forward, that passes control to the target of the forward. The other option with a RequestDispatcher is to do an include.
If you want more control than that, you'll have to use an HTTP client to retrieve the response and then apply whatever processing you want to but using an HTTP client this way is not something I'd recommend. You'd be better off refactoring your application so you can use RequestDispatcher.include.
Related
While creating a servlet, i get confused between request and response methods.
What should be my perspective while writing a servlet, should i view the servlet and think as servlet is making its request and response or do the same from the client side.
For example,
Why is response.getWriter() from response side?
Similarly for certain response and request methods.
Don't get confused.
The request is coming from the client side.
The response is what the servlet going to send to the client side.
So you will need to write something to response most of the time. This is where you get a writer object. So response.getWriter() gives you the writer object.
The servlet does not itself create the response and request objects. The container creates them and forwards these objects to the matching servlet based on the client's http request.
I want to send values from jsp1.jsp to jsp2.jsp but redirect jsp1.jsp to jsp3.jsp . I used the following code in servlet for jsp1
response.sendRedirect("welcome1.jsp");
request.setAttribute("usern",user);
RequestDispatcher rd = request.getRequestDispatcher("afterlogin.jsp");
rd.forward(request,response);
but it keeps on giving this error "org.apache.jasper.JasperException: PWC6033: Error in Javac compilation for JSP".
you can use the possiblites of sessions in those case so you can access the parameter in jsp2 and redirect the jsp 1 to 3
https://www.javatpoint.com/servlet-http-session-login-and-logout-example
http://java.candidjava.com/tutorial/Servlet-Jsp-HttpSession-Login-logout-example.htm
You can use a HttpSession object as a user session, or use the ServletContext object for sharing global application information. Then use methods getAttribute (String attb) and setAttribute (String attb, String value) for sharing information within JSPs. The issue when you use a request is that the domain of the request is constraining you to use this information only when you receive this request.
You can also use JavaBeans to share information within JSPs
EDIT: Have you included your Java code inside a scriptlet? Using <% your code here %>
when you are using the sendRedirect function it will immediately redirect to that page it will not read next line code.
So according to your code your compiler is smart enough to check it so it is giving you an error.
please see below
1) SendRedirect ():
This method is declared in HttpServletResponse Interface.
Signature: void sendRedirect(String url)
1)In case of sendRedirect request is transfer to another resource to different domain or different server for further processing.
2)When you use SendRedirect container transfers the request to client or browser so URL given inside the sendRedirect method is visible as a new request to the client.
3)In case of SendRedirect call old request and response object is lost because it’s treated as new request by the browser.
4)SendRedirect is slower because one extra round trip is required because completely new request is created and old request object is lost.Two browser request required.
5)But in sendRedirect if we want to use we have to store the data in session or pass along with the URL.
2) Forward():
This method is declared in RequestDispatcher Interface.
Signature: forward(ServletRequest request, ServletResponse response)
1)When we use forward method request is transfer to other resource within the same server for further processing.
2)In case of forward Web container handle all process internally and client or browser is not involved.
3)When forward is called on requestdispather object we pass request and response object so our old request object is present on new resource which is going to process our request
4)Using forward () method is faster then send redirect.
5)When we redirect using forward and we want to use same data in new resource we can use request. setAttribute() as we have request object available.
more in : https://www.javatpoint.com/q/3577/difference-between-requestdispatcher-and-sendredirect
While developing JSP pages, we can run a Java code in server side, while JSP page is active in the client side...
How to create request and response cycle between Java Code(at the server side) and JSP page(at the client side)...
What is the difference between JSP and JSF technology for this kind of dynamic web pages?
And which one is better JSF or JSP?
You can use RequestDispatcher Method
In Server side create test.java you can write
ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/LoggedIn");
rd.forward(request, response);// used to forward data
if you want to include data use
rd.include(request,response);// used to include data
The common way to send information from java code to JSP is to set appropriate attributes of HttpServletRequest object. But you can't access JSP variables from java code because there is no sense in it. So it's "unidirectional communication", and when JSP page was sent to client only new HTTP request can help you.
Schematic HTTP request processing:
client -> servlet container -> servlet -> JSP -> HTML -> client
i am new to service and java.i will explain my workflow.
Client will call one synchronous service to service 1. service 1 do some operation and will delegate the session to service 2.then service 1 become free and ready to do some other work.service 2 do some operation and response back to client. is this possible like delegate the work to some other service and reply back to client. below Diagram makes you more clear.
in servlet we can redirect the response (response.sendredirect(url)) to some url like service or html page.but i think that response just redirect to some different url not the session right. so i want to know whether above workflow is possible ? if its possible please give some piece of code or ref link .
You can use RequestDispatcher class to forward the request to service 2;
RequestDispatcher rd = context_object.getRequestDispatcher('url to service 2');
rd.forward(request, response);
Inside service 2, you can use
request_object.getSession();
to get the original session object
How is it possible to call doGet() method from RequestDispatcher?
RequestDispatcher rd = sc.getRequestDispatcher("/CartServlet");
rd.forward(request, response);
This code calls doPost() as the default action.
It calls doPost() because your original request used POST method.
Generally servlets cannot "call" each other. They just can forward or redirect request. In both cases the same HTTP method that was used in original request is used.
If you want to call doGet() of other servlet it is the time to refactor your application, i.e. separate the logic implemented in doGet(), put it to other class and call this class from both servlets.
Check out below link, using HttpURLConnection to send request internally by POST or GET methods. I had felt the need for this for a long long time.
Java - sending HTTP parameters via POST method easily