get the request parameter from url with & symbol - java

I am trying to get the request parameter which has '&' sybol in starting like:-
http://localhost:8080/simple.jsp?my=&234587
On other page I'm getting it like String value=request.getParameter("my");
value.substring(0,4);
I want to get &234, please suggest i am not getting any value.
Thanks,
Ars

In this example you have not one, but two parameters:
my
234
234 is not a value here. The & separates query parameters. If you need that ampersand to be part of the value of my, it needs to be escaped in the URL as %26.

thanks for resonse i found the answer, I used request.getQueryString(); to get the whole string i.e. &234587 and then parsed it accordingly.
:)
Thanks once again.

You will most likely have to encode the '&' in the url:
http://localhost:8080/simple.jsp?my=%26234587

If you will be handling only Get request. You can write something like this.
String requestURI=request.getRequestURI();
String pContent=requestURI.split("?")[1];
String[] pArray= pContent.split("&");
String value="";
for (int i=0 ;i<pArray.length;i++) {
if(pArray[i].equals("my"+"=")){
value="&" + pArray[i+1];
break;
}
}

Related

How to remove special characters from a String in java?

I want to remove special characters from a String which is passed by a jsp file
I am receiving the value 2019/05/09 and I want to remove all the "/".
Expected output - 20190509
How can I do this? Thanks for the time!
You need to remove the '/' and then do the parsing.
int checkin = Integer.parseInt(request.getParameter("checkin").replace("/",""));
Hi i assume that the datatype that you get in the request parameter is String.
You may use this.
int checkin = Integer.valueOf(request.getParameter("checkin").replace("/",""));

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.

Java Web: Detect a URL with a trailing question mark and empty query string

Is there any way I can tell if there is a trailing question mark in a URL? This would theoretically be an empty non null query string while no question mark at all would be a null query string. But either way, my web app is getting request.getQueryString() == null.
String url = request.getRequestURL().toString();
if(url.indexOf("?")== -1){//it doesn't}
How about something like this:-
boolean hasTrailingQuestionMark = "GET".equals(request.getMethod()) && request.getParameterNames().hasMoreElements();
I could be wrong, but if the request is a GET and it has parameters, then I think we can safely assume there is a trailing question mark after the URI.
UPDATE
I just tested the code, this approach works only if you have parameters: http://server/bla?param=1. However, if you have just http://server/bla?, this condition will fail. I don't know if you are trying to capture the latter URL signature.

How can I get URL parameters which have no value?

access using URL:
http://127.0.0.1/test.jsp?action=test&abc
or
http://127.0.0.1/test.jsp?abc
how can I get the String "abc" ?
thanks for help :)
<% java.util.Enumeration names = request.getParameterNames();
while(names.hasMoreElements()){
out.println(names.nextElement() + "<br>");
}
%>
I don't think there is a simple way to do this. Basically you need to iterate over the request's query parameter names and look for the parameter or parameters that don't have a value. I suspect that you will need to resort to embedding a Java scriptlet, or writing your own Tag (in Java).
A better idea is to stick to "name=value" syntax in your URL query.
request.getQueryString();
returns the whole query string after the URL.

Categories