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)}
Related
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.
I am displaying a list which contains objects which store message information. The list is loaded through getting the request as such:
ArrayList<MessageModel> list = (ArrayList<MessageModel>) request.getAttribute("msgList");
I am trying to display information contained in each object as such:
for(MessageModel msg : list){
String messageTo = msg.toAddress;
String messageSub = msg.messageSubject;
out.println(messageTo+ " " +messageSub);
}
I am getting a NullPointerException at the for each statement.
As an aside, I am using this approach to debug as I want to check whether or not I can display the content of the MessageModel object at all. I tried using JSTL forEach tag but I could not access the object attributes.
<c:forEach items="${list}" var="current">
<li><c:out value="${current.toAddress}"/></li>
</c:forEach>
I set the attribute like this :
ArrayList<MessageModel> list =(ArrayList<MessageModel>) request.getAttribute("msgList");
pageContext.setAttribute("messages", list);
This results in the following error message when run :
org.apache.jasper.JasperException: /inbox.jsp (line: 37, column: 12) According to TLD or attribute directive in tag file, attribute items does not accept any expressions
And intelliJ says that it cannot resolve the variable list.
EDIT :
I forward the request from a servlet that handles user login as follows :
ArrayList<MessageModel> msglist = MessageModel.getRecievedMessages(username);
request.setAttribute("msgList", msglist);
I am trying to pass the ArrayList that contains the message objects and then iterate over the list and display it in the jsp.
Is there a possibility for the jsp itself to get the information ? I have a separate java class which handles the messages with a bunch of helper functions. So can I call the method that retrieves all those messages and display them without explicitly setting a request attribute in the servlet ? Because I will be navigating between other pages in the application.
Sorry if my vocabulary was off, this is the first time I am working with jsp's and servlets.
The NullPointerException in the foreach loop is probably due to the list variable being null. You may check for listnot being null before iterating over it.
Regarding the JasperException, can you describe how you forward to the JSP ?
Also, it's probably a typo, but you set an attribute named messages, then you iterate over an attribute named list in the <c:foreach>...
I solved this problem by using creating getter and setter methods to get the class members of MessageModel.
EL tag wiki: this article helped me understand how to interface a java class and a jsp.
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
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.
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.