I am getting user input by get method

out.print("<FORM METH0D=GET action =\"index.jsp\">"); 
out.print("<INPUT TYPE=TEXT NAME=searchTerm><BR><BR>");

and using request.getParameter() for getting value.

request.getParameter("searchTerm");

so, if request.getParameter() is null i hide some code of my page as you'll see . But although user not search any item and request.getParameter("searchTerm")is null.
this Portion of my page is not working....

String see ="null";// here is problem
String saw =(String) request.getParameter("searchTerm");
if(see.equalsIgnoreCase(saw))
{
    out.println("get is false");
    Get = false;}
else {
    Get = true;
    out.println("get is true now "+ request.getParameter("searchTerm"));
    out.print("<br />");
}

Whole page code is here.... (no error)

<%@page import="java.util.Iterator"%>
<%@page import="java.util.Collection"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.io.*"  %>
<%@page import="java.net.URL"  %>
<%@page import="java.net.URLEncoder"  %>
<%@page import="java.net.URLConnection"  %>
<%@page import="org.apache.regexp.*"  %>

<%! String term=""; %>
<%! boolean Get = false; %>
<%

request.getParameter("searchTerm");
URL url = new 
URL("http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?dispmax=10 &db=pubmed&cmd=search&term=" + 
        URLEncoder.encode(term, "UTF-8"));

URLConnection urlConnection = url.openConnection(); 
BufferedReader reader = new BufferedReader(new 
InputStreamReader(urlConnection.getInputStream()));
String s = null;
RE pmidRE = new RE("PMID: ([0-9]+) \\[PubMed"); 
Collection pmids = new ArrayList(); 
while ((s = reader.readLine()) != null) { 
if (pmidRE. match(s )) { 
pmids.add(pmidRE.getParen(1)); 
} 
}
reader.close(); 
String see ="null";// here is problem
String saw =(String) request.getParameter("searchTerm");
if(see.equalsIgnoreCase(saw))
{
    out.println("get is false");
    Get = false;}
else {
    Get = true;
    out.println("get is true now "+ request.getParameter("searchTerm"));
    out.print("<br />");
}

%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<HTML> 
<HEAD><TITLE>PubMed Servlet 1.1</TITLE></HEAD> 
<B0Dy> 
<b>Java for Bioinformatics: </b> 
<h1><font color=red><b>PubMed Servlet version 
l.l</b></font></h1> 
<%
if(Get== true)
    {
out.print("<BR><br>PMIDs found:<br>\n");
int i =1;
for (Iterator iterator = pmids.iterator(); 
iterator.hasNext();) { 
String si = (String) iterator.next(); 
out.print("<a href=\""); 
out.print("http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?cmd=Retrieve&db=pubmed£idopt=Abstract&list_uids=") ;
out.print(si) ;
out.print("\">") ;
out.print(si) ;
out.print("</a>\n"); 
if (iterator.hasNext() && i++ != 5) { 
out.print(" - ") ; 
} else { 
out.print("<BR>"); 
} 
}

}

out.print("<br/> <br /><B>Please enter a term to search on NCBI<br /> PubMed:</B><BR><BR>" );
out.print("<FORM METH0D=GET action =\"index.jsp\">"); 
out.print("<INPUT TYPE=TEXT NAME=searchTerm><BR><BR>");
out.print("<INPUT TYPE=SUBMIT VALUE=Search PubMedX\"><BR>" ); 
out.print("</FORM> " );
out.print("</BODY>" ); 
out.print("</HTML>");
        %>

Have you tried if(saw == "") ?

Have you tried if(saw == "") ?

martin5211 please, If you don't know what you are saying don't post wrong advices. Be sure about what you post and test it before you post it.
Don't use ==. Never use ==.

The problem is that if you don't enter anything at the text field, it will return an empty String, not null.
It will return null if the searchTerm doesn't exist in the request, meaning if you didn't have such field.

Try and write this:

String abc = request.getParameter("some_crazy_string_12312312");

abc will be null.
But if you have searchTerm as a field and you don't enter anything then you will get what you passed which is an empty String "".

Also this is totally wrong:

String see ="null";

see is NOT null, it is "null". null means it doesn't exist. "null" means it has the value of the String "null". When null you cannot use it to call methods.

You cannot do this: You will get an exception:

String s = null;
s.length();

But this will return 4:

String s = "null";
s.length();

Study a bit more about jsp. Your methodology is wrong. Don't use out.print() . Use scriplets.
Don't use servlets to write html code. Change this:

out.print("<br/> <br /><B>Please enter a term to search on NCBI<br /> PubMed:</B><BR><BR>" );
out.print("<FORM METH0D=GET action =\"index.jsp\">");
out.print("<INPUT TYPE=TEXT NAME=searchTerm><BR><BR>");
out.print("<INPUT TYPE=SUBMIT VALUE=Search PubMedX\"><BR>" );
out.print("</FORM> " );
out.print("</BODY>" );
out.print("</HTML>");

To this:

<br/> <br /><B>Please enter a term to search on NCBI<br /> PubMed:</B><BR><BR>
<FORM METH0D=GET action ="index.jsp">
<INPUT TYPE="TEXT" NAME="searchTerm" /><BR/><BR/>
<INPUT TYPE="SUBMIT" VALUE="Search PubMedX" /><BR/>
</FORM> 
</BODY>
</HTML>

And put it in a JSP file.

The same for the rest

First :
if i do...
out.prinln(request.getParameter("searchTerm"));
it prints null.
thats why i put a null "string" in see string variable .
I knew that null and "null" are totally different things.

Second:
About Don't use out.print() :
yes thanks for it i notice it and change it with pure html code .

Finally:
but problem is that i wanna check that user enter search term or not (just load or refresh page) .
In php if(!$_GET["searchTerm"]) work for me here.
i am new to jsp and want to do this thing in jsp.
any method , global variable or something like that.... you can suggest plz?

So from what I understand, you have a form and you submit to a page. But if the form is empty you want to go back to the form? Wouldn't be better if you didn't submit the form in the first place?

<HTML>
<HEAD>
<script>
  // javascript method. Executes at the client. No submit takes place
  function validate() {
    var sT = document.myForm.searchTerm.value;
    if (sT=="") {
      alert("PubMed is empty");
    } else {
       document.myForm.submit();
    }
  }
</script>

</HEAD>
<BODY>
  <FORM METH0D="GET" action="index.jsp" name="myForm">
  <INPUT TYPE="TEXT" NAME="searchTerm" /><BR/><BR/>
  <INPUT TYPE="BUTTON" VALUE="Search PubMedX" onclick="validate();" /><BR/>
  </FORM> 
</BODY>
</HTML>

An alternative to that would be not to have javascript at all and submit to the same page. When action="" then you submit to the same page. Then use java to check the input field and then redirect:

<HTML>
<HEAD>
</HEAD>

<%
String searchTerm = request.getParameter("searchTerm");
if (searchTerm!=null && searchTerm.length!=0) {
  // redirect to the next page
%>
   <jsp:forward page="index.jsp" />
<%
}
%>

<BODY>
  <FORM METH0D="GET" action="" name="myForm">
  <INPUT TYPE="TEXT" NAME="searchTerm" /><BR/><BR/>
  <INPUT TYPE="SUBMIT" VALUE="Search PubMedX" /><BR/>
  </FORM> 
</BODY>
</HTML>

The first time the page loads, the searchTerm is null, so from the if nothing happens. If you submit, then the same page loads but this time the searchTerm is not null. In any case the if will always run and if the searchTerm exists and it is not empty, you redirect.

You <jsp:forward /> you don't have to worry if the searchTerm is in the request, because it is. Even if you use page="index.jsp" the searchTerm is alsoe sent so you can take at the index.jsp page

PS:
For javascript read this: http://www.w3schools.com/default.asp

Thanks it is working .. i got my answer
but i wanna share one more thing
that if i do

<%
  String searchTerm = request.getParameter("searchTerm");
    if (searchTerm == null && searchTerm.length() == 0) { // replacing not equal statment
   // dont redirect to the next page
     else
{
   // direct now to next page
     %>
    <jsp:forward page="index.jsp" />

 <%
  }
 %>

<% 
String searchTerm = request.getParameter("searchTerm");
 if (searchTerm == null && searchTerm.length() == 0)
 { 
// replacing not equal statment
 // dont redirect to the next page 
} else { 
// direct now to next page

 %> 
<jsp:forward page="index.jsp" /> 
<%
 }
 %>

This will output "NullPointerException"
because searchTerm is also not equal to null and not equal to zero.
but if i print searchTerm it will print null
and
but if i print searchTerm.length() it will give "NullPointerException"
..
i'm sharing this for otherz who may get this same problem .

Actually the code that you wrote is logically wrong. If you don't want to redirect then the searchTerm must be null OR searchTerm.length==0. not AND
You will not get a NullPointerException if you write it correctly:

if (searchTerm == null || searchTerm.length() == 0) {
  // don't redirect.
}

That is the opposite of my if.

o yes ok
thanks for answering me i get it totally
thanks again.. ;-)

Thanks it is working .. i got my answer
but i wanna share one more thing
that if i do

<%
  String searchTerm = request.getParameter("searchTerm");
    if (searchTerm == null && searchTerm.length() == 0) { // replacing not equal statment
   // dont redirect to the next page
     else
{
   // direct now to next page
     %>
    <jsp:forward page="index.jsp" />

 <%
  }
 %>

<% 
String searchTerm = request.getParameter("searchTerm");
 if (searchTerm == null && searchTerm.length() == 0)
 { 
// replacing not equal statment
 // dont redirect to the next page 
} else { 
// direct now to next page

 %> 
<jsp:forward page="index.jsp" /> 
<%
 }
 %>

This will output "NullPointerException"
because searchTerm is also not equal to null and not equal to zero.
but if i print searchTerm it will print null
and
but if i print searchTerm.length() it will give "NullPointerException"
..
i'm sharing this for otherz who may get this same problem .

if i use searchTerm==null it will work fine
if i use searchTerm=="null" it is wrong dont work
if i use searchTerm.length()==0 result : nullpointerexception

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.