Im new to JSP and I am using a flag in my application like the URL below:
http://localhost/MyApp/result.jsp?params
How do I get that flag in the landing page?
You can use request.getQueryString() to access the raw query string (everything after the ? up to the first #, if there was one) as it was in the original URL.
Enumeration en = request.getParameterNames();
while (en.hasMoreElements()) {
String paramName = (String) en.nextElement();
if (paramName.equals("params")) {
....
}
}
If you do like this you don't have to specify a value for the parameter.
You can check this example
request.getParameter("params") will return you a blank-string.
To set a boolean flag, you can simply do.
boolean flag = request.getParamater("params") != null;
In your result.jsp file, you can get the param value within the scriplet tag <%...%> like this :
<%
String params = request.getParameter("params");
%>
Also, as DnR quoted in the comments, you will have to place '=' after the param flag even if you don't want to associate a value with this flag.
Related
I have the following request Url /search?charset=UTF-8&q=C%23C%2B%2B.
My controller looks like
#RequestMapping(method = RequestMethod.GET, params = "q")
public String refineSearch(#RequestParam("q") final String searchQuery,....
and here i have searchQuery = 'CC++'.
'#' is encoded in '%23' and '+' is '%2B'.
Why searchQuery does not contain '#'?
searchQuery in debug
I resolved a similar problem by URL encoding the hash part. We have Spring web server and mix of JS and VueJS client. This fixed my problem:
const location = window.location;
const redirect = location.pathname + encodeURIComponent(location.hash);
The main cause is known as the "fragment identifier". You find more detail for Fragment Identifier right here. It says:
The fragment identifier introduced by a hash mark # is the optional last part of a URL for a document. It is typically used to identify a portion of that document.
When you write # sign, it contains info for clientbase. Put everything only the browser needs here. You can get this problem for all types of URI characters you can look Percent Encoding for this. In my opinion The simple solution is character replacing, you could try replace in serverbase.
Finally i found a problem.In filters chain ServletRequest is wrapped in XSSRequestWrapper with DefaultXSSValueTranslator and here is the method String stripXSS(String value) which iterates through pattern list,in case if value matches with pattern, method will delete it.
Pattern list contains "\u0023" pattern and '#' will be replaced with ""
DefaultXSSValueTranslator.
private String stripXSS(String value) {
Pattern scriptPattern;
if (value != null && value.length() > 0) {
for(Iterator var3 = this.patterns.iterator(); var3.hasNext(); value = scriptPattern.matcher(value).replaceAll("")) {
scriptPattern = (Pattern)var3.next();
}
}
return value;
}
<%String dest = request.getParameter("destination").toUpperCase();%>
Hello...
I got a little bit problem here. I am using the above code to get value from form. When use the code without toUpperCase(), it was a success. But, when I add toUpperCase() I got HTTP Status 500 - An exception occured processing JSP page.
When you get value null from request.getParameter("destination"), apply toUpperCase() to a null value gives an error.
Try to do like this:
<%String dest = request.getParameter("destination");
if(dest!=null){
dest = dest.toUpperCase();
}
%>
The request.getParameter() returns String value or a null value from client.
More than likely, request.getParameter("destination") is returning null in your code, which would be why it's throwing an error. If the parameter is not found, then null is returned, otherwise a String is returned.
So you'll want to verify that it's not null
<% String dest = request.getParameter("destination");
if(dest != null) {
dest = dest.toUpperCase();
}
%>
I m new in GWT and I m generating a web application in which i have to create a public URL.
In this public URL i have to pass hashtag(#) and some parameters.
I am finding difficulty in achieving this task.
Extracting the hashtag from the URL.
Extracting the userid from the URL.
My public URL example is :: http://www.xyz.com/#profile?userid=10003
To access the URL in GWT you can use the History.getToken() method. It will give you the entire string that follows the hashtag ("#").
In your case (http://www.xyz.com/#profile?userid=10003) it will return a string "profile?userid=10003". After you have this you can parse it however you want. You can check if it contains("?") and u can split it by "?" or you can get a substring. How you get the information from that is really up to you.
I guess you already have the URL. I'm not that good at Regex, but this should work:
String yourURL = "http://www.xyz.com/#profile?userid=10003";
String[] array = yourURL.split("[\\p{Lower}\\p{Upper}\\p{Punct}}]");
int userID = 0;
for (String string : array) {
if (!string.isEmpty()) {
userID = Integer.valueOf(string);
}
}
System.out.println(userID);
To get the parameters:
String userId = Window.Location.getParameter("userid");
To get the anchor / hash tag:
I don't think there is something, you can parse the URL: look at the methods provided by Window.Location.
I am trying to pass a string value to a JavaScript function by taking from request parameter in JSP, in my struts based project. here is the code:
<%
String timeVal = "Not found";
if(request.getAttribute("myDate")!=null){
timeVal= (String)request.getAttribute("myDate");
}
%>
and then pass it in function as parameter
<html:submit property = "save" styleClass = "button_c" onclick = "return SubmitPage('update', <%=timeVal %>)">Save</html:submit>
Where the JavaScript function is
function SubmitPage(action, aa)
{
alert("Date is ...." + aa);
}
But when i try to run this it gives me an error
HTTP Status 400 - Request[/AMResourceLibraryListAction] does not contain handler parameter named ref
With message on web page.
Request[/AMResourceLibraryListAction] does not contain handler parameter named ref
Thanks in advance.
EDIT Here is stack trace
[ERROR] DispatchAction - -Request[/AMResourceLibraryListAction] does not contain handler parameter named ref
it's work for me :
<html:submit property = "save" styleClass = "button_c" onclick = "return SubmitPage('<%=timeVal %>')">Save</html:submit>
('<%=timeVal %>') // between single Quotation
Rather using that i will advise you to use value like this in your JavaScript function
var tt = <%=(String)request.getAttribute("myDate")%>
alert(tt+ "Done this....");
Hope this will help you.
Use '<%=timeVal %>' instead of <%=timeVal %> in Javascript method:
<html:submit property = "save" styleClass = "button_c" onclick = "return SubmitPage('update', '<%=timeVal %>')">Save</html:submit>
I'm doing a recursive url harvest.. when I find an link in the source that doesn't start with "http" then I append it to the current url. Problem is when I run into a dynamic site the link without an http is usually a new parameter for the current url. For example if the current url is something like http://www.somewebapp.com/default.aspx?pageid=4088 and in the source for that page there is a link which is default.aspx?pageid=2111. In this case I need do some string manipulation; this is where I need help.
pseudocode:
if part of the link found is a contains a substring of the current url
save the substring
save the unique part of the link found
replace whatever is after the substring in the current url with the unique saved part
What would this look like in java? Any ideas for doing this differently? Thanks.
As per comment, here's what I've tried:
if (!matched.startsWith("http")) {
String[] splitted = url.toString().split("/");
java.lang.String endOfURL = splitted[splitted.length-1];
boolean b = false;
while (!b && endOfURL.length() > 5) { // f.bar shortest val
endOfURL = endOfURL.substring(0, endOfURL.length()-2);
if (matched.contains(endOfURL)) {
matched = matched.substring(endOfURL.length()-1);
matched = url.toString().substring(url.toString().length() - matched.length()) + matched;
b = true;
}
}
it's not working well..
I think you are doing this the wrong way. Java has two classes URL and URI which are capable of parsing URL/URL strings much more accurately than a "string bashing" solution. For example the URL constructor URL(URL, String) will create a new URL object in the context of an existing one, without you needing to worry whether the String is an absolute URL or a relative one. You would use it something like this:
URL currentPageUrl = ...
String linkUrlString = ...
// (Exception handling not included ...)
URL linkUrl = new URL(currentPageUrl, linkUrlString);