How to hide value in URL - java

192.168.178.83:18300/Zupdate_we_view.act?name=bunnavitou&position=Research111&sex=Male
hide value on url
from this one :
192.168.178.83:18300/Zupdate_we_view.act?name=bunnavitou&position=Research111&sex=Male
==>To this :
192.168.178.83:18300/Zupdate_we_view.act?
Need Help !! JAVA or JAVA SCRIPT

The best way to hide values on url is to use POST method.
Click HTTP Methods: GET vs. POST or GET vs POST
for more info about GET and POST methods.

First you have to get the url, modify it, and then with javascript replace the "true" url with the one you just modified. Here is what I would do:
//get the url
$old_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
//then you modify it
$new_url = ...;
//then with javascript you replace the url with the new one
<script type="text/javascript">
if(new_url != '')
{
window.history.replaceState({"html":'Title',"pageTitle":'Page Title'}, '', new_url);
}
</script>
This should do the trick, if you really want to use GET ; POST would be better, and you wouldn't have to hide the GET parameters.

Like others suggested, use POST method, but if you want to stick with GET and JavaScript solution is enough (values can be seen in request same as with POST, but furthermore users with disabled JS will see the values), you can use approach described here: https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history

Another way is to encode the values on first page & pass those values and decode the value
on second page.
Using that technique you have no need to post data. User can also read the parameter but
it is encoded not the real value.
You can use different algorithm to encode/decode parameter on both side.

Related

how request.getparameter() take special characters like #,%,& etc

I pass some parameters in ajax URL and want to get that parameters by request.getParameter(); in controller if that parameters have some special character like #,%,&, etc. then how to get it?
String xyz = new String(request.getParameter("XYZ").getBytes("iso-8859-1"), "UTF-8");
You have two options:
1.Encode values to JSON before sending, and decode them on server.
Use javascript method encodeURIComponent https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
I found best solution after spending couple of hours use
((String[])request.getParameterMap().get("paramname"))[0]
which gives me param value with special charater

Send a tag in a url

First, sorry for my english it's not my native language.
So, I am working on an application in JSP and in one of my forms I have a field "comments". When I submit this form, the value of this field is sent to my servlet by an ajax request.
var request = 'mainServlet?command=SendRequest';
request += ('&comments=' + $('#comments').val());
But when there is a "<" or ">" in the field, $('#comments').val() translate them into "&lt" or "&gl". For exemple, is converted to &lt ;test&gl ;
And when I want to recover the value in my servlet, I do:
String comments = request.getParameter("comments");
But the url looks like : mainServlet?command=SendRequest&comments=&lt ;test&gl ;
So request.getParameter("comments"); returns an empty string.
I thought that I could replace the string like &lt by my own code and then replace it again in my servlet, but is there a simpler way to do this?
Thanks.
Edit: After, I reuse the comments in an other jsp.
I believe what you need is the encodeURIComponent function. It will convert any string into a format that you can use inside a URI.
Just remember to decode it on the receiving end, I believe the URLDecoder class can do this for you.

Uri.parse(), how to get the encoding correct?

I am doing an application where I have to read a URL from a webpage as a String[Its not the address of the page]. The URL that I will be reading contains query string, and I specifically need two queries from that URL. So I am using the Uri class available in Android. Now, the problem lies in the encoding/format of the URL and the query. One of the queries that I need is always an URL. Sometimes the query URL is %-encoded and sometimes not.
The URLs can be like the following :
Case 1 :
http://www.example.com/example/example.aspx?file=http%3A%2F%2FXX.XXX.XX.XXX%2FExample.file%3Ftoken%3D9dacfc85
Case 2 :
http://www.example.com/example/example.aspx?file=http://XX.XXX.XX.XXX/Example.file?token=9dacfc85
How do I get the correct Url contained in the file= query?
I am using the following [to accomplish the said work universally] :
Uri.decode(urlString.getQueryParameter("file"));
Is this the correct way to do it?
UPDATE
I have decided to first encode the whole URL regardless of its value and then get the query parameter. Theoretically, it should work.
If you are uncertain about the type of URL you would get then I would suggest you to decode every URL you get from the parameter. And when you need to use it then you can encode it.
As per my knowledge, you are doing it right.

Search for redirected website path

I have two websites, A and B. When I open website A, I am redirected to website B automatically.
What is the function with which I can check what was the full path of website A from which was the redirect?
I was trying to start with:
logger.info(request.getPathInfo());
logger.info(request.getPathTranslated());
logger.info(request.getServletPath());
logger.info(request.getLocalName());
logger.info(request.getRemoteAddr());
logger.info(request.getRemoteHost());
logger.info(request.getRequestURI());
logger.info(request.getServerName());
but none of them is correct.
For redirecting I use response.sendRedirect inside Controller.
Thanks for help.
You can try using the optional referer header:
request.getHeader("referer");
But it is important to note that this may not always be populated (specifically IE).
A better solution, if you are in control of both of the websites, is to pass the value somehow when you are doing the redirect. For example, as a GET or POST parameter.
Edit:
As suggested above, you can append query strings to your redirect URL. For example, you might try something like this:
String redirectUrl = "http://my.redirect.com/";
redirectUrl += "?referer=";
redirectUrl += URLEncoder.encode(request.getRequestURL().toString(), "UTF-8");
Then you can just pull this out of the request on the other side.
Use this as a starting point. You may need to manually append other query parameters that may not be part of the getRequestURL() output.
None of these would get you the page that redirected you to the current page. What you can try is:
String refererPage = request.getHeader("referer");
However keep in mind that this is also browser dependent and may not always be present.
Try this
request.getHeader("referer");
Please try
request.getHeader("referer");

How to return the original string in Java after using jquery.serialize()

I have a problem after serializing the form with jquery.
Why some text retain the html entities even after loaded to Java(Servlet)
For example I have a text & and it will return into %26 in Java.
I serialize and submit the form into Java using this..
function ajaxSubmit(frmN){
var serForm = $(frmN).serialize();
$.ajax({
type:'POST',
url:'inser',
data:{actionName : "insertField", formField : serForm},
success: function(request){
$("#reqContainer").html(request);
}
});
}
Is there a way to deserialize the html entities from java.
I guess I need first to split the & and then split the =
to get the list of field and its value, and after that the
deserialization will begin.
I'll appreciate any help.
I read some article using JSON but I don't have time to study it.
If there is an alternative way submitting all the form values via
ajax with jquery, and will get the original
value from Java please let me know.
Did you try JQuery form plugin? I remember a ajaxSubmit utility method.
http://malsup.com/jquery/form/
Otherwise you could just use URLDecoder in java. This will change your ascii character back to its original string.

Categories