in Education by
In Java, web apps are bundled in to WARs. By default, many servlet containers will use the WAR name as the context name for the application. Thus myapp.war gets deployed to http://example.com/myapp. The problem is that the webapp considers its "root" to be, well, "root", or simply "/", whereas HTML would consider the root of your application to be "/myapp". The Servlet API and JSP have facilities to help manage this. For example, if, in a servlet, you do: response.sendRedirect("/mypage.jsp"), the container will prepend the context and create the url: http://example.com/myapp/mypage.jsp". However, you can't do that with, say, the IMG tag in HTML. If you do image you will likely get a 404, because what you really wanted was "/myapp/myimage.gif". Many frameworks have JSP tags that are context aware as well, and there are different ways of making correct URLs within JSP (none particularly elegantly). It's a nitty problem for coders to jump in an out of when to use an "App Relative" url, vs an absolute url. Finally, there's the issue of Javascript code that needs to create URLs on the fly, and embedded URLs within CSS (for background images and the like). I'm curious what techniques others use to mitigate and work around this issue. Many simply punt and hard code it, either to server root or to whatever context they happen to be using. I already know that answer, that's not what I'm looking for. What do you do? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can use JSTL for creating urls. For example, will prefix the context root. With CSS, this usually isn't an issue for me. I have a web root structure like this: /css /images In the CSS file, you then just need to use relative URLs (../images/header.jpg) and it doesn't need to be aware of the context root. As for JavaScript, what works for me is including some common JavaScript in the page header like this: var CONTEXT_ROOT = '<%= request.getContextPath() %>'; Then you can use the context root in all your scripts (or, you can define a function to build paths - may be a bit more flexible). Obviously this all depends on your using JSPs and JSTL, but I use JSF with Facelets and the techniques involved are similar - the only real difference is getting the context root in a different way.

Related questions

0 votes
    What is a listener in context to event handling? (a) A listener is a variable that is notified when an ... Event Handling of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    What is a listener in context to event handling? (a) A listener is a variable that is notified ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    What is Exception Handling in WCF? What are the ways for WCF Exception Handling?...
asked Apr 2, 2021 in Education by JackTerrance
0 votes
    How should I handle /_ah/start and /_ah/stop requests? Right now they're just getting 404s. Also, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    What are the ways to define Context variables in Talend?...
asked May 30, 2021 in Technology by JackTerrance
0 votes
    I am new to java so excuse my lame questions:) I am trying to build a web service in Java ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    The Sitecore package wizard can be used to build a package containing data and files for the local Sitecore ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 16, 2022 in Education by JackTerrance
0 votes
    We currently deploy web applications by creating a database and running SQL scripts through query analyzer. Then ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 29, 2022 in Education by JackTerrance
0 votes
    HTTP defines two ways in which values entered by a user at the browser can be sent to the Web ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    List some common ways to reduce the load time of a web application?...
asked Jun 13, 2021 in Education by Editorial Staff
0 votes
    I am using VBA in most of my office 365 applications for many years now. Last week an error ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    While I try to answer any question in brainly app, the cursor is moving from right to left instead of moving ... Why does it happen? Select the correct answer from above options...
asked Nov 29, 2021 in Education by JackTerrance
0 votes
    Apologies if this question is answered elsewhere on StackOverflow, I did my best to search for an answer ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    This is a question I asked on another forum which received some decent answers, but I wanted to see ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I have a problem where a Web Application needs to (after interaction from the user via Javascript) 1) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 18, 2022 in Education by JackTerrance
...