Servlet Tutorial

Validation of a form

Validation of a form:

Develop a html form and validate that data by using a servlet?

validation form

Validations:

  1. stno - must contain data and it should contain always int data.
  2. sname - must contain data and no special characters are allowed.
  3. smarks - must contain data and it should contain float data.

Answer:

Student database:

create table Student (
stno number (3),
stname varchar2 (15),
stmarks number (5,2)
);

web.xml:

<web-app>
    <servlet>
        <servlet-name>abc</servlet-name>
        <servlet-class>ValidationServ</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>abc</servlet-name>
        <url-pattern>/validation</url-pattern>
    </servlet-mapping>
</web-app>

validpro.html:

<html>
    <title>Validation Project</title>
    <head><center><h2>Student form validation</h2></center></head>
    <body bgcolor="#FFE4C4">
    <center>
        <form name="validpro" action="./validation" method="post">
            <table border="1" bgcolor="A9A9A9">
                <tr>
                    <th>Enter student number : </th>
                    <td><input type="text" name="validpro_sno" value=""></td>
                </tr>
                <tr>
                    <th>Enter student name : </th>
                    <td><input type="text" name="validpro_sname" value=""></td>
                </tr>
                <tr>
                    <th>Enter student marks : </th>
                    <td><input type="text" name="validpro_smarks" value=""></td>
                </tr>
                <tr>
                <table>
                    <tr>
                        <td><input type="submit" name="validpro_insert" value="Insert"></td>
                        <td><input type="reset" name="validpro_clear" value="Clear"></td>
                    </tr>
                </table>
                </tr>
            </table>
        </form>
    </center>
    </body>
</html>

ValidationServ.java:

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

public class ValidationServ extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        ArrayList al = new ArrayList();
        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();
        String sno1 = req.getParameter("validpro_sno");
        String sname = req.getParameter("validpro_sname");
        String smarks1 = req.getParameter("validpro_smarks");
        int sno = 0;
        float smarks = 0;
        if ((sno1 == null) || (sno1.equals(""))) {
            al.add("PROVIDE STUDENT NUMBER...");
        } else {
            try {
                sno = Integer.parseInt("sno1");
            } catch (NumberFormatException nfe) {
                al.add("PROVIDE int DATA IN STUDENT NUMBER...");
            }
        }
        if ((sname == null) || (sname.equals(""))) {
            al.add("PROVIDE STUDENT NAME...");
        }
        if ((smarks1 == null) || (smarks1.equals(""))) {
            al.add("PROVIDE STUDENT MARKS...");
        } else {
            try {
                smarks = Float.parseFloat("smarks1");
            } catch (NumberFormatException nfe) {
                al.add("PROVIDE float DATA IN STUDENT MARKS...");
            }
        }
        if (al.size() != 0) {
            pw.println(al);
        } else {
            try {
                Class.forName("oracle.jdbc.driver.OracleDriver");
                Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:Hanuman", "scott", "tiger");
                PreparedStatement ps = con.prepareStatement("insert into Student values (?,?,?)");
                ps.setInt(1, sno);
                ps.setString(2, sname);
                ps.setFloat(3, smarks);
                int i = ps.executeUpdate();
                if (i > 0) {
                    pw.println("RECORD INSERTED...");
                } else {
                    pw.println("RECORD NOT INSERTED...");
                }
                con.close();
            } catch (Exception e) {
                res.sendError(503, "PROBLEM IN DATABASE...");
            }
        }
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        doGet(req, res);
    }
};