Solved

Display various error messages in a html form

Posted on 2007-04-04
4
252 Views
Last Modified: 2013-11-24
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

            
      }



}










0
Comment
Question by:sivakugan
  • 3
4 Comments
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 500 total points
ID: 18849772
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
 

Author Comment

by:sivakugan
ID: 18850158
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
 

Author Comment

by:sivakugan
ID: 18850188
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
 

Author Comment

by:sivakugan
ID: 18850251
hi kuldeep,

I apologize for the comment regarding validation. i've fully understood ur code. thanks.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now