10

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">
 <a href="/some/url/createUserMVC.do?hasCreatePermission=${user.hasPermission['createUser']}">Create New User</a>
</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.

1
  • 2
    Why don't you use Boolean.parseBoolean()? Commented Mar 25, 2015 at 18:43

4 Answers 4

13

I don't think it is possible. Request is always String content. But you can do

boolean hasCreatePermission= Boolean.parseBoolean(request.getParameter("hasCreatePermission")); 
6

If you are sure it's a boolean you can use

boolean value = Boolean.valueOf(yourStringValue)
4

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.