URL gets cut after the hashtag - java

Im using Java (Maven), Angular(8.3.20) and a Tomcat server
In the Java I have a sendRedirect for a HttpServletResponse with a URL that contains a hashtag.
So for example: https://localhost:4200/api/hello#world.
But the string after the # (the world part) will not appear in the frontend.
In the console/network as a header, I received https://localhost:4200/api/hello. So the #world is gone.
I have tried to change the hashtag to an encoded value (it will return a %23) but that does not work as well.
How can I get the part after the hashtag being send to the frontend? So from the backend (a url with a pound/hashtag) to the frontend.

I looked for this problem, the solution I found is to code # to %23,
but it seems that it does not work for all browsers (ex: IE, Safari) so try to use google-chrome for example.
more information on:
https://support.google.com/richmedia/answer/190941?hl=en

Routing strategy
What angular exploits with the HashLocationStrategy is the fact that any content after the # symbol isn't send to a server - which makes it ideal to use it for storing application state.
Why is it useful
With hash routes a page reload (or revisit through bookmark) on a subpage like
http://localhost:4200/#/articles/35
doesn't query the server for the subpage, but instead returns the main application page.
http://localhost:4200/
This way the server implementation only needs to know about the root page (which is the only thing that'll ever be queried)
Using the PathLocationStrategy (default) the server needs to be set up to handle requests for every single URL your application implements.
So what can you do is : disable HashLocationStrategy in angular from routes file. Or you can append the url without # and then send it to server and handle the url on sever side.
I had the same problem, what I did was add the part which is infront of # as a query parameter, so that server can access it.
E.g.
(Https://localhost:4200/api/test?remainingDataFromHash=requiredData) like this

Related

Is there a way to access complete angular URL (with #!) in JAVA filter?

We have many pages implemented using angular JS. We have created offline snapshot html and store it for web crawlers.
Now when a web crawler asks for a particular page, based on user agent value we redirect the request to a JAVA servlet which returns appropriate snapshot page to crawler.
When the request comes in to the servlet from a crawler (like Endeca), I just get the url till #.. and everything after # in the url is lost hence servlet is not able to return corresponding snapshot.
I know that its not possible to send complete url (along with #) via http request but I want to know if there is a way to overcome this issue.
curl -A "endeca webcrawler" "http://test.com/test#!/test1/id"
In JAVA servlet filter I get just http://test.com/test
Note: Google and bing bit convert #! to _escaped_fragment hence I don't see issues with those crawlers.

how to send file objects to servlet using json

I am new to web programming. My web application can upload files (uploaded by drag and drop method in javascripts ) and i want to retrieve them in servlet using Json . servlet file only needs the contents of the text files to do the calculation.
Can any one suggest how to do this ?
softwares used - netbeans ,tomcat
Thank you.
I'm not quite sure if you mean rendering your files in servlets or actually downloading them from your browser. I'm going to assume you mean rendering them. If so, then what you have to do is set up a URI which is associated with the content you want to render. Let's say this is a simple "hello world" rendering in the browser. what you would do is set up a URI as such:
localhost:3000/helloWorld.html.jsp
What you do on your back end is then wait to receive a http GET request to "/helloWorld.html.jsp" page, and then have your router send over the correct page. Luckily, with tools such as tomcat, the routing from local host is straight forward enough, and as your comments mentioned this is simple enough to do with the ample resources on the web.
Also an important point, you don't need JSON when using servlets (if i understood your problem correctly). If you want to send over data in a JSON format, then all you would do is modify the above steps to listen for "/sendJSON.html.jsp" (this could be an empty jsp), and you send over JSON from your back end in your response.

Apache Http Client Encoding UTF-8 not working

I have the following scenario:
JSP -> Servlet -> ServiceAPI -> Service Servlet
I enter some cyrilic symbols in the JSP page, which is the start of the scenario. On the next step, the Servlet, I read the data from the JSP in UTF-8. So for, so good. Everything is OK.
Then I pass the data to a ServiceAPI, which sends it to a Service Servlet. Here comes the problem. The data in the Service Servlet is read as '??????'. So, I guess the problem is in the Service API which does not send the data correctly. ServiceAPI implementation uses Apache Http Client to send the data to the Service Servlet.
As I read in Apache Http Client documentation (http://hc.apache.org/httpclient-3.x/preference-api.html#HTTP_method_parameters) there is a way to set a character encoding in the request. But I am not able to apply this, becuase of a the following error: "Access restriction: The method setParameter(String, Object) from the type HttpParams is not accessible due to restriction on required library ...". So I am kind of stuck. Do you have any idea if the problem is really in Apache Http Client and I how can I fix it.
Thanks in advance.
To correctly implement Character Encoding in web apps consists of 4 steps
1.First you have to configure your web server.
2.Then you have to force your web app to use UTF-8 encoding for all requests/responses.
3.Third you have to use JSP page encoding.
4.And last you must use HMTL-meta tags.
In your case the problem lies most probably on step 2 IMO
Here is the perfect article for you How to get UTF-8 working in Java webapps? that describes how to do all these extensively

jsonp referer url determine

I have a jquery plugin and I'm using jsonp for crossdomain call to a jsp file.
I want to strict the jsp return values only to specific websites in our database.
To achieve this I need to somehow get the ip or url of the website the jsonp call triggered and not the client/user ip. I've tried the referer value in the http header but this will not work with IE and I guess this is not the best solution either.
How can I securely now who is calling my jsp file with my plugin, from his website?
Thanks in advance.
The simplest answer would be to issue each website a unique key or other identifier that they include in their request. You parse this identifier and flex your response appropriately.
However with a request originating from the client browser, you would have to be careful and would have to evaluate what you mean by how "securely" you need the request to be handled. (since the untrusted client would be making the request it would be a simple task to harvest and reuse such an identifier)...
Referrer (if present) could be used as a double check, but as you pointed out, this is unreliable and coming from an untrusted client computer, this portion of the request could be faked as well.
If we could assume some server side processing by the website owners, you could have them implement a proxy for the jsonp call (which would ensure such a token would never fall into the hands of the browser)... but we'd have to know if such a safeguard would really be worth it or not :)

GWT applications and the returned response from the server

I have some GWT application that run on the server.
we are subscripting with some solution that pings this application in a regular interval of time.
The point is, this solution (service) checks the returned response from the server to contain some pre-defined keywords.
But as you know, GWT return plain empty HTML page with the data contained in the .js file.
So, the Ping service will not be able to exmain the pre-defined keywords, Is this statement true??
And if this is ture, cannot we find any workaround solution to solve such problem?
Thanks.
The problem you are facing is related to the crawlabitlity of AJAX applications - Google has some pointers for you :) Generally, you need a headless browser on the server to generate the output you'd normally see in the browser, for example see HtmlUnit.
Only the initial container page and the loader script that it embeds are HTML & JS. Afterwards, you use GWT's RPC mechanism to exchange Java objects with the server, or Ajax (eg. RequestBuilder) to exchange any kind of data with the server. you name it: JSON, XML, plain text, etc.

Categories

Resources