JSP Request Parameter truncated after passing to javascript function - java

I have an issue with a parameter that I'm trying to pass into a Javascript function where the parameter gets cut off.
In my Servlet, I have set a parameter request.setAttribute("questions", service.getQuestions("123"))
It sets a list of questions with each question containing several values;
I loop through them with a JSTL loop <c:forEach var="data" items="${questions}">...</c:forEach> which I can then access the values as so ${data.question}, ${data.options} etc.
console.log(${data.question}) returns a value of the form 123,45,35|43,94,73|23,91,34 which is as expected.
But when I try to pass this ${data.question} into a javascript function such as <script>MyFunction(${data.question})</script>, it only receives 123.
MyFunction(data) {
console.log(data); //Only shows 123
//Split the string into arrays for processing
}

You receive first element becouse your function expect one parameter, and your value 123,45,35|43,94,73|23,91,34 is split by comma so it looks for function like diferent parametrs. Use arguments property insted or pass all value as string in '' like this
<script>MyFunction('${data.question}')</script>
Sorry for my english.. still working on it

Related

Request parameter is modified in my servlet

I sent one request as URL with data to servlet, But by default servlet is modifying the data and sending as request. Can you please suggest how to maintain the request URL with data which i passed to servlet should remain same ?
Example:- when i am passing the data to servlet
http://localhost/helloservlet/servlet/ppd.abcd.build.coupons.CouponValueFormatterServlet?dsn=frd_abc_abcde&lang=ENG&val=PRCTXT|12345 &ABCDEFG
when it using the above url in servelt as request , like string abc = request.getParameter("val"), the val attribute is trimmed automatically and assigned as " val=PRCTXT|12345" but it supposed to be like " val = PRCTXT|12345 &ABCDEFG ". Please help me on this.
The servlet interprets each & in the URL as the start of a new parameter. So when it sees &ABCDEFG, it thinks you are sending a new parameter called ABCDEFG with no value (though this is technically a "keyless value" according to the specifications).
Two things to fix this, first is when you want to actually send an &, use %26 instead. This will be skipped by the code that divides up the parameters, but converted to a real & in the parameter's value.
Second is to replace spaces with +. Spaces in URLs work sometimes but can be problematic.
So your actual request URL should look like this:
http://localhost/helloservlet/servlet/ppd.abcd.build.coupons.CouponValueFormatterServlet?dsn=frd_abc_abcde&lang=ENG&val=PRCTXT|12345+%26ABCDEFG
If you're building these parameters in javascript, you can use encodeURIComponent() to fix all problem characters for you. So you could do something like this:
var userInput = *get some input here*
var addr = 'http://www.example.com?param1=' + encodeURIComponent(userInput);

Sending boolean as request parameter

In my web application I have a link - "Create New User". From the jsp I am sending some request to the server like -
<div style="float:right" class="view">
Create New User
</div>
Here user.hasPermission[] is an array of boolean. If the current user (that is user ) has the permission(that is 'createUser') to create an new user than it returns true.
Now from my controller I am trying to get the value from the request parameter, like -
request.getParameter("hasCreatePermission");
But the problem is request.getParameter() returns a String. So how can I get the boolean value from the parameter. There is no overloaded version of request.getParameter() method for boolean.
I don't think it is possible. Request is always String content. But you can do
boolean hasCreatePermission= Boolean.parseBoolean(request.getParameter("hasCreatePermission"));
If you are sure it's a boolean you can use
boolean value = Boolean.valueOf(yourStringValue)
All parameters are translated by servelt as String. You need to convert String's value to Boolean.
Boolean.parseBoolean(request.getParameter("hasCreatePermission"));
To avoid manual parsing, you have to use a framework like Spring MVC or Struts.
In servlet it will accept all inputs given by users as string.So, we should parse the input after we got the inputs.
e.g
boolean flag = Boolean.parseBoolean(req.getParameter("Flag "));
I think this will be useful for you.

InnerHTML doesn't brings the value to servlet

cell = this.getElementsByTagName("td")[3];
uname = cell.innerHTML;
i get the value of the particular cell through innerHTML and pass that value to Servlet
xmlhttp.open("POST","UserServlet",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("uname="+uname);
in Servlet
uname = request.getParameter("uname");
out.print(uname);
System.out.println(uname);
i get "undefined" in console.......
is there any way get the value and pass that to servlet
i tried .innerHTML,.innerText,.value nothing worked,i tried in array too....but nothing worked......Help me thanks in advance.....
Please refer to https://stackoverflow.com/a/15312976/1031191. That implies that your xmlhttp code is just fine. Try and use javascript console in the browser to verify that uname is a string and contains the right data.
Receiving "undefined" means that the value of uname is exactly "undefined" on the client side. See getParameter's reference: http://docs.oracle.com/javaee/1.3/api/javax/servlet/ServletRequest.html.
It says that you must receive either a String or null. (so in your case the argument of xmlhttp.send() is "uname=undefined" for some reason.)
UPDATE 2:
Probably you need document.getElementsByTagName('td')[3] instead of "this".
But hey, if you use jQuery anyway, why don't you write $('td').get(3) instead of getElementsByTagName?
UPDATE 3:
I think you have less than 4 td elements in your html. Please note that javascript arrays are indexed from 0. You receive "undefined" if you accidentally try to access an index in an array that is out of bounds.

Passing nullable parameters to a Page via PageParameters in Wicket 6

I need to pass a parameter to my page and I can't find a way to pass parameters that might be null.
If I do:
PageParameters pageParameters = new PageParameters ();
pageParameters.add ("key", null);
This will result in an exception
java.lang.IllegalArgumentException: Argument 'value' may not be null.
at org.apache.wicket.util.lang.Args.notNull(Args.java:41)
If I use Google Guava's Optional, I can't find any way to cast the object even if the the Optional object is not holding a null ie not equals to Optional.absent() :
In my landing page's constructor I do
StringValue sv = parameters.get ("key");
sv.to ( Optional.of (MyEnum.SOME_ENUM_CONSTANT).getClass () );
and when I run it I get this error:
org.apache.wicket.util.string.StringValueConversionException: Cannot
convert 'Optional.of(SOME_ENUM_CONSTANT)'to type class
com.google.common.base.Present.
Am I doing something wrong?
Is there any other way to pass a possibly null object in wicket 6?
I noticed in wicket 1.4 they have PageParameters.NULL which seems to have dissapeared in wicket 6.
Thank you
This might be too simple, but what's wrong with
Object value = ?
if (value != null) {
pageParameters.add ("key", value);
}
and
StringValue sv = pageParameters.get("key");
if (!sv.isNull()) {
// process string value
}
All page parameters in wicket will be treated as Strings eventually. The idea is that a page parameter will be on the URL of the request.
From the javadoc:
Suppose we mounted a page on /user and the following url was accessed /user/profile/bob?action=view&redirect=false. In this example profile and bob are indexed parameters with respective indexes 0 and 1. action and redirect are named parameters.
If you add something like x=y&x, the parameter x will appear twice, once with the String y and another with the empty string.
Depending on what you are trying to accomplish I would suggest to either
Don't pass the parameter at all when there is a null value required and use the isNull or toOptional methods.
Use indexed parameters and test for presence of a certain word

JSP Param object to string

I want to send a string to a function defined in jsp taglib. Specifically, I want to send a param that I'm getting in the page.
i.e. Here the second parameter should be a string. If I use ${param.username} it gives an error, probably because ${param.username} is an object and not a string
${insert:insert(5,)}
You can't embed an EL expression (${...}) inside another EL expression (${...}). You just need
${insert:insert(5, param.username)}

Categories

Resources