//Using a Servlet to Look at the Context Path and get the content bellow it
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* ShowContext - a servlet to display the context name, and show
* all the resource in the web application.
* It even shows off its own source.
*
*/
public class ShowContext extends HttpServlet {
public static final String LT_SYMBOL = "<";
public static final String GT_SYMBOL = ">";
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
/* Set the response type, retrieve the writer */
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.write("<HTML><HEAD><TITLE>Show Context Servlet</TITLE></HEAD><BODY>");
out.write("<H1>Show Context Servlet</H1>");
/* The context path name is in the request */
String ctxPath = request.getContextPath();
out.write("<H2>The name of the context is " + ctxPath + "</H2>");
/* This is the technique for getting the root path of the context */
ServletContext sc = getServletContext();
String ctxRealPath = sc.getRealPath("/");
out.write("<H2>The name of the root path for the context is "
+ ctxRealPath + "</H2>");
/* List the file contents at the root of the context */
out.write("<H3>A listing of files & directories from the context root follows...</H3>");
File f = new File(ctxRealPath);
listFiles(f, out);
/* List this source file */
out.write("<H3>A source listing of this servlet follows...</H3>");
out.write("<CODE>");
f = new File(sc
.getRealPath("/WEB-INF/src/tutorial/servlets/ShowContext.java"));
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
while ((line = br.readLine()) != null) {
line = clean(line);
out.write("<BR />" + line);
}
out.write("</CODE>");
/* Tidy up */
out.write("</BODY></HTML>");
out.flush();
out.close();
}
/*
* Lists all files from given location downwards; designed to be called
* recursively.
*/
private void listFiles(File f, PrintWriter out) {
if (f.isFile()) {
out.write("<BR />File: " + f.getPath());
return;
}
if (f.isDirectory()) {
out.write("<BR />Directory: " + f.getPath());
File[] files = f.listFiles();
for (int i = 0; i < files.length; i++) {
listFiles(files[i], out);
}
}
}
/*
* Cleans out the " <" and ">" symbols in the code and substitutes a
* browser-friendly equivalent. Otherwise, the symbols in the code listing
* are treated as HTML tags!
*/
private String clean(String line) {
int index;
StringBuffer sb = new StringBuffer(line);
while ((index = sb.indexOf(LT_SYMBOL)) > -1) {
sb.replace(index, index + 1, "&");
sb.insert(index + 1, "lt;");
}
while ((index = sb.indexOf(GT_SYMBOL)) > -1) {
sb.replace(index, index + 1, "&");
sb.insert(index + 1, "gt;");
}
return sb.toString();
}
}
//example getting init parameters
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ShowInitParams extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
/* Set the response type, retrieve the writer */
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.write("<HTML><HEAD><TITLE>" + "Show Initialization Parameters "
+ "Servlet</TITLE></HEAD><BODY>");
out.write("<H1>Show Initialization Parameters Servlet</H1>");
/* Retrieve the names and display names and values */
Enumeration paramNames = getInitParameterNames();
int parmCount = 0;
while (paramNames.hasMoreElements()) {
String name = (String) paramNames.nextElement();
String value = getInitParameter(name);
out.write("<H4>The parameter with name " + name
+ " has a value of " + value + "</H4>");
parmCount++;
}
if (parmCount == 0) {
out
.write("<H2><FONT color=red>There were no initialization parameters!</FONT></H2>");
}
/* Tidy up */
out.write("</BODY></HTML>");
out.flush();
out.close();
}
}
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* ShowContext - a servlet to display the context name, and show
* all the resource in the web application.
* It even shows off its own source.
*
*/
public class ShowContext extends HttpServlet {
public static final String LT_SYMBOL = "<";
public static final String GT_SYMBOL = ">";
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
/* Set the response type, retrieve the writer */
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.write("<HTML><HEAD><TITLE>Show Context Servlet</TITLE></HEAD><BODY>");
out.write("<H1>Show Context Servlet</H1>");
/* The context path name is in the request */
String ctxPath = request.getContextPath();
out.write("<H2>The name of the context is " + ctxPath + "</H2>");
/* This is the technique for getting the root path of the context */
ServletContext sc = getServletContext();
String ctxRealPath = sc.getRealPath("/");
out.write("<H2>The name of the root path for the context is "
+ ctxRealPath + "</H2>");
/* List the file contents at the root of the context */
out.write("<H3>A listing of files & directories from the context root follows...</H3>");
File f = new File(ctxRealPath);
listFiles(f, out);
/* List this source file */
out.write("<H3>A source listing of this servlet follows...</H3>");
out.write("<CODE>");
f = new File(sc
.getRealPath("/WEB-INF/src/tutorial/servlets/ShowContext.java"));
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
while ((line = br.readLine()) != null) {
line = clean(line);
out.write("<BR />" + line);
}
out.write("</CODE>");
/* Tidy up */
out.write("</BODY></HTML>");
out.flush();
out.close();
}
/*
* Lists all files from given location downwards; designed to be called
* recursively.
*/
private void listFiles(File f, PrintWriter out) {
if (f.isFile()) {
out.write("<BR />File: " + f.getPath());
return;
}
if (f.isDirectory()) {
out.write("<BR />Directory: " + f.getPath());
File[] files = f.listFiles();
for (int i = 0; i < files.length; i++) {
listFiles(files[i], out);
}
}
}
/*
* Cleans out the " <" and ">" symbols in the code and substitutes a
* browser-friendly equivalent. Otherwise, the symbols in the code listing
* are treated as HTML tags!
*/
private String clean(String line) {
int index;
StringBuffer sb = new StringBuffer(line);
while ((index = sb.indexOf(LT_SYMBOL)) > -1) {
sb.replace(index, index + 1, "&");
sb.insert(index + 1, "lt;");
}
while ((index = sb.indexOf(GT_SYMBOL)) > -1) {
sb.replace(index, index + 1, "&");
sb.insert(index + 1, "gt;");
}
return sb.toString();
}
}
//example getting init parameters
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ShowInitParams extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
/* Set the response type, retrieve the writer */
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.write("<HTML><HEAD><TITLE>" + "Show Initialization Parameters "
+ "Servlet</TITLE></HEAD><BODY>");
out.write("<H1>Show Initialization Parameters Servlet</H1>");
/* Retrieve the names and display names and values */
Enumeration paramNames = getInitParameterNames();
int parmCount = 0;
while (paramNames.hasMoreElements()) {
String name = (String) paramNames.nextElement();
String value = getInitParameter(name);
out.write("<H4>The parameter with name " + name
+ " has a value of " + value + "</H4>");
parmCount++;
}
if (parmCount == 0) {
out
.write("<H2><FONT color=red>There were no initialization parameters!</FONT></H2>");
}
/* Tidy up */
out.write("</BODY></HTML>");
out.flush();
out.close();
}
}
No comments:
Post a Comment