How to look for the value whether it is null or not in JSP. E.g. in below I want to check the description column in pipe table for null values.
String Query1 = "SELECT description FROM pipe where pipe_id='" + id+"' and version_id=2";
SQLResult = SQLStatement.executeQuery(Query1);
SQLResult.first();
description = SQLResult.getString("description");
if(description=="")
{
json_string=json_string+"&desc="+description;
out.println("not null");
} else
{
json_string=json_string;
out.println("null");
}
Well: if (description != null && !description.isEmpty()) ... however are you writing this code within a JSP ? That seems wrong. You should probably do this stuff within a servlet and pass the description as an request attribute to a jsp.
Or you can use StringUtils.isNotEmpty(description).
If you are really using jsp, you should store description in a request attribute and then use <c:if test="${empty description}">.
I use this personal method:
public static boolean isNullOrEmpty(Object obj) {
if (obj == null || obj.toString().length() < 1 || obj.toString().equals(""))
return true;
return false;
}
Related
the value passed fromjsonObject.getString("firstName"); to firstNameValidateUser is null as it doesn't have any value, I need to run the following code which contain String firstName=jsonObject.getString("firstName");.... till returnedUser = new User(firstName, lastName, user.userName, user.password, birthDate, position,qualification,email); when the value of firstNameValidateUser is null. How do I check it,I have used the if condition to check if the firstNameValidateUseris null ,but from the output it seems like it is working the other way round. Is there something wrong with my if condition, or if I have done any other mistake please notify me.. please help me to fix this. Thank you in advance.
firstNameValidateUser=jsonObject.getString("firstName");
// if there are no details are send through the JSON object,
Log.e("jsonObjectlength",jsonObject.length()+"");
Log.e("firstName",firstNameValidateUser);
String usedToCheck=null;
if (firstNameValidateUser!=null && !firstNameValidateUser.isEmpty()) {
Log.e("firstName","firstName is not null");
String firstName = jsonObject.getString("firstName");
String lastName = jsonObject.getString("lastName");
//String username=jsonObject.getString("username");
//String password=jsonObject.getString("password");
String position=jsonObject.getString("position");
String qualification=jsonObject.getString("qualification");
String birthDate=jsonObject.getString("birthDate");
String email=jsonObject.getString("email");
returnedUser = new User(firstName, lastName, user.userName, user.password, birthDate, position,qualification,email);
//values are sent to the returnedUser Object
} else {
Log.e("is Empty","firstName is null");
returnedUser = null;
}
The most easiest way to check is using native API
if (jsonObject.isNull("firstName")) {
Log.e("is Empty","firstName is null");
} else {
Log.e("firstName","firstName is not null");
}
Refer to android API
http://developer.android.com/reference/org/json/JSONObject.html#isNull(java.lang.String)
Wrong condition:
Try:
if (firstNameValidateUser!=null || !firstNameValidateUser.isEmpty()) {
// Rest of your code.
}
Reason:
True && True == True
True && False == False
So from 2nd case, if the second value will be False, it won't enter the if condition.
So after the code edit it seems fine, however if the error still persists then i suggest you check if the following line actually returns something at all.
firstNameValidateUser=jsonObject.getString("firstName");
(Just let it show up in the console using: console.log() )
If that returns nothing the problem is probably in the code that either retrieves the string or the code that sets the string.
I'm having some trouble to create a query where a necessary condition is to use a like into a field where I need to replace a special character. Something like this:
public List<MyClass> search(Myclass object){
Criteria cri = criteria(MyClass.class);
if(object.getName() != null){
cri.add(Restrictions.sqlRestriction("replace("+cri.getAlias()+".name,"+object.getSpecialCharacter()+", '') like '"+object.getName()+"'"));
}
if(dominio.getType()!= null){
cri.add(Restrictions.eq("type", object.getType()));
}
if(dominio.getWorkspace() != null){
cri.add(Restrictions.eq("workspace", object.getWorkspace()));
}
if(dominio.getStatus() != null){
cri.add(Restrictions.eq("status", object.getStatus()));
}
return cri.list();
}
With this code I've got the error below:
this_.ID_MYCLASS as ID_MYCLASS1_33_0_,
this_.NM_MYCLASS as NM_MYCLASS4_33_0_,
this_.ID_STATUS as ID_STATU5_33_0_,
this_.ID_TYPE as ID_TYPE_7_33_0_,
this_.ID_WORKSPACE as ID_WORKS8_33_0_
from KDTB_MYCLASS this_
where replace(this.name,'_', '') like 'COD'
and this_.ID_WORKSPACE=?
ORA-00904: "THIS"."NAME": invalid identifier
What am I doing wrong?
Thanks in advance!
Try this instead:
cri.add(Restrictions.sqlRestriction("replace({alias}.name,"+object.getSpecialCharacter()+", '') like '"+object.getName()+"'"));
You could try this as the first parameter of sqlRestriction:
"replace("+cri.getAlias()+"_.name,"
Ultimately, you're pointing out that HQL doesn't support the replace routine. If you have more trouble, consider ditching Criteria & use a native SQL query for this.
I just want to check that the parameter passed to the jsp are null or not using httpcontext so how i can able to do that
case_yr = Integer.parseInt(request.getParameter("case_yr"));
sub_type_int =Integer.parseInt(request.getParameter("sub_type_int"));
String case_num = request.getParameter("case_num");
String lang_mode = request.getParameter("lang_mode");
String mobile_no = request.getParameter("caller_id");
String dialled_id = request.getParameter("dialled_id");
i want to above values are valid or not I am submitting value from xml page to jsp page and getting value in jsp using request.getparameter()
so pleaase tell how to check values are null or not and there is no sevlet only vxml page and jsp page
Use JSTL
<c:if test="${not empty case_num}">
case_num is NOT empty or null.
</c:if>
if you wanna check if the request attributes are null,just check if they are null this way
String case_num = request.getParameter("case_num");
if(case_num!=null){
//do your logic
}
else {
System.out.println("case_num is null");
}
int number ;
String num = request.getParameter(num);
if(num!=null){
number = Integer.parseInt(num);
}
else {
System.out.println("case_num is null");
}
I am trying to write my first app on Google App Engine, I was trying to maintain a session, I created a login page on submit, it call to a servlet, servlet validates the user and create a new session using the following code.
void createSession(String Username) {
getThreadLocalRequest().getSession(true).setAttribute("Username", Username);
}
login page after calling the servlet redirects to some page i.e. abc.jsp, my abc.jsp page contains
<body><%
try {
if (request.getSession(false) == null) {
} else {
%>
Welcome to
<%=session.getAttribute("Username")%>
<%
if (session.getAttribute("Username").equals("")) {
%>
<b>Login </b>
<%
} else {
%>
<b>Logout</b>
<%
}
}
} catch (Exception e) {
}
%></body>
it works fine, but when I access abc.jsp without creating a session it is throwing an exception at this if (session.getAttribute("Username").equals("")) line, I dunno why kindly help me. I think it dont detect if session exists. but I have read so many threads like this It gave me this solution, I dunno what I am doing wrong.
As far as I remember
session.getAttribute("xyz")
returns null if the attribute does not exist...
so your NullPointerException occurs because you try to call equals on null.
I suggest to do a check on your attribute itself before validating its content:
if (session.getAttribute("Username") == null || session.getAttribute("Username").equals(""))
<%
if (session.getAttribute("Username") != null) {
…
}
%>
Before to check session's attributes, you have to see the session itself.
So, first:
HttpSession session = request.getSession(false);
if(session != null){...}
and then,
if(session.getAttribute("xyz") != null){...}
Better solution might be both in one line:
if(session != null && session.getAttribute("xyz") != null)
returns null if the attribute does not exist... so your
NullPointerException occurs because you try to call equals on null.
Obviously, it's strictly recommended to check your attribute, before validating its content (as above).
It Worked For me. Try This
String userName;
userName = (String)session.getAttribute("uname");
if (session.getAttribute("userName").equals(""))
In following code there is a problem in If Else statement. Only Else part of Request dispatcher is executing even if I don't enter anything in Text Fields. Also, how to check null values of text fields inside Java?
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String Email = request.getParameter("email");
String Password = request.getParameter("password");
if (Email == null || Password == null){
RequestDispatcher rd = request.getRequestDispatcher("JSP Address");
rd.forward(request, response);
} else {
RequestDispatcher rd = request.getRequestDispatcher("JSP Address");
rd.forward(request, response);
}
}
I like to use the method StringUtils.isBlank(String) in the commons-lang (http://commons.apache.org/lang) for this type of checking. It checks if the string is null, empty, or if it contains only whitespaces.
I think the parameters become null only if the form does not send them at all.
Suppose your form is like this:
input type=text name="pass" (not password)
then the parameter you are using in request.getParameter is not present so it is null.
Perhaps the values are (non-null) empty strings? Try:
if (Email == null || Password == null || Email.equals("") || Password.equals("")){
Likely the fields are coming in as empty strings rather than nulls, but we can't tell that for sure without seeing more code.
Running it in a debugger or adding debug prints would clarify.