[an error occurred while processing this directive]

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><input type="text" name ="name" /></td>
        <tr align="left" valign="top">
            <td><input type="submit" name="submit" value="submit"/></td>

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)
IOException, ServletException{
    PrintWriter out = response.getWriter();    
    Connection con = null;    
    Statement stmt = null;
    ResultSet rs = null;
    String jspname = request.getParameter("name");
    String name= new String("");
    try {
      con =DriverManager.getConnection ("jdbc:mysql
      stmt = con.createStatement();
      rs = stmt.executeQuery("SELECT * 
FROM servlet where name='"
    }catch(Exception e){
    Boolean isResponseCorrect = Boolean.FALSE;
      out.println("<b><font color='green'>You are 
successfully enter the valid name!</font></b>"
      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


[an error occurred while processing this directive]