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.

CODE A

package itso.servjsp.servletapi;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ResourceHandler extends HTMLFormHandler {
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><TITLE>HTMLFormHandlerResource</TITLE></BODY>");
out.println("<H2>Servlet API Example -
HTMLFormHandlerResource</H2><HR>");
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
InputStreamReader(sc.getResourceAsStream("ResourceHandlerHTML.html")));
String str;
while ((str = in.readLine()) != null)
out.println(str);
in.close();
out.println("<HR></BODY></HTML>");
}
}

CODE B

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

ADO,131,ASP,3,C++,61,CORE JAVA,1,CSS,115,HTML,297,index,5,JAVASCRIPT,210,OS,47,PHP,65,SAD,53,SERVLETS,23,SOFTWARE ENGINEERING,245,SQL,71,TCP/IP,1,XHTML,9,XML,18,
ltr
item
Best Online Tutorials | Source codes | Programming Languages: Resource usage in Servlets
Resource usage in Servlets
Best Online Tutorials | Source codes | Programming Languages
https://www.1000sourcecodes.com/2012/05/resource-usage-in-servlets.html
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/2012/05/resource-usage-in-servlets.html
true
357226456970214079
UTF-8
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