Resource usage in Servlets

In request dispatching, discussed above, we can only dispatch to another active application resource, such as another servlet. We described...

In request dispatching, discussed above, we can only dispatch to another active application resource, such as another servlet. We described passive application resources as elements such as HTML files.

We have shown that we can redirect to an application’s resources, such as HTML files. But how do we get access to this application’s resources directly, without having to redirect?

We can interact with an application’s passive resources through the servlet context. The ServletContext object allows us access to these resources through the getResource or getResourceAsStream methods:

❑  getResource: This method returns a URL object to a resource known to the servlet context, and we can access the resource as a URL object: 
URL url = servletContext.getResource("resourcefilename");

getResourceAsStream: This method allows us to read the resources body directly as an InputStream that we can manipulate: 
 InputStream is = servletContext.getResourceAsStream("resourcefilename");

The ResourceHandler servlet (code A) shows how we can implement these objects. In this example, we are accessing the HTML file shown in Code B. We reference it as a URL, through the getResource method, and also as an input stream, through the getResourceAsStream method. Note that only the TITLE tag of the servlet is displayed in the output, not the title of the included HTML file.


package itso.servjsp.servletapi;
import javax.servlet.*;
import javax.servlet.http.*;
public class ResourceHandler extends HTMLFormHandler {
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();
out.println("<H2>Servlet API Example -
ServletContext sc = getServletContext();
URL url = sc.getResource("HTMLFormHandlerRedirect.html");
out.println("URL name: " + url.getFile());
out.println("<HR> Now the input file html:");
BufferedReader in = new BufferedReader(new
String str;
while ((str = in.readLine()) != null)


<TITLE>Servlet Examples - ResourceHandlerHTML</TITLE>
<h4> Just a plain-old static HTML page </h4>

Best Online Tutorials | Source codes | Programming Languages: Resource usage in Servlets
Resource usage in Servlets
Best Online Tutorials | Source codes | Programming Languages
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content