I am trying to include jquery.js and javascript.js files on my page, as proposed in the previous question by passing it through the Servlet class like:
response.getDocument().body().appendChild(new DataNode("<script src=\"" + themeUri
+ "/js/nativeinit.js\" type=\"text/javascript\"></script>",""));
response.getDocument().body().appendChild(new DataNode("<script src=\"" + themeUri
+ "/js/jquery.min.js\" type=\"text/javascript\"></script>",""));
When I see the Console in Browser though I see the messages for both files:
Resource interpreted as Script but transferred with MIME type text/html: "http:8080/uri/js/nativeinit.js". myhost/:47
Resource interpreted as Script but transferred with MIME type text/html: "http:8080/uri/js/jquery.min.js". myhost/:47
And then:
Uncaught SyntaxError: Unexpected token < nativeinit.js:1
Uncaught SyntaxError: Unexpected token < jquery.min.js:1
What is wrong and it cannot read them as script? Why this strange message of unexpected token? What should I change/add?
Update:
I have also added in my web.xml file:
<mime-mapping>
<extension>js</extension>
<mime-type>application/javascript</mime-type>
</mime-mapping>
but problem with the same message remains. I am aware of the fact that #Javascript annotation is the recommended way in vaadin 7, but I think it should work as well on the common way. I do not set Content Type in Servlet Class. I can use there only the class BootstrapPageRespone from https://vaadin.com/api/ , which does not provide the methods from ServletResponse. Otherwise I would have set it also programatically.
check this question: Chrome says "Resource interpreted as script but transferred with MIME type text/plain.", what gives? - maybe when browser requests your script, it gets answer from server as text/html. Check MIME type of your server response.
The recommended approach is to use the #Javascript annotation, as discussed in this article.
Related
I need help on the issue where we are appending special character like % with URL.
Suppose my application URL is "http://www.google.com/".
we have our custom error page which will come when server will not find URL address.
scenario 1: hitting "http://www.google.com/sdfkdjkfj :---redirecting on custom error page which is correct.
Scenario 2: hitting "http://www.google.com/% :--Instead of redirecting on my own custom page it is showing message "this page is not working HTTP ERROR 400".
we are using below code in web.xml to handle the page not found exception.
<error-page>
<error-code>404</error-code>
<location>/ErrorPage.jsp</location>
.
Please help on scenario 2 .
If you want to include a % character in a URL, it needs to be percent-encoded; e.g. http://www.google.com/%25. (That URL still may not be recognized, but at it is syntactically well-formed.)
Error code 400 means "Bad request", the server cannot understand this request. Generally you should avoid passing special character "%" in your URL like in your example.
However if you want to pass this character and preserve its meaning, try to encode it: https://www.w3schools.com/tags/ref_urlencode.asp. When you try http://www.google.com/%25 - you will have 404 error.
I have a URL shortner that should sendRedirect(URL) to URLs specified by users.
Sometimes URL contain curly braces like this: http://example.com?someparam={something}.
Instead of sending response 302 to client browser, my Spring MVC app at Tomcat server gives error 404 with no text.
Apparently it's some sort of URL variable evaluation taking place, can I disable it? I could not find docs regarding this feature.
I know this is an old question but I think the OP was looking for a way to prevent Spring from doing variable replacement in redirect URL
I faced the exact same issue and the fix was using RedirectView
and in RedirectView you can set setExpandUriTemplateVariables(false)
that made it redirect to the url given exactly without Spring trying to replace anything in it
here is how the code looks like
RedirectView redirect = new RedirectView(redirectUrl);
redirect.setExpandUriTemplateVariables(false);
return new ModelAndView(redirect);
Hope that helps
This is not valid Google search URL http://google.com/{something}. It should have been https://www.google.ca/search?q=http{302}
Emphasis is on search?q. After domain name you have specify your service name and then query string if you want to pass some inputs.
When you do http://google.com/{something} then you really do not have any resource or service as {something} so 404 is the expected output.
HTTP 302 is for redirection, I am not sure why you were expecting redirection.
URL encoding will also not help because issue is related to resource/service, if it is not present then you will get 404. URL encoding is not meant to solve problem related to 404.
I'm not 100% clued up on Java, or Filters. Taking over some code and we have discovered that .ZIP (uppercase) files in Firefox are rendered as text/plain. The .ZIP file downloads correctly in IE but not Firefox. A .zip (lowercase) downloads correctly in both IE and Firefox.
As far as I can make out the web.xml points to a Filter class.
At the crux of the code where chain.doFilter is called I've tried to set the content type before the chain.doFilter, and then check what the content type is before and after doFilter.
This is the code:
LOG.debug("Current Content Type: " + response.getContentType());
response.setContentType("application/zip");
LOG.debug("New Content Type: " + response.getContentType());
chain.doFilter(request, response);
LOG.debug("Current Content Type2: " + response.getContentType());
The output of this is as follows (roughly):
Current Content Type: null New Content Type: application/zip
<Some stuff where doFilter is called />
Current Content Type2: text/plain
In Firefox I get the content type as text/plain so I think its the doFilter setting the content type.
We don't have the option of changing the extension as these are files coming from an external source so cannot be changed.
Any pointers as to why this happens, or how to get a .ZIP file to prompt to download correctly.
Thanks.
The doFilter() method is calling the next filter in the filter chain.
The filter chain is the list of filters, as they are defined in your web.xml.
So, maybe there is a filter in your web.xml, that does change the content type.
But maybe it is simpler, look for mime mapping either in the default configuration of your application server, or define one in your web.xml and see if that helps:
<mime-mapping>
<extension>ZIP</extension>
<mime-type>application/zip</mime-type>
</mime-mapping>
Lets say I hit
http://localhost/webapp/wcs/stores/servlet/en/marksandspencer/l/women/dresses/party-and-cocktail-dresses
and this internally redirects me to custom 404.jsp page, But URL remain same in address bar.
I tried this code - <%= request.getAttribute("javax.servlet.forward.request_uri") %>; and it's returning me the path of 404.jsp
How can I get the entered URL which is there in address bar?
Use request.getAttribute("javax.servlet.error.request_uri") to get URI of requested page that not found (404 error). Check this: https://tomcat.apache.org/tomcat-7.0-doc/servletapi/constant-values.html
When error raised (because of some reason such as page not found (404), Internal Server Error (500), ...), the servlet engine will FORWARD the request to corresponding error page (configured in web.xml) using ERROR dispatcher type, NOT FORWARD dispatcher type so that is the reason we must use javax.servlet.error.request_uri, NOT use javax.servlet.forward.request_uri
I think you were close. javax.servlet.forward.request_uri is for normal forwarding, but for 404, you need javax.servlet.error.request_uri.
You can use :
String url = request.getRequestURL().toString();
but this doesn't hold Query String. So, to get query string, you may call
request.getQueryString()
You can do this to get the whole URL including parameters.
request.getRequestURL()+""+request.getQueryString();
use request.getHeader("Referer").
referer gives a url from where you redirected.
I'm currently developing RESservices using the customService bean. One thing I've noticed is that for some reason when I dont use the responsewriter object but only set the response status using engine.getHttpResponse().setStatus(404) (for instance). The header is correctly set ( 404 ) but there is still some html generated.
I've already tried to set the rendered property on the view tag (of the xpage) to false but that doesn't seem to do the trick. Is there some documentation on how to use the CustomServiceBean and not returning any data?
Setting the Status is a good approach, so the API is easy to handle. However you want to consider:
set the content type to what would actually be rendered if you had a result. If you don't set response.contenttype it defaults to HTML
404 is meant for navigation/urls. Since your XPage renders a result, the request found a valid URL. That based on user and/or parameters there's no result isn't therefore a 4xx class of error, but rather a 5xx. Pick one of them.
when testing use wget or curl, so you can be sure the browser doesn't do the HTML
Let us know how it goes
I think returning a 404 response causes always HTML data as it normally gives you the error message etc.
If you don't want to response with any data just return an empty JSON object - as I assume you return JSON when you HAVE data, right?
dont set 404 status ,set null for this case,ant error status like 404 means creating html for that