#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    48
    Rep Power
    12

    Problems in servlet responding with an audio file


    Hi ,

    Could anyone please inform me about how can a servlet return an html file with background music in it?

    in.html is a normal html file that cn be viewed in a browser. Music was played. its working fine.
    But when i put the same code(in.html) as a response from a servlet, it isn't working. Could you please tell me how it can be done?

    This is how I tested it...
    I put in.html in C:/Tomcat/webapps/vegdeli2/
    I run Tomcat, and in browser window typed...
    http:\\localhost:8080\vegdeli2\in.html
    It worked fine(music was played)

    I put Test.java in
    C:/Tomcat/webapps/vegdeli2/web-inf/classes
    I run Tomcat and in browser window typed
    http:\\localhost:8080\vegdeli2\servlet\Test
    Music wasn't played

    I couldn't figure out why music isn't played from a servlet.

    CODE FOR TEST.JAVA..................
    -----------------------------------------
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.util.*;

    public class Test extends HttpServlet{

    private PrintWriter out;

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException{

    resp.setContentType("text/html");
    out=resp.getWriter();

    out.println("<HTML><BODY>");
    out.println("this is good");
    out.println("<bgsound SRC=\"moonlight3.midi\" LOOP=infinite>");
    out.println("</BODY></HTML>");
    }
    }


    CODE FOR IN.HTML........
    ------------------------------
    <HTML>
    <BODY>
    this is good
    <bgsound SRC="moonlight3.midi" LOOP=infinite>
    </BODY>
    </HTML>

    The audio file is moonlight3.midi

    Thanks in advance for ur help.
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    The most likely reason is the relative path you are using. When you use a relative path (a document-relative path in this case), the web browser makes an absolute path and then requests the info.

    (I am assuming that moonlight3.midi is located in the same folder as in.html.) When the src attribute for the bgsound tag is parsed for the in.html file, it makes a url something like this:
    Code:
    <bgsound SRC="moonlight3.midi" LOOP=infinite>
    
    gets turned into
    
    http://localhost:8080/vegdeli2/moonlight3.midi
    ----------------
    When it turns src attribute into a url for the servlet it makes this:
    
    http://localhost:8080/vegdeli2/servlet/moonlight3.midi
    As you can see, it is now requesting a servlet named moonlight3.midi. The easiest way to fix this is to use a root-relative path for the src attribute. Since you are in a web app you will need to get the contextpath and append it to the front of the link like this:
    Code:
    out.println("<bgsound SRC=\"" + request.getContextPath() + "/moonlight3.midi\" LOOP=infinite>");
    Look at the outputted html when you get it in your browser. It should read:
    Code:
    <bgsound SRC="/vegdeli2/moonlight3.midi" LOOP=infinite>
    Last edited by Nemi; March 23rd, 2003 at 08:05 PM.

IMN logo majestic logo threadwatch logo seochat tools logo