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
Related
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
I am working on an application, that will pass client input to a vendor using web services. As phase I of the project, the vendor provided us with the XSD's and WSDL information. I used apache CXF to build the client jar. Now the issue I am facing is that, as part of the requirement, I need to send them the SOAP Request in an encrypted(I have taken care of the encryption part) XML file, that they will manually process, and send me back the response in another XML file that I need to parse and retrieve the response object.
Is there anyway to use the client jar in a dummy mode or something, where it looks like we are calling the client, but all we are doing is getting the raw SOAP request to a file
I kind of a hit a dead end and I am not totally sure how to proceed here, any help or suggestions would be appreciated
You might try SoapUI, it's a free web service testing tool. I know you can view the raw data of your soap request and response with it. soapUI
I need to read binary file from intranet http server and get it to download to public.
SCHEMA
intranet file server(Apache)=1 <-->Public http server(Apache Tomcat)=2<-->internet authorized user=3
howto release this without save to filesystem on server 2
Thanks for answers i am new on java.
Sorry for my English.
Use java.net.URL (or another http client) to read from 1 and then print it out (in response to 3).
(In Apache Http Server or Nginx this can be achieved using reverse proxy.)
I can only think of two ways in this situation:
Redirect the internet request to intranet.
In JSP page use:
<% response.sendRedirect("http://intranet_address");%>
or
<c:redirect url="http://intranet_address"/> using standard taglib.
In Servlet page use:
response.setStatus(302);
response.setHeader("Location", "http://intranet_address"); or just
response.sendRedirect("http://intranet_address");
Use a kind of proxy on server 2 to read from server 1 and send directly to internet user without saving to server 2.
I have never tried the first approach on an intranet, but I don't think it would work given the fact the intranet address won't be valid to the internet user.
Now we are only left with the second approach - using a proxy layer. The proxy function could be implemented in many ways: a simple one might be just a bean behind the Servlet to open URL to the file server 1, read file and send it through Servlet response stream to the user or maybe you can use some kind of embedded HTTPClient.
Edit: Since you are going to download binary file, JSP is not a good choice. It's meant to handle textual data. You need Servlet to do binary stream. You can set things like the following on your HttpServletResponse:
resp.setContentType("application/octet-stream");
resp.setContentLength(length);
resp.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"" );
so the content will be send as an attachment with the name you set.
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.
I am trying to put some logging to capture the raw http request coming to my application. My Java code is inside a SpringMVC controller. I have access to the "HttpServletRequest" object. But I could not find a way to get the raw http request stream out of it. There is a reader but only reads the post content. What I want is the whole shebang, the url, the headers, the body. Is there an easy way to do this?
Thanks in advance.
No.
The servlet provides no such API, and it would be hard to implement because (basically) you cannot read the same data twice from a Socket. It is not difficult to get header information, but raw headers are impossible to capture within a servlet container. To get the bodies you need to capture them yourself as your application reads/writes the relevant streams.
Your alternatives are:
Write your own server-side implementation of the HTTP protocol. (Probably not right for your application.)
You may be able to get the header information you need with filters, though they don't show the raw requests.
Some servlet containers have request header logging; e.g. with Tomcat there's a beast called the RequestDumperValve that you can configure in your "server.xml" file.
Implement a proxy server that sits between the client and your "real" server.
Packet sniffing.
Which is best depends on what you are really trying to achieve.
FOLLOWUP:
If the "badness" is in the headers, the RequestDumperValve approach is probably the best for debugging. Go to the "$CATALINA_HOME/conf/server.xml" file, search for "RequestDumperValve" and uncomment the element. Then restart Tomcat. You can also do the equivalent in your webapp's "context.xml" file. The dumped requests and responses end up in "logs/catalina.out" by default. Note that this will give a LOT of output, so you don't want to do this in production ... except as a last resort.
If the badness is in the content of a POST or PUT request, you'll need to modify your application to save a copy the content as it reads it from the input stream. I'm not aware of any shortcuts for this.
Also, if you want to leave logging on for long periods, you'll probably need to solve the problem yourself by calling the HttpServletRequest API and logging headers, etc. The RequestDumperValve generates too much output, and dumps ALL requests not just the bad ones.
No, servlets provide no api to get at the raw request - you might need a sniffer like wireshark for that.
You can get at the parsed request headers and uri though:
getHeaderNames()
getRequestURI()
etc.
I managed to read my raw request in my webapplication deployed on Tomcat 5.5
All I had to do is to read HttpServletRequest through my servlet/Spring controller
using request.getInputStream() only.
It must be the first API approach to the request. before any filter or other command start to mass with the request that cause its completely reading by the webserver.
What's the problem with that approach?