Java examples on javax.servlet
- How do I get servlet request URL information?
- How do I get web application context path?
- How do I read text file in Servlet?
- How do I get a spring's bean from a servlet?
- How do I get parameter names from servlet request?
- How do I get client IP and hostname in Servlet?
- How do I invalidate user's session?
- How do I create a HelloWorld Servlet?
- How do I count number of online users?
- How do I read servlet context initilization parameters?
- How do I create a hit counter servlet?
- How do I read request parameter from servlet?
- How do I create zip file in Servlet for download?
- How do I get servlet request headers information?
- How do I check if parameter is exists in servlet request?
- How do I send a response status in servlet?
- How do I get my web application real path?
- How do I set the maximum age of a cookie?
- How do I get a notification when session attribute was changed?
- How do I read servlet init parameter?
- How do I share object or data between users in web application?
- How do I know session last access time?
- How do I capture session creation and removal events?
- How do I read cookie in Servlet?
- How do I send a cookie in Servlet?
- How do I define welcome files for web application?
- How do I define a servlet with @WebServlet annotation?
- How do I define a filter using @WebFilter annotation?
- How do I obtain ServletContext of another application?
- How do I delete a cookie in Servlet?
How do I obtain ServletContext of another application?
The ServletContext.getContext(String uripath) enable us to access servlet context of another web application deployed on the same application server. A configuration need to be added to enable this feature.
In the example below we will forward the request from the current application to the /otherapp/hello.jsp page. We place a string in the request object attribute of the current application and going to show it in the hello.jsp page.
package org.kodejava.example.servlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = {"/context"})
public class GetAnotherContextServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//
// Get ServletContext of another application on the same Servlet
// container. This allow us to forward request to another application
// on the same application server.
//
ServletContext ctx = request.getServletContext().getContext("/otherapp");
//
// Set a request attribute and forward to hello.jsp page on another
// context.
//
request.setAttribute("MESSAGE", "Hello There!");
RequestDispatcher dispatcher = ctx.getRequestDispatcher("/hello.jsp");
dispatcher.forward(request, response);
}
}
To enable this feature in Tomcat we need to enable the crossContext attribute by setting the value to true, the default value is false. Update the server.xml file to add the following configuration.