How to add a favicon to a Google appengine app - java

I am trying to add a favicont to a appengine web app but not completely sure how to go about it.
What i've done:
I added the following to the of my appengine-web.xml file
tag: The favicon is located in src/main/webapp...
It still doesn't get served. Any idea what i;m doing wrong? I also added /

Put your favicon image inside the war directory of project . It works for me . Nothing else needed . Path should be like this /YourProjectName/war/favicon.ico. You can verify this path by right clicking on properties for your favicon image .

The syntax that you used is the one for the app.yaml file (which is used by the environments other than Java)
For the Java environment the static files are specified in the appengine-web.xml file. Look for the <public-root> and the <static-files> tags in the appengine-web.xml Reference:
< public-root >
Optional. The is a directory in your application that
contains the static files for your application. When a request for a
static file is made, the for your application is
prepended to the request path. This gives the path of an application
file containing the content that is being requested.
The default is /.
For example, the following would map the URL path /index.html to the
application file path /static/index.html:
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<!-- ... -->
<public-root>/static</public-root>
<!-- ... -->
</appengine-web-app>
and
< static-files >
Optional. The element specifies patterns that match
file paths to include and exclude from the list of static files,
overriding or amending the default behavior. Static file are served
from dedicated servers and caches that are separate from the
application servers and are useful for serving static content such as
images, CSS stylesheets or JavaScript files.

Related

How to make resource files privatly and locally accessible to the application with app.yaml in GAE?

In appengine-web.xml we have <resource-files> tag to make resources files accessible to the application at runtime but not public, but in the new app.yaml we don't.
How can I make files privatly and locally accessible to the application with app.yaml like it used to be with the <resource-files> tag in Google App Engine?
According to the documentation
If you are using an appengine-web.xml in your project, the app.yaml is automatically generated for you at deployment.
Why not just create the appengine-web.xml as you normally would. Deploy your app. Then take a look at the generated app.yaml file
After deploying a test application, I could verify that it's not necessary any special configuration in app.yaml anymore to have access to the contents of WEB-INF. Everything there can be used using the ServletContext method getResource and getResourceAsStream as expected.

Tomcat custom context.xml file is never taken into account

I have currently a problem with the Tomcat configuration of a web application (I use Tomcat 8.5.57).
This web application is packaged in a war file containing, among others, html files and css files.
This application works well.
Now, I have a request from a customer asking to be able to modify the look and feel of the application from outside of the war via a custom css file managed by the client (used to set the logo of the client or stuff like that).
So I tried to create a custom context file, called custom.xml, that I placed in tomcat\conf\Catalina\localhost directory.
This file looks like :
<Context docBase="E:/somedirectory/support"
path="/app/css"
reloadable = "false"
unpackWAR = "false"
swallowOutput = "true" >
<WatchedResource>custom.css</WatchedResource>
</Context>
I put the custom.css file containing some css instructions as test in the E:/somedirectory/support directory.
In the html file of my web application, I have the following line in the head section :
<link rel="stylesheet" href="css/custom.css" media="screen" type="text/css"/>
The problem is that my custom.css file is never taken into account.
When I open the Sources tab of Chrome's developer tools, I see a custom.css file in the hierarchy in app/css as expected (probably due to the line in the html file), but it is hopelessly empty.
I tried a lot of things found on the Web and on stackoverflow, but nothing worked for me...
Can someone help me ?
Thank you !
The path attribute of <Context> element is ignored outside of server.xml:
This attribute must only be used when statically defining a Context in server.xml. In all other circumstances, the path will be inferred from the filenames used for either the .xml context file or the docBase. [from Tomcat documentation]
Therefore you have two choices:
You can define a new context (new web application) with context path /app/css by creating a file named conf\Catalina\localhost\app#css.xml and content:
<Context docBase="E:\somedirectory\support" />
This way everything under the /app/css subtree will only be served from the E:\somedirectory\support directory.
You can redefine your application context to include an additional virtual directory (beside the contents of the WAR file) by adding a file named conf\Catalina\localhost\app.xml with content:
<Context>
<Resources>
<PreResources className="org.apache.catalina.webresources.DirResourceSet"
base="E:\somedirectory\support"
webAppMount="/css" />
</Resources>
</Context>
This way, while serving a request for /app/css/foo/bar, Tomcat will first look for foo/bar in E:\somedirectory\support and then in the WAR file.

Provide <Realm/> info to Tomcat on eclipse

Our web application has an appName.xml file in our Tomcat directory structure at <tomcatInstall>/conf/Catalina/localhost/. Its entire text is:
<?xml version="1.0" encoding="UTF-8"?>
<Context crossContext="true">
<Realm className="com.blah.ApDataSourceRealm"
dataSourceName="jdbc/blahDev"
roleNameCol="blahRole"
userCredCol="blahToken"
userNameCol="blahCol"
userRoleTable="blahTable"
userTable="blahTable"
/>
</Context>
I made sure to delete the tomcat 8 install in eclipse and then add it back again; I understand that is what copies all the tomcat installation stuff to the workspace locations. When I attempt to run the server on eclipse, the login page is displayed, but the username/password is always rejected.
I fixed it once by finding the eclipse copy of server.xml in the eclipse-workspace folder tree (...eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/temp1/conf). But the next time I rebooted the computer and ran it again, the login failed, and on checking server.xml, I find that it was overwritten this morning. I assume eclipse overwrites it under some conditions, perhaps on eclipse startup? I put the <Realm ... /> tag into server.xml within <Context ... /> again, and it is working again.
Where should this information go? I'd like to put it somewhere that it won't disappear from eclipse. I don't think we want this realm tag in the actual server.xml (as opposed to the one local to eclipse), because this one is specific to the development environment. So where can it be put on my machine so that it is used by eclipse's Tomcat and yet doesn't interfere with the application's overall source?
One of the way is to put your <Context> configuration in the webapp/META-INF/context.xml.
Reference :
Individual Context elements may be explicitly defined:
In an individual file at /META-INF/context.xml inside the application
files. Optionally (based on the Host's copyXML attribute) this may be
copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to
application's base file name plus a ".xml" extension.
In individual
files (with a ".xml" extension) in the
$CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The context
path and version will be derived from the base name of the file (the
file name less the .xml extension). This file will always take
precedence over any context.xml file packaged in the web application's
META-INF directory.
Inside a Host element in the main conf/server.xml.
Default Context elements may be defined that apply to multiple web
applications. Configuration for an individual web application will
override anything configured in one of these defaults
Where should this information go? I'd like to put it somewhere that it
won't disappear from eclipse
So where can it be put on my machine so
that it is used by eclipse's Tomcat and yet doesn't interfere with the
application's overall source?
When you create a Tomcat in Eclipse , a server project will also be created. This project stores the configuration for the created tomcat instance which will be copied to Tomcat instance 's conf folder (i.e. eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/temp1/conf when tomcat starts.
So , if you do not want to add the Realm configuration to your project source codes likes /webapp/META-INF/context.xml , you can add it to the context.xml in the server project.

Java Web Application. Problems in locating images

I'm making a java web app, and I want it to display an image. However, it doesn´t find my image.
I've made a folder in /src/main/resources/images
Also, in the .jsp file, I´ve tried with the following sentences.
<img src="/src/main/resources/images/Head.png"> </img>
<img src="< c:url value='/src/main/resources/images/Head.png'/>"> </img>
Is there anything bad I'm doing?
Thanks
Edit:
The path of mi .jsp file is /src/main/webapp/WEB-INF/jsp/welcome.jsp
You can find the web app code in https://github.com/Santi-7/hello
Your app is a Spring Boot app. I think that you can also try to use the facilities provided by Spring Boot for serving static content. Anyway, you are doing it right now because you are using webjars for css and js libs!!! Be consistent with the tech that you are using.
The structure of a .war file is as follows:
/
/WEB-INF
/lib
/classes
/META-INF
Now, your application has the following structure (I assume, given the folder structure, you are using Maven)
/
/src
/main
/java
/resources
/webapp
Now, the Maven war plugin will copy everything in the classpath to /WEB-INF/classes during compilation - this is /src/main/java and /src/main/resources by default.
The crux of the matter is that nothing under /WEB-INF or /META-INF can be accessed by requests - this is for security as otherwise someone could simply download /WEB-INF/web.xml for example.
So, in order to add a resource that is accessible by a browser, you need to place it into /src/main/webapp - this will become the root of the application.
So if you place Head.png into /src/main/webapp/images then in the JSP you would use:
<c:url value='/images/Head.png'/>
In short, you need to read up on how the directory structure of a .war works and how that relates to your code.
The path to the image must be relative to the path to the .jsp file.
Because the path to your image is: /src/main/resources/images/Head.png, and the path to your jsp file is: /src/main/webapp/WEB-INF/jsp/welcome.jsp, in your image tag you need to write:
<img src="../../../resources/images/Head.png" />
The ../../../ is for getting out from the jsp folder to the main folder, and the resources/images/Head.png is the path from the main folder to the image.
Thanks everybody, i could resolve my problem.
Changes I made:
So, in order to add a resource that is accessible by a browser, you need to place it into /src/main/webapp - this will become the root of the application.
Now, my images are in /src/main/webapp/images.
The path to the image must be relative to the path to the .jsp file.
Now, the sentence of my .jsp file is
<img src="images/Head.png" />
Edit [1]:
¡ I made a mistake. The path to the image is relative to the /webapp classpath !

Eclipse - How do I publish non-Java resources without a web.xml file?

My WebApp uses Java annotations to map the URL to the servlet (#WebServlet) and I don't have a web.xml file. I want to foward the request to another HTML file and I did it like so:
request.getRequestDispatcher("/WEB-INF/test/testpage.html").forward(request, response);
It works, but I can't get additional resources (javascript) from the server.
<script type="text/javascript" src="testsuite.js"></script>
testsuite.js is located in /WEB-INF/test/testsuite.js, the same folder as testpage.html. When requesting testsuite.js I get 404. How can I configure Tomcat to serve all the resources in my work tree without a web.xml? Worst case I will simply embed JS.
Additionally I let eclipse install the web server on my local machine so I have no idea how they are deployed.
You have to move resources outside of your WEB-INF folder. It is a special folder for compiled Java code, jars, configuration files etc.
The WEB-INF folder is restricted in application servers and servlet containers for security reasons, so for example a request for /appname/WEB-INF/web.xml will be deined of course.
Static resources such as *.html files, *.css files, *.js files, images etc. should be placed outside of WEB-INF folder, in your case the test folder should be moved up next to WEB-INF.

Categories