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.
Related
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);
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
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
I want to pass an Integer value as an attribute in JSP. But when i try
int i = Integer.parseInt(request.getAttribute("count"));
an error is returned. Could you tell me a method to store Integer numbers as attributes in JSP?
I am getting a casting error saying parseInt() is not suited for handling objects.
request.getAttribute returns an Object. you need to cast this to String like this:
Integer.parseInt((String)request.getAttribute("count"));
To access the request in a JSP use request with a lowercase r. It also needs to be in a scriptlet, however the use of scriptlets is not advised since you can easily use JSP EL.
<%
int i=Integer.parseInt((String)request.getAttribute("count"));
%>
If you are displaying this value on the page, you can easily use the expression language:
${count}
request.getAttribute() - returns an Object.
So this object has to be typecasted as follows
int i = (Integer.parseInt)(String.valueOf(request.getAttribute("count")));
I have working example with me..have a look
String Balance = (String.valueOf(session.getAttribute("CostOfTotalTicket")));
int i = Integer.parseInt(Balance);
where CostOfTotalTicket variable i have stored in session is of String type
Try this
<%Object object = request.getAttribute("count");
int val =Integer.parseInt(object.toString());%>
it's working for me
It depends on the type of data type you need to send, for this example I 'm sending data of integer type.
in the first servlet:
request.setAttribute("count", count); //To send data
in the second servlet:
request.getAttribute("count"); //To receive data
this worked for me.
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)}