Servlet Tutorial

Handling Html Data in Servlet

Handling Html Datain Servlet:

In order to handle or obtain the data html form in a servlet, we have the following method which is present in HttpServletRequest.

http servlet request

For example:

String sno1=req.getParameter ("sno"); 
String sname1=req.getParameter ("sname"); 
String cname1=req.getParameter ("cname");

req is an object of HttpServletRequest.

Note: An object of HttpServletRequest will be created automatically by servlet container and it contains client requested data.

Write a servlet which accepts client request and display the client requested data on the browser?

Answer:

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

public class DataServ extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();
        String name = req.getParameter("persdata_eurn");
        String cname = req.getParameter("persdata_eurc");
        pw.println("<center><h3>HELLO..! Mr/Mrs. " + name + "</h3></center>");
        pw.println("<center><h3>Your COURSE is  " + cname + "</h3></center>");
    }

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

web.xml:

<web-app>
    <servlet>
        <servlet-name>abc</servlet-name>
        <servlet-class>DataServ</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>abc</servlet-name>
        <url-pattern>/getdata</url-pattern>
    </servlet-mapping>
</web-app>

Note:

  1. Whenever we are not entering the data in html form that data will be treated as empty space.
  2. Query string represents the data which is passed by client to the servlet through html form. URI stands for Uniform Resource Indicator which gives the location of servlet where it is available. URL stands for Uniform Resource Locator which gives at which port number, in which server a particular JSP or servlet is running. ht represents a context path which can be either a document root or a war file.

Write a servlet which accepts product details from html form and stores the product details into database?

Answer:

Product database:

create table Product (
pid number (4),
pname varchar2 (15),
price number (6, 2)
);

web.xml:

<web-app>
    <servlet>
        <servlet-name>abc</servlet-name>
        <servlet-class>DatabaServ</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>abc</servlet-name>
        <url-pattern>/getdata</url-pattern>
    </servlet-mapping>
</web-app>

prodata.html:

<html>
    <title>Product Data</title>
    <head><center><h2>Product Information</h2></center></head>
    <body bgcolor="#BDB76B">
    <center>
        <form name="prodata" action="./getdata" method="post">
            <table border="1" bgcolor="#E9967A">
                <tr>
                    <th>Enter product id : </th>
                    <td><input type="text" name="prodata_pid" value=""></td>
                </tr>
                <tr>
                    <th>Enter product name : </th>
                    <td><input type="text" name="prodata_name" value=""></td>
                </tr>
                <tr>
                    <th>Enter product price : </th>
                    <td><input type="text" name="prodata_price" value=""></td>
                </tr>
                <tr>
                <table>
                    <tr>
                        <td align="center"><input type="submit" name="prodata_insert" value="Insert"></td>
                        <td align="center"><input type="reset" name="prodata_reset" value="Clear"></td>
                    </tr>
                </table>
                </tr>
            </table>
        </form>
    </center>
    </body>
</html>

DarabaServ.java:

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

public class DatabaServ extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();
        String pid1 = req.getParameter("prodata_pid");
        String pname = req.getParameter("prodata_pname");
        String price1 = req.getParameter("prodata_price");
        int pid = Integer.parseInt(pid1);
        float price = Float.parseFloat(price1);
        try {

            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:Hanuman", "scott", "tiger");
            PreparedStatement ps = con.prepareStatement("insert into Product values (?,?,?)");
            ps.setInt(1, pid);
            ps.setString(2, pname);
            ps.setFloat(3, price);
            int i = ps.executeUpdate();
            pw.println("<h4>" + i + " ROWS 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);
    }
};