Link to home
Start Free TrialLog in
Avatar of Sh_Rashed
Sh_Rashed

asked on

Get Entered Fields in the Servlet

I have search form with four inputs field and the user can enter one field or two or four (it is optional), then click search.

In the Servlet, How I can get the fields that the user has entered?
Avatar of Peter Kwan
Peter Kwan
Flag of Hong Kong image

You can use request.getParameter("field") to get values. If there is no value input, then this method returns null.
Avatar of Sh_Rashed
Sh_Rashed

ASKER

But How I can write query to seach the database?
You can use JDBC. What database are you using?
I know how to connect and excute query.. BUT

for example I have this form:

<form method="POST" action="action.do">
      <p>Name: <input type="text" name="name" size="20"></p>
      <p>Email:<input type="text" name="email" size="20"></p>
      <p>Company: <input type="text" name="company" size="20"></p>
      <p>&nbsp;</p>
      <p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>

the user may want to search for person whose name is Jane and company is IBM. so he will enter two fields only ..

so in the servlet how I can get the name and company and make my query to search and find all person whose name is jane and company is IBM.
String p1 = request.getParameter("field1");
String p2 = request.getParameter("field2");
String p3 = request.getParameter("field3");
String p4 = request.getParameter("field4");
 
if(field1 == null) ; //report error
string query = "select * from table where field = " + field1;
if(field2 != null) query += " OR field = " + field2;
if(field3 != null) query += " OR field = " + field3;
if(field4 != null) query += " OR field = " + field4;
String p1 = request.getParameter("field1");
String p2 = request.getParameter("field2");
String p3 = request.getParameter("field3");

 
if(field1 == null && field2 == null && field3 == null) ; //report error
string query = "select * from table where ";
boolean first = true;
if(field1 != null) {
if(!first)query += " AND";
first = false;
query += " field1 = '" + field1 + "'";
}

if(field2 != null) {
if(!first)query += " AND";
first = false;
query += " field2 = '" + field2 + "'";
}

if(field3 != null) {
if(!first)query += " AND";
first = false;
query += " field3 = '" + field3 + "'";
}

Why should I report error if field1 is null?

it is optional .. may  he need to search for all person who work in IBM?
ASKER CERTIFIED SOLUTION
Avatar of enachemc
enachemc
Flag of Afghanistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank u that what I need .. u deserve the points
Or if all are optional:

String f1 = request.getParameter("Field1");
String f2 = request.getParameter("Field2");
String f3 = request.getParameter("Field3");
String f4 = request.getParameter("Field4");

String sql = "";
String sql1 = "";

if (f1 == null && f2 == null && f3==null && f4 ==null)
   sql = "select * from yourtable";
else {
   sql += "where ";
   sql1 = "";
   if (f1 != null)
         sql1 = "field1 = '" + f1 + "'";
   
   if (f2 != null) {
        if ("".equals(sql1))
            sql1 = " field2 = '" + f2 + "'";
        else
             sql1 += " and field2 = '" + f2 + "'";
   }

   if (f3 != null) {
        if ("".equals(sql1))
            sql1 = " field3 = '" + f3 + "'";
        else
             sql1 += " and field3 = '" + f3 + "'";
   }

   if (f4 != null) {
        if ("".equals(sql1))
            sql1 = " field4 = '" + f4 + "'";
        else
             sql1 += " and field4 = '" + f4 + "'";
   }

   sql += sql1;
}

// your query