Display various error messages in a html form

Hi all,
I'm a beginner for servlet. I just want to resolve some basic problems in servlets.
I'm using html form to get inputs from user. first i validated these inputs using servlet. if the user entered empty fields it will go to error page. but now i'm concerning to put various inputs validation such as to enter email address properly (@ sign). currently i'm using one html form to create error page.

if i want to display the different error messages, i need to create another html form.
Is there any other way to mention various error messages using one html form.?....
To undertand more i've put my code here. Also please mentioned about validating email field.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class members extends HttpServlet
{

      Connection connection;


      

      public void init()
      {

            try
            {
                  Class.forName("org.gjt.mm.mysql.Driver");            
                  //loading a driver

                    connection = DriverManager.getConnection("jdbc:mysql://url","password","password");
                  //connecting to a database

            }

            catch (Exception e)

            {

                   e.printStackTrace();



            }


      }



      public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
      {

                  //assigning parameters to variables

                  String Strnme=request.getParameter("name");
                  String Strema=request.getParameter("email");
                  String Stradd=request.getParameter("address");
                  String Strpwd=request.getParameter("password");
                  int    intb=500;
                  int    into=50;


                  response.setContentType("text/html");
                  PrintWriter out=response.getWriter();


                  
                                    
                              
                  try  // try to add the inputs to the punters table in a database

                  {
                        Statement statement = connection.createStatement();

                        //(StrEma.trim().equals("") && StrPwd.trim().equals(""))



                        if (Strnme.trim().equals("") || Strema.trim().equals("") || Stradd.trim().equals("") || Strpwd.trim().equals(""))

                        {
                  
                        out.println("<HTML>");
                        out.println("<BODY>");
                        out.println("<FORM METHOD=\"GET\" ACTION=\"http://url\">");
                        out.println("<H1>Registration Error</H1><p>");
                        out.println("Fields cannot be empty<br>");                        
                        out.println("<INPUT TYPE=\"SUBMIT\" VALUE=\"Go to Registration\">");
                        out.println("</FORM>");
                        out.println("</BODY>");
                        out.println("</HTML>");

                        }


                        else

                        {                  


                        int r=statement.executeUpdate("INSERT INTO runners VALUES('"+Strnme+"','"+Strema+"','"+Stradd+"','"+Strpwd+"','"+intb+"','"+into+"')");
                        // Executing a SQL Query.

                        }


                              
                  }

                  catch (Exception s)
                    {
                          out.println("sorry an error occured"+s);
                          s.printStackTrace();
                    
                      }



            //Statement.close();
            //connection.close(); //closing database connection

            
      }



}










sivakuganAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KuldeepchaturvediCommented:
It really depends as to what the ultimate goal is. I would suggest that these validations should have been done using the javascript on the HTML form itself for any live application.
However if you are just doing this servlet for learning purposes then its a different story.

nways coming to your current problem.. here is how I would have done it..

  public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
      {

                  //assigning parameters to variables
boolean errors = false;
Vector errormsg = new Vector();
                  String Strnme=request.getParameter("name");
                  String Strema=request.getParameter("email");
                  String Stradd=request.getParameter("address");
                  String Strpwd=request.getParameter("password");
                  int    intb=500;
                  int    into=50;


                  response.setContentType("text/html");
                  PrintWriter out=response.getWriter();


                 
                                   
                             
                  try  // try to add the inputs to the punters table in a database

                  {
                        Statement statement = connection.createStatement();

                        //(StrEma.trim().equals("") && StrPwd.trim().equals(""))



                        if (Strnme.trim().equals("") || Strema.trim().equals("") || Stradd.trim().equals("") || Strpwd.trim().equals(""))
{
errors=true;
errormsg.addElement("All fields are required");
}
if(Strema.indexOf('@')<0||Strema.indexOf('.')<0)
{
errors=true;
errormsg.addElement("Please enter a valid email");
}

               if(errors)         {
                 
                        out.println("<HTML>");
                        out.println("<BODY>");
                        out.println("<FORM METHOD=\"GET\" ACTION=\"http://url\">");
                        out.println("<H1>Registration Error</H1><p>");
                        for(int i=0;i<errormsg.length();i++)
                             { out.print("<BR>");
                             out.println(errormsg.get(i));
                            out.print("<BR>");}                        
                        out.println("<INPUT TYPE=\"SUBMIT\" VALUE=\"Go to Registration\">");
                        out.println("</FORM>");
                        out.println("</BODY>");
                        out.println("</HTML>");

                        }


                        else

                        {                  


                        int r=statement.executeUpdate("INSERT INTO runners VALUES('"+Strnme+"','"+Strema+"','"+Stradd+"','"+Strpwd+"','"+intb+"','"+into+"')");
                        // Executing a SQL Query.

                        }


                             
                  }

                  catch (Exception s)
                    {
                          out.println("sorry an error occured"+s);
                          s.printStackTrace();
                   
                      }



            //Statement.close();
            //connection.close(); //closing database connection

           
      }


Now I have typed the code right here in this window, so I do not gaurntee that the syntax is correct.. but still it should give you a fair idea of how it will look.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sivakuganAuthor Commented:
Hi kuldeep,
As u said it's for learning purpose only. so i can't use javascript here. i'm trying to implement with servlet.
and I got ur idea as well but one more thing to ask. In the error message , i need something like button, to say "go to members page". so it can takes the user to register page again. How can i implement that?..
Thanks in advance for ur help.

0
sivakuganAuthor Commented:
Kuldeep,
I'm kindly asking you to post any information regarding the login page. I'm trying to implement that. but haven't got an actual idea about how to implement that..if u can help for this...It would be grateful for me.
0
sivakuganAuthor Commented:
hi kuldeep,

I apologize for the comment regarding validation. i've fully understood ur code. thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.