CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2007
    Posts
    63

    strange servlet problem

    hi

    i m a total servlet n00b...so i dont quite understand what is happening here.

    i got a javascript from the net ( http://scripts.hashemian.com/js/countdown.js )

    i saved it onto my hard-drive. as countdown.js...

    i then wrote a servlet program whose code is shown below:
    [/b](case1)[/b]
    Code:
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    
    public class Execute extends HttpServlet
    
    {
    
    	public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
    
    	{
    
    		res.setContentType("text/html");
    		PrintWriter out=res.getWriter();
    		out.println("<html>");
    		out.println("<head>");
    		out.println("</head>");
    		out.println("<body>");
                    out.println("<script language=\"JavaScript\">");
                    out.println("TargetDate = \"1/1/2009 12:00 AM\"");
                    out.println("BackColor = \"palegreen\"");
                    out.println("ForeColor = \"navy\"");
                    out.println("CountActive = true");
                    out.println("CountStepper = -1");
                    out.println("LeadingZero = true");
                    out.println("DisplayFormat = \"%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds. for new years\"");
                    out.println("FinishMessage = \"It is finally here!\"");
                    out.println("</script>");
                    out.println("<script language=\"JavaScript\" src=\"countdown.js\"></script>");		
    		out.println("<p>it works!!</p>");
    	        out.println("</body></html>");
    	        out.close();
    
             }
    
    }
    the java file(Execute.java) and javascript(countdown.js) are in the same folder...sitting beside each other.

    i then compile the java file and i then create a .war file...
    the war file, i deploy on the apache-tomcat server, using the tomcat manager. but when i run the servlet, the javascript does not run.
    (case2)
    when i set "src=" as ,

    Code:
    out.println("<script language=\"JavaScript\" src=\"http://scripts.hashemian.com/js/countdown.js\"></script>");
    and again deploy it on tomcat server, the script gets fetched from the http:// website and runs.

    (case3)
    when i create a html file as shown below

    Code:
    <html>
    <body>
    
    
    <script language="JavaScript">
    TargetDate = "1/1/2009 12:00 AM";
    BackColor = "palegreen";
    ForeColor = "navy";
    CountActive = true;
    CountStepper = -1;
    LeadingZero = true;
    DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds. for new years";
    FinishMessage = "It is finally here!";
    </script>
    <script language="JavaScript" src="countdown.js"></script>
    
    <p> it works!! </p>
    
    </body>
    </html>
    the script that i have saved(and i call) on the harddisk runs and everything works out.

    so thats my story.

    In case(1) the js file saved on the hard-disk is called, but it does not get executed
    In case(2) the same js file is retrived from the internet and the script gets executed
    In case(3) instead of using a servlet i create a static html page and call the javascript saved on the hdd and it works

    the servlets "web.xml" files code is as shown below



    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    
        version="2.4">
    
    
    
        <servlet>
    
            <servlet-name>Execute</servlet-name>
    
            <servlet-class>Execute</servlet-class>
    
        </servlet>
    
    
    
        <servlet-mapping>
    
            <servlet-name>Execute</servlet-name>
    
            <url-pattern>/Execute</url-pattern>
    
        </servlet-mapping>
    
    </web-app>
    so why is it that case(1) does not work out but case(2) and case(3) work???

    please help out.



    if you dont understand what i m trying to say...i have uploaded the war files...they are only 5KB...it would be really nice...if you people could take a look at them and understand my problem first-hand.

    1.war file that has javascript pointing to http://scripts.hashemian.com/js/countdown.js/ - http://www.savefile.com/files/1950654
    2.war file that has javascript pointing to the local hard-disk - http://www.savefile.com/files/1950656

  2. #2
    Join Date
    Oct 2007
    Posts
    63

    Re: strange servlet problem

    i viewed the sources that the plain html file and the servlet file generated...
    they are exactly the same...yet the html file works , but the servlet file does not....what am i doing wrong?????

    Code:
     <html>
     <head>
     </head>
     <body>
     <script language="JavaScript">
     TargetDate = "1/1/2009 12:00 AM"
     BackColor = "palegreen"
     ForeColor = "navy"
     CountActive = true
     CountStepper = -1
     LeadingZero = true
     DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds. for new years"
     FinishMessage = "It is finally here!"
     </script>
     <script language="JavaScript" src="countdown.js"></script>
     <p>it works!!</p>
     </body></html>
    this generated by the servlet (source of: http://localhost:8080/execute/Execute - Mozilla Firefox)

    and

    Code:
     <html>
     <body>
     
     
     <script language="JavaScript">
     TargetDate = "1/1/2009 12:00 AM";
     BackColor = "palegreen";
     ForeColor = "navy";
     CountActive = true;
     CountStepper = -1;
     LeadingZero = true;
     DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds. for new years";
     FinishMessage = "It is finally here!";
     </script>
     <script language="JavaScript" src="countdown.js"></script>
     
     <p> it works!! </p>
     
     </body>
     
     </html>
    this generated by the html page (source of: file:///usr/local/apache-tomcat-6.0.18/webapps/examples/servlets/cd.html - Mozilla Firefox)
    thats because i saved the html file and another copy of the same js file at /usr/local/apache-tomcat-6.0.18/webapps/examples/servlets/

  3. #3
    Join Date
    Oct 2007
    Posts
    63

    Re: strange servlet problem

    solved...the problem was that

    suppose the servlet url is http://xyz/bar/

    so when i say
    Code:
    src="foo.js"
    the server thinks that foo.js is in http://xyz/bar/...but i did not place foo.js there...that was causing all the problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured