URL decoding resulting in question marks in tomcat [duplicate] - java

This question already has answers here:
How to set request encoding in Tomcat?
(2 answers)
Closed 6 years ago.
I'm calling a url like this:
http://localhost:8080/abc/income?content=%E0%B6%B8%E0%B6%9C%E0%B7%99+%E0%B6%B1%E0%B6%B8+%E0%B6%BD%E0%B6%9A%E0%B7%8A%E0%B6%B8%E0%B7%8F%E0%B6%BD%E0%B7%8A.&SOURCE_PRV=%20HTTP/1.1
and in the backend (tomcat) I'm decoding the string.
But I get the content as "??? ?? ???????" (several question marks).
How can I fix this?

The java.net.URI class can help; in the documentation of URL you find
Use one of the constructors with more than one argument, like:
URI uri = new URI(
"http",
"search.barnesandnoble.com",
"/booksearch/first book.pdf",
null);
URL url = uri.toURL();
//or String request = uri.toString();

Related

url mapping in servlet [duplicate]

This question already has answers here:
Servlet and path parameters like /xyz/{value}/test, how to map in web.xml?
(7 answers)
Closed 7 years ago.
I new to servlet and trying to find a way to get the values from url for example /api/v1/http://localhost:8080/getInfo/product/1/order/22 So I know how to map url getInfo in web.xml. All I am looking for a way to get the product value and order value from url. How we do this in servlet? Any direction will be very helpful.
Thanks
following the conversation, I would suggest to use
request.getPathInfo() to get any information appended to the servlet url and then parse it depending on your params names.

how to remove parameter from url when redirecting to get method in `Java` [duplicate]

This question already has answers here:
Spring MVC Controller: Redirect without parameters being added to my url
(6 answers)
Closed 7 years ago.
I want to remove the parameter from url which is coming from HeaderInterceptor.java in postHandle method when I load the get method after redirect from post method.
What is written in my HeaderInterceptor.java file is
modelAndView.addObject("roleId", stu.getRoleId());
So, when I redirect from post method to get , the url which come is:
http://localhost:8080/System/StudentList.htm?roleId=23
So, here I want to remove is ?roleId=23.
Following is the code written in POST method:
String referer = request.getHeader("referer");
return "redirect:"+referer;
But I m not able to see any parameter in referer string.
The code I tried in POST method is :
RedirectView redirectview = new RedirectView("?");
redirectview.setExposeModelAttributes(false);
but it is still showing the same url with parameter.
Any help would be appreciated. Thanks :)
You can manually remove the URL parameters by calling something like
String newURL = url.substring(0, url.indexOf("?"));

Check String is URL in android code? [duplicate]

This question already has answers here:
How to check if URL is valid in Android
(12 answers)
Closed 8 years ago.
I allow user to input text string in my EditView. When I get Editview text, I want to validate if it is URL link or not? Can any one give Android code function? Thanks
Use WEB_URL pattern in Patterns (android.util.Patterns) Class
Patterns.WEB_URL.matcher(inputTextForURLEdittext.toLowerCase()).matches();
It will return true if URL is valid and false if URL is invalid.
For more info refer to this link
http://developer.android.com/reference/android/util/Patterns.html
You can check it by using URLUtil .
if (URLUtil.isValidUrl(urlString)) {
// URL is valid
}
Hope it will be work for you.

Java URL Encode how to encode Hello World as Hello%20World [duplicate]

This question already has answers here:
URLEncoder not able to translate space character
(19 answers)
Closed 9 years ago.
Hi Um quite new to Java and I want to encode Hello World it to Hello%20World . But when I use URLEncoder.encode it will encode the string as Hello+World and when we try to pass it via a Request to IIS it ll consider as a threat. How to encode as i mentioned ? Thank you in advance.
This only works for real URLs
URL u = new URL("Hello World");
String path = u.getPath();

Getting the last part of the referrer url [duplicate]

This question already has answers here:
How to obtain the last path segment of a URI
(13 answers)
Closed 7 years ago.
I wanted to get the last part of the referrer URL. How would be the best way to obtain this information?
String referrer = request.getHeader("referer");
Currently this is how I am getting the referrer URL. This is giving me the entire URL, but I only want part of the URL.
For Example: Requested URL: http://localhost:8080/TEST/ABC.html
If this is the referrer URL, I only want the ABC.html.
Thank you for the assistance, please let me know if there are any misunderstandings to my question.
This will give you XYZ.html :
String url = "http://localhost:8080/TEST/XYZ.html";
url = url.substring(url.lastIndexOf("/") + 1);
Use java.net.URL.getFile()
String path = new URL( request.getHeader( "referer" )).getPath();
int sep = path.lastIndexOf( '/' );
return ( sep < 0 ) ? path : path.substring( sep + 1 );
It's basic string manipulation, treat it like any other string. You can use String.indexOf/String.lastIndexOf and String.substring, or you can use String.split, or you can use StringTokenizer, or you can use Regular Expressions (the most flexible option, but requires learning regex).
Just get the substring after the last / occurence.

Categories