Home Servlets Servlet Error Message based on user input
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Servlet Error Message based on user input

Advertisement
In this example user is presented with a screen to enter the name.

Servlet Error Message based on user input

     

In this example user is presented with a screen to enter the name. If the entered name exists in the database its displays the sucess message otherwise it displays the error message. So, you can use Servlet to check the user input against database and display the message to the user.

This example illustrate how to ensure that user input is correct by validating against the database. In this example any error on the user end is checked by the servlet. If user submits his data then servlet checks user existence in database by connecting to the database, if data is found correct then servlet sends a message to the user for successfully entering the correct data but if data is not correct then sends a message to user for wrong entry of data.

This example illustrates how to show error message on the page when user enters wrong entry for any specific field on the page. For this create a page "test.jsp" which renders an input field to enter the name of the user.

Here is the code for test.jsp:

<%@page language="java" session="true" contentType="text/html;charset=ISO-8859-1" %> 
<font color="blue">Please Enter Your Name To check the error massege</font><br><br>
<form name="frm" method="post" action="../ErrorMessage">
  <table border = "0">
  <tr align="left" valign="top">
  <td>Name:</td>
  <td><input type="text" name ="name" /></td>
  </tr>
  <tr align="left" valign="top">
  <td></td>
  <td><input type="submit" name="submit" value="submit"/></td>
  </tr>
  </table>
</form>

The above code displays the page as below:

Now, we will create a servlet to check whether the user has entered the name as "sandeep" in the input field of the page. This servlet checks the name in the database and if it finds the name in the database then displays the greeting text indicating that the user has entered successfully, otherwise it displays the error message on the page showing the wrong entry.

Here is the code for servlet "ErrorMessage.java"

import java.io.*;
import java.util.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ErrorMessage extends HttpServlet {
  public void service(HttpServletRequest request,
   HttpServletResponse response)

   throws 
IOException, ServletException{
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();  
  Connection con = null;  
  Statement stmt = null;
  ResultSet rs = null;
  String jspname = request.getParameter("name");
  String name= new String("");
  try {
  Class.forName("com.mysql.jdbc.Driver");
  con =DriverManager.getConnection ("jdbc:mysql
://192.168.10.59:3306/example"
"root""root");
  stmt = con.createStatement();
  rs = stmt.executeQuery("SELECT * 
FROM servlet where name='"
+jspname+"'");
  while(rs.next()){
  name=rs.getString("name");
  }
  rs.close();
  stmt.close();
  }catch(Exception e){
  System.out.println(e);
  }
  Boolean isResponseCorrect = Boolean.FALSE;
  if(jspname.equals(name)){
  isResponseCorrect=true;
  out.println("<b><font color='green'>You are 
successfully enter the valid name!</font></b>"
);
  }
  else{
  isResponseCorrect=false;
  out.println("<b><font color='red'>This 
name is Not A Valid Name. Try Again!</font></b>"
);
  }
  }
}

If the user enters the name "piyush"( not "sandeep" or "vineet" or "amit" or "anusmita" which is in database)

then it displays the error message like in the picture below.

If user enters correct name i.e. "sandeep".

then a successful message is displayed.

Download Source Code

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 26, 2008

Ask Questions?    Discuss: Servlet Error Message based on user input  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Mario
June 11, 2013
Thanx

Gracias!!! This is clear and useful. good way to use pictures/images/displays to enhance what you are trying to convey.
Shree
November 30, 2011
ErrorMessage

Good scenario.... Than Q very much
mann
September 20, 2013
registation form show this page in servlet

wait for ans.
DMCA.com