*
The moose likes Servlets and the fly likes How to call HTML file from servlet program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to call HTML file from servlet program" Watch "How to call HTML file from servlet program" New topic
Author

How to call HTML file from servlet program

sudha javvadi
Ranch Hand

Joined: Mar 23, 2007
Posts: 97
Hi All,

I wrote a servlet program which calls a HTML file. I am getting the error as: The requested resource (/FindEmp.html) is not available.
The part of servlet program is as follows.

out.println("<a href='/FindEmp.html'> Find another </a>");

If i click on 'Find Another' link it should open 'FindEmp.html' file.Do I need to specify the whole path for /FindEmp.html?? I tried that way too but same error.What else i can do to run the program.

Thanks in advance,
Sudha.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

No, but you need to learn how to make context relative URLs.

From a servlet, use the request.getContextPath method.

Example:


Once you move on to using JSPs for your view, you'll want to see this FAQ entry
for the syntax in various versions of JSP and for a little background on why this is necessary.
[ February 20, 2008: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
sudha javvadi
Ranch Hand

Joined: Mar 23, 2007
Posts: 97
Thanks! Ben.
nikhil govind
Ranch Hand

Joined: Mar 08, 2011
Posts: 31
hey so what we have to do i have the same error
Deepthi Tanguturi
Greenhorn

Joined: Sep 06, 2011
Posts: 17
Ben Souther wrote:No, but you need to learn how to make context relative URLs.

From a servlet, use the request.getContextPath method.

Example:


Once you move on to using JSPs for your view, you'll want to see this FAQ entry
for the syntax in various versions of JSP and for a little background on why this is necessary.
[ February 20, 2008: Message edited by: Ben Souther ]

By using Request Dispatcher you can call the html page
Vigneswaran Marimuthu
Greenhorn

Joined: Aug 30, 2011
Posts: 24

Deepthi Tanguturi wrote:
By using Request Dispatcher you can call the html page


ya we can achieve it but request.getContextPath() is a more professional way. Suppose consider your project has many files in different folders and your project is in server. If some user tries to access your project, as a programmer we should not make him to type the entire URL or make him to remember the entire URL of all pages that he is going to access. So you should use this method so that the user will just give the URL to your project and with that URL we need to add the location of the startup page of our project !!!


Regards,

Vigneswaran.M
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

Huh? I'm not sure of the point that you are trying to make.

The context path should always be used when constructing URLs that will be used within pages that are sent to the client; images, CSS and JS files for example, as well as form actions.

It should not be used on the server when forwarding using the request dispatcher.

The two scenarios are completely different and the correct approach should be used for each.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vigneswaran Marimuthu
Greenhorn

Joined: Aug 30, 2011
Posts: 24

Thanks Bear !! i was taught like that. Got some information about the usage of Context path. i still didnt try it. tried only request dispatcher !!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to call HTML file from servlet program
 
Similar Threads
how to use ssi in servlets using apache web server and jserv
Http status 404 error
servlet program doubt
Struts with Resin
How do I start a web application on an xhtml page?