url mapping in servlet [duplicate] - java

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.

Related

How to pass hashmap to jsp from servlet request.setatttibute [duplicate]

This question already has answers here:
How to loop through a HashMap in JSP?
(3 answers)
Closed 5 years ago.
How to pass linkedhashmap from java servlet to jsp using request.setatttibute() and how to use it in the jsp`
LinkedHashmap<String,Person> link = new LinkedHashmap<String,Person>()
request.settattribute("hash",link)
Now how do I get it in the jsp and make it work
Here is how you can put the request attribute and then get it.
Map link = new LinkedHashMap();
link.put("key", "value");
//put map as request attribute
request.setAttribute("hash",link);
//get map as request attribute from the request
Map link1 =(Map)request.getAttribute("hash");

URL decoding resulting in question marks in tomcat [duplicate]

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();

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.

How do I rewrite a URI to a destination with a query string [duplicate]

This question already has answers here:
How do I preserve the existing query string in a mod_rewrite rule
(2 answers)
Closed 8 years ago.
We are trying to redirect bin-ends2 to wines.jsp with a number of parameters passed over to the application server. Apache is stripping the parameters off and so the application server fdoes not know what yo put in the page. The Apache config is:
RewriteRule ^/wines/bin-ends2$ http://qa2:7025/wines/wines.jsp?Form=WinesSearch&type=binends [PT]
Does anyone know how to make this work?
You need to add QSA flag to your rule when you introduce new query string parameters and would like to preserve (better say, include) existing query string.
Your rule should be
RewriteRule ^/wines/bin-ends2$ http://qa2:7025/wines/wines.jsp?Form=WinesSearch&type=binends [QSA,PT]
Useful link: http://httpd.apache.org/docs/current/rewrite/flags.html#flag_qsa

Categories