CSS won't load with UTF-8 encoding - servlets

trying for several days now to get my webapp to accept UTF-8 encoded requests. I'm out of ideas how to get that going.
Setup:
- DigitalOcean Droplet (Debian)
- Tomcat Container (8.5.43)
- JavaServlet Webapp
What i already did:
- set the locale at my Droplet to de_AT.UTF-8
- set the tomcat config to use UTF-8 for every request on 8080 via config.xml
...
<Connector port=8080 ... URIEncoding="UTF-8" ...>
...
- implemented a CharSetFilter, that is used for every request (web.xml holds the /* mapping for that filter). Code of the Filter:
#Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain next) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
next.doFilter(request, response);
}
- set the encoding on every jsp file with
<%#page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
- set the encoding on every html with
<meta charset="UTF-8" http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- set the encoding in the css with
#charset "UTF-8";
use UTF-8 in eclipse as encoding before exporting to WAR
The CSS does not get loaded although it is referencable via the link in the browser-inspector.
I have deployed it so you can take a look at it:
http://2foliant.tk
Can anyone PLEASE help me out and tell me, why the CSS won't get loaded?

Remove the filter and see the result
Please remove the css tag since it is not related to css problem
Great to hear it is sorted
Accept this as a correct answer if you can do that

Related

How to redirect 404 to start page?

I just started to host a web application on firebase.
the automatic setup with the firebase cli (firebase-tools) created a 404.html file in my public folder.
but i don't want to see a custom 404 error page nor the default one from firebase.
I would like to redirect all 404 to the startpage, so that the app will get initialised anyway. (my local setup with webpack-dev-server is just working fine)
a workaround is just to put the same content from index.html into the 404.html. But this leads into doubled maintenance..
i found some information about redirect configuration here https://firebase.google.com/docs/hosting/url-redirects-rewrites.
but a redirect for 404 error types isn't possible.
using a .htaccess file isn't possible.
am i missing the right location to change this behaviour whether in my firebase.json file or in the https://console.firebase.google.com/??
Not sure if this will work for you, I've never done stuff with .htaccess, but in my case I've always fixed it this way:
You can include a wildcard route as the last route in the rewrites section of your firebase.json file, so any previously unclaimed routes will match:
"rewrites": [
{
// route info here
},
{
// another route
},
{
// If it makes it here, it didn't match any previous routing
// Match all routes that get to this point and send them to the home page.
"source": "**",
"destination": "/index.html"
}
]
If you don't want to have original/non-existent url left (eg. https://example.com/asdf) you can modify 404.html source and replace it with js redirect.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Page Not Found</title>
<script>
window.location.href = "/";
</script>
</head>
</html>

Renaming css stylesheet file causes bad MIME type - Apache

I've come over a curious problem. When I include my stylesheet as following
<link rel="stylesheet" href="css/style.css">
When I rename the file to explicitly 'setup.css' it causes a bad content type
Content-Type:text/html; charset=UTF-8
However, changing on any other name is just fine.
I'm running Apache 2.4.26 and PHP 7.0.21 whether it matters or not.

Image Servlet doesn't want to show image in browser (FireFox, IE..) but in Eclipse browser works?

I have strange situation with my image servlet. It works well with Eclipse internal web browser, but won't to work with FireFox, InternetExplorer, Chrome, Opera....
Servlet code (below)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
File fl=new File("C:\\Documents and Settings\\Administrator\\My Documents\\Eclipse\\zadatak\\WebContent\\WEB-INF\\upload\\");
PrintWriter pw=response.getWriter();
response.setContentType("text/html");
pw.println("<html>");
pw.println("<head>");
pw.println("<style type=\"text/css\">");
pw.println("img{display:block-inline;width:100px;height:100px;");
pw.println("</style>");
pw.println("</head>");
pw.println("<body>");
pw.println("<table border=\"1\">");
pw.println("<tr>");
for(File f:fl.listFiles()){
pw.println("<td>");
pw.println("<img src=\"" + f.getPath() + "\">");
pw.println("</td>");
}
pw.println("</tr>");
pw.println("</table>");
pw.println("</body>");
pw.println("</html>");
}
In Eclipse internal web browser looks like this (below)
PrintScreen of Eclipse internal web browser
Finally I found a solution for my problem. Thanks you all for helping me!
Now, this code works!
ServletContext context=request.getServletContext();
String path=context.getRealPath("upload");
File fl=new File(path);
PrintWriter pw=response.getWriter();
response.setContentType("text/html");
pw.println("<html>");
pw.println("<head>");
pw.println("<style type=\"text/css\">");
pw.println("img{display:block-inline;width:100px;height:100px;");
pw.println("</style>");
pw.println("</head>");
pw.println("<body>");
pw.println("<table border=\"1\">");
pw.println("<tr>");
LinkedHashMap<Integer, String> hm=new LinkedHashMap<Integer,String>();
int imageIndex=-1;
for(File f:fl.listFiles()) {
if(f.getName()!=null){
if(f.getName().endsWith(".bmp"))
hm.put(++imageIndex, f.getName());
}
}
Iterator<String> pic=hm.values().iterator();
while(pic.hasNext()){
pw.println("<img src=\"upload/" + pic.next() + "\"/>");
}
pw.println("</tr>");
pw.println("</table>");
pw.println("</body>");
pw.println("</html>");
class hierarchy in my project looks like
MyWebProject
|-- src
| :
|
|-- web
| |-- META-INF
| | `-- MANIFEST.MF
| |-- WEB-INF
| | `-- web.xml
|-- upload (the place where I keep pictures)
:
Firefox now can correctly to show pictures
Firefox correctly shows page
Think about what you're doing. There's a server, which has the images on its disk. And there's a browser, which will run on a different machine, potentially dozens of miles away from the server.
Your servlet sends the following to the browser:
<img src="C:\Documents and Settings\Administrator\My Documents\Eclipse\zadatak\WebContent\WEB-INF\upload\someIage.jpg"/>
This will only work if
the browser accepts to load images from the file system, although the HTML page is loaded with HTTP
the browser accepts to load files from the file system although the URL is incorrect (it should be file://...)
the client runs on Windows
the user happens to have a folder named C:\Documents and Settings\Administrator\My Documents\Eclipse\zadatak\WebContent\WEB-INF\upload\ on his machine
this folder happens to already contain all the images that the servlet sends
You should understand that this can't possibly work.
What you need is a servlet which loads an image from a file on the server, and sends the contents of this file to the HTTP servlet response (and doesn't forget to set the correct content type).
Once you have such a servlet (let's assume it is mapped to the URL /image and takes the ID of an image as parameter), the servlet you already have should generate the follwoing code:
for (String id: idsOfTheImageToDisplay){
pw.println("<td>");
pw.println("<img src=\"/image?" + id + "\"/>");
pw.println("</td>");
}
Also, BMP is not a good format for the web, because it is not copressed and thus consumes a lot of bandwidth. You should transform your images to JPEG files, for example.

Unable to show CSS when running JSP page

I read almost all the posts on this issue and so far no solutions have worked.
My CSS is outside the WEB-INF folder;
I've tried the "${pageContext.request.contextPath}" with no avail;
I've tried the following servlet mapping in my web.xml but it causes a NoTargetForURIException:
The error :
PageNotFound W org.springframework.web.servlet.DispatcherServlet noHandlerFound No mapping found for HTTP request with URI [/webapp/css/util.css] in DispatcherServlet with name 'cm-dispatcher'
My JSP :
<link href="${pageContext.request.contextPath}/css/util.css" rel="stylesheet" type="text/css" />
HELP!
Try to give the exact path like localhost:8080/css/example.css. that is refer it with absolute path. Your problem will be solved.Since it is outside the WEB-INF it can be only resolved by doing this.
Use the absolute path to the Css while importing it.

Resource interpreted as stylesheet but transferred with MIME type text/html

This is my html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv = "Content-Language" content = "en"/>
<meta http-equiv = "Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="/xxx/app/www/style.css">
Now when I load the page, Safari's inspector gives me:
Resource interpreted as stylesheet but transferred with MIME type text/html
I'm working on localhost with MAMP. I'm not sure what to modify to get this to work. I read that this might be a server problem of some kind.
How do I solve it?
Create an .htaccess file into your root folder (or update the existing one) with this line inside
AddType text/css .css
this will tell apache to send the right content-type header for .css file
There is possibility that not just css resources were interpreted wrongly. It maybe a problem of your webserver configuration.
The best way to check is to go to Chrome -> Network tab and check responses section for each request.
Also you can run $ curl -I http://path_to_file_or_page with terminal and have a look at response, you should see it in following format, e.g. Content-Type: image/jpeg
So, if it will occur that webserver (apache) does it for all filesthen you can try to solve the problem in following way:
Check your /etc/apache2/apache2.conf
If there are any SetHandler application/x-httpd-php line, try to comment it and then reload your apache by $ sudo system apache2 reload
Let us know if the problem is still there.
It could be that your web server is properly configured but it really is returning HTML instead of CSS.
Check that you have the correct path, spelling, case, etc. Hit the URL directly and see what comes back. Your web server might be responding with an error page (HTML) and a 200-Ok instead of a 404-Not Found.
I'm not saying this ever happened to me...

Resources