Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to call HTML file from servlet program

 
sudha javvadi
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
sudha javvadi
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! Ben.
 
nikhil govind
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey so what we have to do i have the same error
 
Deepthi Tanguturi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65116
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vigneswaran Marimuthu
Greenhorn
Posts: 24
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic