Storing integer as attribute in jsp - java

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.

Related

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.

JSP Request Parameter truncated after passing to javascript function

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

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)}

What actually means "value" in JSP?

I have read JSP recently, and have a doubt in the javabeans technolgy it uses. Lets say that the following JavaBeans code :
package mortgage;
public class Mortgage
{
private double amount = -1.0;
public void setAmount(double amount)
{
this.amount = amount;
}
}
And lets say i have to make use of this JavaBeans in my JSP and take the parameter values obtain from the HTML form or from the URL query string and JSP code as follows:
<jsp:useBean id="calc" class="mortgage.Mortgage" />
<p> Testing . . .
<c:set target="${calc}" property="amount" value="${param.mortgageAmount}" />
. . . . .
This example was little modified from my book. My question is what does this value in the above code JSP does? Where does the mortgageAmount came from?(is this the value from the HTML form element?)
And also what does target and property does?
Since I am a novice, i dont know what actually is going on the above code. Please help me and correct me if am wrong?
value represents expression that would be set to the target
Where does the mortgageAmount came from?
it assumed to be coming as param as you have used it in your code by param.mortgageAmount in url like
yourapp/page.jsp?mortgageAmount=someVAlue
In Simlper words
value is and Expression to be evaluated which will be set to
target object's property represented by property
See Also
Javadoc
param is a JSP implicit object. It's a map whose entries are the page parameters - so anything that's come in as a parameter in the query string, or (i think) through a form post.
Target and property govern what the c:set does; it sets the named property on the named target object to the given value.

Categories

Resources