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

    Join Date
    Mar 2003
    Rep Power

    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...
    It worked fine(music was played)

    I put Test.java in
    I run Tomcat and in browser window typed
    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{


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

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

    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
    Lincoln, NE. USA
    Rep Power
    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:
    <bgsound SRC="moonlight3.midi" LOOP=infinite>
    gets turned into
    When it turns src attribute into a url for the servlet it makes this:
    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:
    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:
    <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