Send XML data to client : XML Word PDF Mp3 « Servlet « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. JSP
22. JSTL
23. Servlet
24. Web Services SOA
25. Email
26. J2ME
27. J2EE Application
28. XML
29. Design Pattern
30. Log
31. Security
32. Apache Common
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Servlet » XML Word PDF Mp3 
23. 31. 3. Send XML data to client
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 responsethrows 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)
23. 31. XML Word PDF Mp3
23. 31. 1. Send Word file to client in a Servlet
23. 31. 2. Send MP3 file back to client with Servlet
23. 31. 3. Send XML data to client
23. 31. 4. Display PDF based on Request Parameter
23. 31. 5. Send XML to client using Servlet
23. 31. 6. Send Pdf to client using Servlet
23. 31. 7. Use XPath in servlet
w__w_w.___j_a_va__2s__.c___o___m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.