import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.net.*;
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String file = "/WEB-INF/web.xml";
URL url = null;
URLConnection urlConn = null;
PrintWriter out = null;
BufferedInputStream buf = null;
try{
out = response.getWriter();
url = getServletContext().getResource(file);
response.setContentType("text/xml");
urlConn = url.openConnection();
urlConn.connect();
buf = new BufferedInputStream(urlConn.getInputStream());
int readBytes = 0;
//read from the file; write to the PrintWriter
while((readBytes = buf.read()) != -1)
out.write(readBytes);
} catch (MalformedURLException mue){
throw new ServletException(mue.getMessage());
} catch (IOException ioe){
throw new ServletException(ioe.getMessage());
} finally {
if(out != null)
out.close();
if(buf != null)
buf.close();
}
} //end doGet
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<servlet><servlet-name>MyServletName</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping><servlet-name>MyServletName</servlet-name>
<url-pattern>/index.html</url-pattern>
</servlet-mapping>
</web-app>
Download: ServletDisplayWebXML.zip( 89 k)