• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Servlet

MySql Concat Separator Function in Servlet

By Dinesh Thakur

This function ‘CONCAT_WS’ returns the value with separater.

i make a table in a database named ‘dbase’ with mandatory fields and value in it within the reference of mySql(php myAdmin). Then i import all the required java packages from java library. I made a class named ‘MySqlConcatSeparatorFunc’ which completely extends the ‘HttpServlet’.i used serviceMethod() which is responsible for getting request from doGet()Method for output on the web browser. Before declaring the variables i loaded all the required drivers for database accessing. Then i declare some mandatory variable like i declare ‘Connection’ which is been responsible for creating a link between database and the java code. The other variable will be ‘resultSet’ this variable will use to get the value from selected columns and rows on the output table. The other variable is ‘preparedStatement’ this variable is used to executing the selected query like executeQuery() as (Select CONCAT_WS(‘,’,’Radhika’,’Tanwar’)). In last i use doGet() method which will bring the output as required condition.

as on the web browser to get output in a manner way i use some tags of ‘HTML’ which appear the output in a tabular form.

    
EXAMPLE
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlConcatSeparatorFunc extends HttpServlet
 {
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
    {
      rp.setContentType("text/html");
      PrintWriter disp = rp.getWriter();
      String driver = "com.mysql.jdbc.Driver";
      String url = "jdbc:mysql://localhost/dbase";
      String uid = "root";
      String psw = "root";
      Connection con=null;
      PreparedStatement ps = null;
      ResultSet rs;
      try
       {
         Class.forName(driver);
         con = DriverManager.getConnection(url,uid,psw);
         ps=con.prepareStatement("Select CONCAT_WS(',','Radhika','Tanwar')");
         rs = ps.executeQuery();
             String title = "Using Concat With Separator Function";
         String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
             disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
         "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
             "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value Is </th>\n"+ "</body> </html>");
         while(rs.next())
           {
             String val = rs.getString(1);
                 disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
           }     
        } 
                 catch(Exception e) 
                   {
                          e.printStackTrace();
                   }
                          disp.close();
     }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                 doGet(rq,rp);
                           }
 }
 
WEB.XML
 
<servlet>
   <servlet-name>MySqlConcatSeparatorFunc</servlet-name>
   <servlet-class>MySqlConcatSeparatorFunc</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlConcatSeparatorFunc</servlet-name>
   <url-pattern>/MySqlConcatSeparatorFunc</url-pattern>
</servlet-mapping>

MySql Concat Function in Java Servlet Example

By Dinesh Thakur

The Function ‘CONCAT’ will add Two String with each other.

In the first instance i make a table  with required Fields and values into the database named ‘dbase’ within the reference of mySql(php myAdmin).then i import all the required java packages from java library. Then i make a class named ‘MySqlConcatFunc’ which extends the ‘HttpServlet’. I use serviceMethod() which will bring the request from doGet()Method for output on the web browser. After that i loaded all the required drivers for database accessing. Here i declare some mandatory variables like i declare Connection (this variable will create a link between database and the java code). Then i use ‘resultset’ that will bring the value from the required columns and rows. i declare ‘preparedStatement’ that will responsible for the executing selected query like executeQuery() as (Select CONCAT(‘My’,’S’,’ql’). Then i use doGet()Method for getting output on the web Browser.

To Getting Output on web browser in a tabular form i use ‘HTML’ code and tags.

 EXAMLPE 
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlConcatFunc extends HttpServlet
 {
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
   {
      rp.setContentType("text/html");
      PrintWriter disp = rp.getWriter();
      String driver = "com.mysql.jdbc.Driver";
      String url = "jdbc:mysql://localhost/dbase";
      String uid = "root";
      String psw = "root";
      Connection con=null;
      PreparedStatement ps = null;
      ResultSet rs;
      try
        {
          Class.forName(driver);
          con = DriverManager.getConnection(url,uid,psw);
          ps=con.prepareStatement("Select CONCAT('My','S','ql')");
          rs = ps.executeQuery();
              String title = "Using Concat Function";
          String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
              disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
          "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
              "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value Is </th>\n"+ "</body> </html>");
          while(rs.next())
           {
             String val = rs.getString(1);
                 disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
           }     
            }         
                 catch(Exception e) 
                        {
                           e.printStackTrace();
                        }
                           disp.close();
    }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                 doGet(rq,rp);
                           }
 }
 
WEB.XML
 
<servlet>
   <servlet-name>MySqlConcatFunc</servlet-name>
   <servlet-class>MySqlConcatFunc</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlConcatFunc</servlet-name>
   <url-pattern>/MySqlConcatFunc</url-pattern>
</servlet-mapping>

MySql Concatenation Java Servlet

By Dinesh Thakur

The Function ‘CONCATENATION’ will add Two String with each other.

In the first instance i make a table named ‘org’ with required Fields and values into the database named ‘dbase’ within the reference of mySql(php myAdmin). Then i import all the required java packages from java library. Then i make a class named ‘MySqlConcatenationJavaServlet’ which extends the ‘HttpServlet’. I use serviceMethod()

which will bring the request from doGet()Method for output on the web browser. After that i loaded all the required drivers for database accessing. Here i declare some mandatory variables like i declare Connection (this variable will create a link between database and the java code).then i use ‘resultset’ that will bring the value from the required columns and rows. i declare ‘preparedStatement’ that will responsible for the executing selected query like executeQuery() as (SELECT last_name||job_id as UC FROM org). Then i use doGet()Method for getting output on the web Browser.

To Getting Output on web browser in a tabular form i use ‘HTML’ code and tags.

  
EXAMPLE
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlConcatenationJavaServlet extends HttpServlet
 {
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
    {
      rp.setContentType("text/html");
      PrintWriter disp = rp.getWriter();
      String driver = "com.mysql.jdbc.Driver";
      String url = "jdbc:mysql://localhost/dbase";
      String uid = "root";
      String psw = "root";
      Connection con=null;
      PreparedStatement ps = null;
      ResultSet rs;
      try
       {
         Class.forName(driver);
         con = DriverManager.getConnection(url,uid,psw);
         ps=con.prepareStatement("SELECT last_name||job_id as UC FROM org;");
         rs = ps.executeQuery();
             String title = "Employee's Info With Using Of Concatenation";
         String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
             disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
         "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
             "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Name</th><th>Job ID</th>\n"+ "</body> </html>");
         while(rs.next())
           {
             String l_name = rs.getString("UC");
                 String  j_id = rs.getString("UC");
             disp.println("<tr><td align=\"center\">" + l_name +  "<td align=\"center\">" + j_id +"</td></tr>" );  
           }     
            }         
                 catch(Exception e) 
                   {
                         e.printStackTrace();
                   }
                         disp.close();
      }
                     public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                 doGet(rq,rp);
                           }
 }
 
WEB.XML
 
<servlet>
   <servlet-name>MySqlConcatenationJavaServlet</servlet-name>
   <servlet-class>MySqlConcatenationJavaServlet</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlConcatenationJavaServlet</servlet-name>
   <url-pattern>/MySqlConcatenationJavaServlet</url-pattern>
</servlet-mapping>

MySql Comparison Operator Java Servlet

By Dinesh Thakur

This function ‘Comparison Operator’ will check the comparison between two expressions.

First i made a table named ‘sal’ with required fields and values into a database named ‘dbase’ within the reference of mySql(php myAdmin).then i import all the java packages from the java library. I made a class named ‘MySqlComparisonOperatorJavaServlet’ extends the ‘HttpServlet’. I use serviceMethod() which is use to getting the request from the doGet()method for output on a web server. Then i loaded all the required drivers for database accessing. I use variables that will be used to database connectivity. First i declare ‘connection’ (this variable will create a link between a database and the java code). I declare ‘resultSet'(this variable will be the responsible for getting value from required fields like columns and rows).then i declare ‘preparedStatement’ this variable will use to execute the selected query like executeQuery() as (SELECT last_name, salary FROM sal WHERE salary <= 8000″). Now i use the doGet()Method which will bring the output on web browser.

To getting output in a tabular form on a web browser i use ‘HTML’ code and tags that will bring output in a manner way.

   
EXAMPLE
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlComparisonOperatorJavaServlet extends HttpServlet
 {
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
   {
     rp.setContentType("text/html");
     PrintWriter disp = rp.getWriter();
     String driver = "com.mysql.jdbc.Driver";
     String url = "jdbc:mysql://localhost/dbase";
     String uid = "root";
     String psw = "root";
     Connection con=null;
     PreparedStatement ps = null;
     ResultSet rs;
     try
       {
         Class.forName(driver);
         con = DriverManager.getConnection(url,uid,psw);
         ps=con.prepareStatement("SELECT last_name, salary FROM sal WHERE salary <= 8000");
         rs = ps.executeQuery();
             String title = "Employee's Info With Comparison Condition";
         String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
             disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
         "<body bgcolor=\"#f4efef\">\n" + "<h3 align=\"center\">" + title + "</h3>\n" + "<ul>\n" +
             "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Last Name</th><th>Salary</th>\n"+ "</body> </html>");
         while(rs.next())
           {
             String l_name = rs.getString("last_name");
                 int sal = rs.getInt("salary");
                 disp.println("<tr><td align=\"center\">"+ l_name +          "<td align=\"center\">"+ sal +"</td></tr>" );   
           }     
       }  
                 catch(Exception e) 
                    {
                           e.printStackTrace();
                        }
                           disp.close();
     }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                 doGet(rq,rp);
                           }
 }
 
WEB.XML
 
<servlet>
   <servlet-name>MySqlComparisonOperatorJavaServlet</servlet-name>
   <servlet-class>MySqlComparisonOperatorJavaServlet</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlComparisonOperatorJavaServlet</servlet-name>
   <url-pattern>/MySqlComparisonOperatorJavaServlet</url-pattern>
</servlet-mapping>

MySql CHAR LENGTH Function

By Dinesh Thakur

The Function ‘CHAR_LENGTH’ will provide the numbers of character.

I make a table in database named ‘dbase’ within the reference of mySql(php myAdmin).then i import all the required java packages from java library.then i made a class named ‘MySqlCHARLENGTHFunc’ which extends ‘HttpServlet’. After i loaded the all required drivers for accessing the database. Then i declare mandatory variables which will be used to connect with database completely. I first declare ‘connection’ this variable will be responsible for making a bridge between database and java code. Then i declare ‘resultSet’ that will use to bring the values from required columns and rows. I declare ‘preparedStatement’ that variable will be used to executing the selected query like executeQuery() as (Select CHAR_LENGTH(‘mysql’)). Then i used doGet()method that will get the output on the web browser.

Now i used some kind of ‘HTML’ code and tags that will present the output in a tabular form on a web browser.

  
EXAMPLE
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlCHARLENGTHFunc extends HttpServlet
 {
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
   {
     rp.setContentType("text/html");
     PrintWriter disp = rp.getWriter();
     String driver = "com.mysql.jdbc.Driver";
     String url = "jdbc:mysql://localhost/dbase";
     String uid = "root";
     String psw = "root";
     Connection con=null;
     PreparedStatement ps = null;
     ResultSet rs;
     try
      {
        Class.forName(driver);
        con = DriverManager.getConnection(url,uid,psw);
        ps=con.prepareStatement("Select CHAR_LENGTH('mysql')");
        rs = ps.executeQuery();
            String title = "Using CHAR LENGTH Function";
        String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
            disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
        "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
            "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value Is </th>\n"+ "</body> </html>");
        while(rs.next())
           {
             String val = rs.getString(1);
                 disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
           }     
       }  
                 catch(Exception e) 
                   {
                         e.printStackTrace();
                   }
                         disp.close();
    }
                     public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                 doGet(rq,rp);
                           }
 }
 
WEB.XML
 
<servlet>
   <servlet-name>MySqlCHARLENGTHFunc</servlet-name>
   <servlet-class>MySqlCHARLENGTHFunc</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlCHARLENGTHFunc</servlet-name>
   <url-pattern>/MySqlCHARLENGTHFunc</url-pattern>
</servlet-mapping>

MySql CHAR Function in Servlet

By Dinesh Thakur

The Function ‘CHAR’ will return the character for each Integer value.

The firstly i made a table in database named ‘dbase’ with required fields and values in it.i import all the required java packages from the java library.I made a class named ‘MySqlCHARFunc’ which extends the ‘HttpServlet’.after then i use serviceMethod() which will bring the request from doGet()method for output. Then after i loaded all required drivers for the database accessing. I declare some mandatory variable for database connecting like i declare ‘Connection’ (this variable will use to create a link between database and the java code or actual code).the next will be ‘resultSet’ the variable will be responsible for the getting values from required columns and rows. The other one variable will be ‘preparedStatement’ this variable will use to execute the Selected Query like executeQuery() as (Select CHAR(’77’,’87’,’83’,’81’,’76’)).i use doGet()Method which will bring the output on web browser.

On the other hand i use ‘HTML’ code and tags for presenting the output in a tabular form or in a manner way on the web browser.

 
EXAMPLE
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlCHARFunc extends HttpServlet
 {
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
    {
      rp.setContentType("text/html");
      PrintWriter disp = rp.getWriter();
      String driver = "com.mysql.jdbc.Driver";
      String url = "jdbc:mysql://localhost/dbase";
      String uid = "root";
      String psw = "root";
      Connection con=null;
      PreparedStatement ps = null;
      ResultSet rs;
      try
       {
         Class.forName(driver);
         con = DriverManager.getConnection(url,uid,psw);
         ps=con.prepareStatement("Select CHAR('77','87','83','81','76')");
         rs = ps.executeQuery();
             String title = "Using CHAR Function";
         String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
             disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
         "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
             "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value Is </th>\n"+ "</body> </html>");
         while(rs.next())
           {
             String val = rs.getString(1);
                 disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
           }     
            }         
                 catch(Exception e) 
                   {
                          e.printStackTrace();
                   }
                          disp.close();
     }
                      public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                 doGet(rq,rp);
                           }
 }
 
WEB.XML
 
<servlet>
   <servlet-name>MySqlCHARFunc</servlet-name>
   <servlet-class>MySqlCHARFunc</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlCHARFunc</servlet-name>
   <url-pattern>/MySqlCHARFunc</url-pattern>
</servlet-mapping>

MySql CEILING Function

By Dinesh Thakur

The ‘CEILING’ Function will return the smallest integer value that not in the passed expression.

I make a table in database named ‘dbase’ within the reference of mySql(Php MyAdmin). I import all the required java packages from java library. Then i make a class named ‘MySqlCEILINGFunction’ which extends the ‘HttpServlet’.then i use serviceMethod() which will bring request from the doGet()Method for output on the web browser. After that i loaded all required drivers for database accessing. Now time to declare some variables for database connectivity like i declare ‘Connection’ (this variable is responsible for creating link between database and java code(actual code). Now the next one will be the ‘resultSet’ (this variable will help user to get value from the required columns and rows as required. The next one will be the preparedStatement that will use to execute the query like executeQuery() as (Select CEILING(‘5.67’)). Now to move next i use doGet()Method in the end that will bring the output on the web Browser.

Now to get the output on the web browser in an impressive look i use ‘HTML’ code and some of these tags which will bring output in the tabular form on the web browser.

  
EXAMPLE
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlCEILINGFunction extends HttpServlet
 {
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
    {
      rp.setContentType("text/html");
      PrintWriter disp = rp.getWriter();
      String driver = "com.mysql.jdbc.Driver";
      String url = "jdbc:mysql://localhost/dbase";
      String uid = "root";
      String psw = "root";
      Connection con=null;
      PreparedStatement ps = null;
      ResultSet rs;
      try
        {
          Class.forName(driver);
          con = DriverManager.getConnection(url,uid,psw);
          ps=con.prepareStatement("Select CEILING('5.67')");
          rs = ps.executeQuery();
              String title = "Using CEILING Function";
          String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
              disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
          "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
              "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value</th>\n"+ "</body> </html>");
          while(rs.next())
            {
              String val = rs.getString(1);
                  disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
            }     
            }         
                      catch(Exception e) 
                               {
                                 e.printStackTrace();
                               }
                                     disp.close();
            }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                     doGet(rq,rp);
                           }
  }
WEB.XML
 
<servlet>
   <servlet-name>MySqlCEILINGFunction</servlet-name>
   <servlet-class>MySqlCEILINGFunction</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlCEILINGFunction</servlet-name>
   <url-pattern>/MySqlCEILINGFunction</url-pattern>
</servlet-mapping>

MySql CEIL Function in Servlet

By Dinesh Thakur

The ‘CEIL’ Function will return the smallest integer value that not in the passed expression.

I make a table in database named ‘dbase’ within the reference of mySql(Php MyAdmin).i import all the required java packages from java library. Then i make a class named ‘MySqlCEILFunction’ which extends the ‘HttpServlet’.then i use serviceMethod() which will bring request from the doGet()Method for output on the web browser. After that i loaded all required drivers for database accessing. Now time to declare some variables for database connectivity like i declare ‘Connection’ (this variable is responsible for creating link between database and java code(actual code). now the next one will be the ‘resultSet’ (this variable will help user to get value from the required columns and rows as required. The next one will be the preparedStatement that will use to execute the query like executeQuery() as (Select CEIL(‘5.67’)).

Now to move next i use doGet()Method in the end that will bring the output on the web Browser.

Now to get the output on the web browser in an impressive look i use ‘HTML’ code and some of these tags which will bring output in the tabular form on the web browser.

 
EXAMPLE
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlCEILFunction extends HttpServlet
 {
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
    {
      rp.setContentType("text/html");
      PrintWriter disp = rp.getWriter();
      String driver = "com.mysql.jdbc.Driver";
      String url = "jdbc:mysql://localhost/dbase";
      String uid = "root";
      String psw = "root";
      Connection con=null;
      PreparedStatement ps = null;
      ResultSet rs;
      try
       {
         Class.forName(driver);
         con = DriverManager.getConnection(url,uid,psw);
         ps=con.prepareStatement("Select CEIL('5.67')");
         rs = ps.executeQuery();
             String title = "Using CEIL Function";
         String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
             disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
         "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
             "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value</th>\n"+ "</body> </html>");
         while(rs.next())
           {
             String val = rs.getString(1);
                 disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
           }     
       }  
                 catch(Exception e) 
                        {
                          e.printStackTrace();
                        }
                          disp.close();
     }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                     doGet(rq,rp);
                           }
 }
 
WEB.XML
 
<servlet>
   <servlet-name>MySqlCEILFunction</servlet-name>
   <servlet-class>MySqlCEILFunction</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlCEILFunction</servlet-name>
   <url-pattern>/MySqlCEILFunction</url-pattern>
</servlet-mapping>

MySql BIT LENGTH Function in Servlet

By Dinesh Thakur

The ‘BIT_LENGTH’ Function will returns the arguments length.

Now i make a table into a database named ‘dbase’ within the reference of mySql(php myAdmin). I import all the required java packages from java library. Then i make a class named ‘MySqlBITLENGTHFunc’ which extends the ‘Http Servlet’. Then i use serviceMethod() which will use to get the request from doGet() method for Output on web Browser. Then i loaded all the mandatory drivers for database accessing. After that I’ll have to declare the variables like ‘connection’ (this variable will responsible for the creating a link between database and the java code (actual code). the other variable is ‘resultSet’ (that will help to fetch value from the selected columns and rows as required).the next will be ‘preparedStatement’ that will be used to execute the selected query like executeQuery() as (Select BIT_LENGTH(‘mysql’)). The last instance will be to call the doGet()Method that will bring Output on the web browser.

To get Output in a manner form i use ‘Html’ tags which will appear Output in a tabular form on the web browser.

  
EXAMPLE
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlBITLENGTHFunc extends HttpServlet
 {
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
   {
    rp.setContentType("text/html");
    PrintWriter disp = rp.getWriter();
    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost/dbase";
    String uid = "root";
    String psw = "root";
    Connection con=null;
    PreparedStatement ps = null;
    ResultSet rs;
    try
     {
       Class.forName(driver);
       con = DriverManager.getConnection(url,uid,psw);
       ps=con.prepareStatement("Select BIT_LENGTH('mysql')");
       rs = ps.executeQuery();
       String title = "Using BIT LENGTH Function";
       String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
       disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
       "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
       "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value  </th>\n"+ "</body> </html>");
       while(rs.next())
           {
             String val = rs.getString(1);
                 disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
           }     
      }   
                 catch(Exception e) 
                     {
                          e.printStackTrace();
                        }
                          disp.close();
   }
                      public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                 doGet(rq,rp);
                           }
}
 
WEB.XML
 
<servlet>
   <servlet-name>MySqlBITLENGTHFunc</servlet-name>
   <servlet-class>MySqlBITLENGTHFunc</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlBITLENGTHFunc</servlet-name>
   <url-pattern>/MySqlBITLENGTHFunc</url-pattern>
</servlet-mapping>

MySql BINARY Code Function in Servlet

By Dinesh Thakur

The ‘BIN'(Binary) Function will convert the numeric expression into a binary Expression.

The first instance i make a table into a database named ‘dbase’ within the reference of mySql(php myAdmin).then i import all the mandatory  java packages from the java library. Then i made a class named ‘MySqlBINARYCodeFunc’ which extends the ‘HttpServlet’.i use serviceMethod() which is responsible for getting request from the doGet()Method for Output on a web browser. After then i loaded all required drivers that necessary for database accessing. At the next move i declare variable those objects will create a criteria to use database i use ‘connection’ variable(which will be use to create a connection between database and the java code (actual code).the next variable will be the ‘resultSet’ (this will be responsible for getting value from the desired column and rows as required).and the next one will be the ‘preparedStatement’ that will use to executing the query like executeQuery() as (Select BIN(35)).at the last instance i use doGet()Method which will use to get output on a web Browser.

On the next step on web browser i use ‘HTML’ tags that will prefer the Output into tabular form as a manner way.  

EXAMPLE
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlBINARYCodeFunc extends HttpServlet
 {
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
   {
     rp.setContentType("text/html");
     PrintWriter disp = rp.getWriter();
     String driver = "com.mysql.jdbc.Driver";
     String url = "jdbc:mysql://localhost/dbase";
     String uid = "root";
     String psw = "root";
     Connection con=null;
     PreparedStatement ps = null;
     ResultSet rs;
     try
      {
        Class.forName(driver);
        con = DriverManager.getConnection(url,uid,psw);
        ps=con.prepareStatement("Select BIN(35)");
        rs = ps.executeQuery();
            String title = "Using BIN Function";
        String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
            disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
        "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
            "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value  </th>\n"+ "</body> </html>");
        while(rs.next())
           {
             String val = rs.getString(1);
                 disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
           }     
      }   
                 catch(Exception e) 
                        {
                          e.printStackTrace();
                        }
                          disp.close();
                                     
  }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                     doGet(rq,rp);
                           }
}
 
WEB.XML
 
<servlet>
   <servlet-name>MySqlBINARYCodeFunc</servlet-name>
   <servlet-class>MySqlBINARYCodeFunc</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlBINARYCodeFunc</servlet-name>
   <url-pattern>/MySqlBINARYCodeFunc</url-pattern>
</servlet-mapping>

MySql AVG Function in Servlet

By Dinesh Thakur

This function ‘AVG'(Average) will present the average of selected numeric column or row.

Then first i made a table named ‘worker’ with selected fields and values in it in the database named ‘dbase’ within the reference of mySql(php myAdmin).after that i import all the required java packages from java library. Then i made a class named ‘MySqlAVGFunction’ extends the ‘HttpServlet’. Then i use serviceMethod() which will getting the request from doGet()Method for Output. After i loaded all the important drivers for Database accessing. Now i declare variables which are necessary for database using like i declare Connection (this variable will use to create a link between database and java code or actual code this will bring us allotment to make a link between both procedures and then after i define resultSet variable this will be responsible for the fetching value from the required columns and rows as required by the condition or query. The next will be the prepared Statement that will responsible for the executing the query like executeQuery() as (SELECT AVG(salary) AS av FROM worker). Then after i use doGet()Method which will use to show output on the web browser as required.

On the web Browser i use ‘Html’ tags to print the output in tabular form on the web browser this will present output in a manner way. 

EXAMPLE 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlAVGFunction extends HttpServlet
 {
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
    {
      rp.setContentType("text/html");
      PrintWriter disp = rp.getWriter();
      String driver = "com.mysql.jdbc.Driver";
      String url = "jdbc:mysql://localhost/dbase";
      String uid = "root";
      String psw = "root";
      Connection con=null;
      PreparedStatement ps = null;
      ResultSet rs;
      try
       {
         Class.forName(driver);
         con = DriverManager.getConnection(url,uid,psw);
         ps=con.prepareStatement("SELECT AVG(salary) AS av FROM worker");
         rs = ps.executeQuery();
             String title = "Using AVG(Average) Function";
         String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
             disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
         "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
             "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Total</th>\n"+ "</body> </html>");
         while(rs.next())
           {
             String avg = rs.getString("av");
                 disp.println("<tr><td align=\"center\">" + avg +"</td></tr>" );  
           }     
       }  
                 catch(Exception e) 
                   {
                          e.printStackTrace();
                   }
                          disp.close();
      }
                      public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                 doGet(rq,rp);
                           }
 }
 
WEB.XML
 
<servlet>
   <servlet-name>MySqlAVGFunction</servlet-name>
   <servlet-class>MySqlAVGFunction</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlAVGFunction</servlet-name>
   <url-pattern>/MySqlAVGFunction</url-pattern>
</servlet-mapping>

MySql ATAN Function in Servlet

By Dinesh Thakur

The ‘ATAN’ function will return the arctangent value of a numeric expression.

First i make a table in database named ‘dbase’ with required fields and values in it and within the reference of mySql(Php Admin).then i import all the required java package from java library. I made a class named ‘MySqlATANFunction’ extends the ‘HttpServlet’. Then i use serviceMethod() which will bring the request from doGet()Method for Output. Then i load all the drivers for database accessing. after then i declare variables which are necessary for database conditions like i declare connection(this variable will use to make a link between database and java code or actual code).after then i declare resultset (this variable will use to get the values from desired columns and rows as required from query). The next one will be the preparedStatement (this variable will responsible for the executing query like executeQuery() as (Select ATAN(‘1’)).in the last i used the doGet()Method which will bring the output on the web Browser.

On the web Browser for getting output in the manner way i used ‘HTML’ tags which will present the output in a tabular form on a web browser.

 
EXAMPLE
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlATANFunction extends HttpServlet
 {
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
    {
      rp.setContentType("text/html");
      PrintWriter disp = rp.getWriter();
      String driver = "com.mysql.jdbc.Driver";
      String url = "jdbc:mysql://localhost/dbase";
      String uid = "root";
      String psw = "root";
      Connection con=null;
      PreparedStatement ps = null;
      ResultSet rs;
      try
       {
         Class.forName(driver);
         con = DriverManager.getConnection(url,uid,psw);
         ps=con.prepareStatement("Select ATAN('1')");
         rs = ps.executeQuery();
             String title = "Using ATAN Function";
         String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
             disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
         "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
             "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value</th>\n"+ "</body> </html>");
         while(rs.next())
           {
             String val = rs.getString(1);
                 disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
           }     
       }  
                 catch(Exception e) 
                        {
                          e.printStackTrace();
                        }
                          disp.close();
     }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                  doGet(rq,rp);
                           }
}
 
WEB.XML
 
<servlet>
   <servlet-name>MySqlATANFunction</servlet-name>
   <servlet-class>MySqlATANFunction</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlATANFunction</servlet-name>
   <url-pattern>/MySqlATANFunction</url-pattern>
</servlet-mapping>

MySql ASCII Code in Servlet Example

By Dinesh Thakur

The function ‘ASCII'(American standard code for interface and interchange) use to get the numeric value of left most character of a string.

First i made a table in database named ‘dbase’ with in the reference of mySql(Php myAdmin).I import all the required java packages from java library. After then i declare a class named ‘MySqlASCIICode’ which extends the ‘HttpServlet’. After i use serviceMethod() responsible for the getting request from doGet() method. Then i load all required drivers for database accessing. Then i declare some variables like connection (this will create a bridge between the database and the java code or in other words Frontend and Backend with the help of loaded drivers. The other variable is to declare will be resultset it will use to get the value from required columns and rows into on to the database. The next one will be the preparedStatement the object of the variable is ps. this will be responsible for executing the selected query like executeQuery() as (Select ASCII(‘mysql’)). The next step to use doGet() method which will bring the output on a web browser.

To make output manner able i use ‘HTML’ tags to get Output in tabular form on a web browser.   

EXAMPLE 
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlASCIICode extends HttpServlet
 {
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
    {
      rp.setContentType("text/html");
      PrintWriter disp = rp.getWriter();
      String driver = "com.mysql.jdbc.Driver";
      String url = "jdbc:mysql://localhost/dbase";
      String uid = "root";
      String psw = "root";
      Connection con=null;
      PreparedStatement ps = null;
      ResultSet rs;
      try
       {
         Class.forName(driver);
         con = DriverManager.getConnection(url,uid,psw);
         ps=con.prepareStatement("Select ASCII('mysql')");
         rs = ps.executeQuery();
             String title = "Using ASCII Function";
         String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
             disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
         "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
             "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value  </th>\n"+ "</body> </html>");
         while(rs.next())
           {
             String val = rs.getString(1);
                 disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
           }     
       }  
                 catch(Exception e) 
                   {
                          e.printStackTrace();
                   }
                          disp.close();
     }
                      public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                 doGet(rq,rp);
                           }
}
 
WEB.XML
 
<servlet>
   <servlet-name>MySqlASCIICode</servlet-name>
   <servlet-class>MySqlASCIICode</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlASCIICode</servlet-name>
   <url-pattern>/MySqlASCIICode</url-pattern>
</servlet-mapping>

MySql Arithmetic Operator in Servlet

By Dinesh Thakur

The ‘Arithmetic Operator’ does the mathematical computation in MySql.

 

First i make a table in database named ‘dbase’. All the fields in table must be filled with required values. After then i import all the required java packages from java library. Then i made a class named ‘MySqlArithmeticOperator’ extends the’HttpServlet’. Then i use serviceMethod() which is responsible for getting request from doGet() method for Output. After then i load all the required driver for database connection. Then i declare mandatory variables as required like connection (this variable will create a link between database and actual code(java code)).After declaring connection i declare resultSet variable this variable will get the value from the required columns and values within it’s own object rs. The other variable will be prepared Statement this variable will be responsible for executing the Selected Query like executeQuery() as (SELECT name, salary,salary + 300 cal FROM worker).then i use doGet() method for getting output on the web browser.

to getting output in tabular form on web browser i used ‘HTML’ code and the tags for showing output in manner form also.  

 
EXAMPLE
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlArithmeticOperator extends HttpServlet
 {
     public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
      {
        rp.setContentType("text/html");
        PrintWriter disp = rp.getWriter();
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost/dbase";
        String uid = "root";
        String psw = "root";
        Connection con=null;
        PreparedStatement ps = null;
        ResultSet rs;
        try
          {
            Class.forName(driver);
            con = DriverManager.getConnection(url,uid,psw);
            ps=con.prepareStatement("SELECT name, salary,salary + 300 cal FROM worker");
            rs = ps.executeQuery();
                String title = "Employee's Info And Salary With Using Of Arithmetic Operator ";
            String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
            "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Name</th><th>Salary</th><th>Calculation</th>\n"+ "</body> </html>");
            while(rs.next())
             {
               String nm = rs.getString("name");
                   int sal = rs.getInt("salary");
                   int c=rs.getInt("cal");
               disp.println("<tr><td align=\"center\">" + nm +  "<td align=\"center\">" + sal +"<td align=\"center\">" + c +"</td></tr>" );  
             }     
              }       
                   catch(Exception e) 
                         {
                            e.printStackTrace();
                         }
                            disp.close();
       }
                        public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                 doGet(rq,rp);
                           }
}
 
WEB.XML
 
<servlet>
   <servlet-name>MySqlArithmeticOperator</servlet-name>
   <servlet-class>MySqlArithmeticOperator</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlArithmeticOperator</servlet-name>
   <url-pattern>/MySqlArithmeticOperator</url-pattern>
</servlet-mapping>

MySql And Operater in Servlet

By Dinesh Thakur

The function in MySql ‘AND’ Operator known as Conditional Operator which will use to full fill the Desired condition.

First we need to make a table with required fields into a database named ‘dbase’ within the reference of MySql(Php MyAdmin).first we need to import all the require packages from the java library. then we will make a class named ‘MySqlAndOperater’ which extends ‘HttpServlet’.After then we define serviceMethod() which will bring the request from doGet()method for Output. Then we need to declare all the required Drivers for Database linking. Next move will be to declare mandatory variables for accessing the database like Connection variable this variable will make a bridge or link between database and actual code within the help of drivers.the next variable will be the resultSet this will be use to fetch value from the required columns and rows.and the preparedStatement will does the job as it will use to execute the Query like executeQuery() as (SELECT emp_id, last_name,salary,job_id FROM org WHERE salary >=10000 AND job_id LIKE ‘%man%’).and in the end the whole criteria goes to doGet()Method which will help to getting output on the web browser.

On to the web browser we need to use ‘HTML’ code and its tags to get Output in Tabular form or in a manner way.

 
EXAMPLE
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlAndOperater extends HttpServlet
 {
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
   {
     rp.setContentType("text/html");
     PrintWriter disp = rp.getWriter();
     String driver = "com.mysql.jdbc.Driver";
     String url = "jdbc:mysql://localhost/dbase";
     String uid = "root";
     String psw = "root";
     Connection con=null;
     PreparedStatement ps = null;
     ResultSet rs;
     try
      {
        Class.forName(driver);
        con = DriverManager.getConnection(url,uid,psw);
        ps=con.prepareStatement("SELECT emp_id, last_name,salary,job_id FROM org WHERE salary >=10000 AND job_id LIKE '%man%'");
        rs = ps.executeQuery();
            String title = "Employee's Info With Using of AND Query";
        String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
            disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
        "<body bgcolor=\"#f4efef\">\n" + "<h3 align=\"center\">" + title + "</h3>\n" + "<ul>\n" +
            "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Employee Id</th><th>Last Name</th><th>Salary</th><th>Job Id</th>\n"+ "</body> </html>");
        while(rs.next())
           {
             int e_id = rs.getInt("emp_id");
                 String l_name = rs.getString("last_name");
                 int sal = rs.getInt("salary");
                 String j_id=rs.getString("job_id");
                 disp.println("<tr><td align=\"center\">" + e_id +  "<td align=\"center\">" + l_name +"<td align=\"center\">" + sal +"<td align=\"center\">" + j_id +"</td></tr>" );        
           }     
       }  
                 catch(Exception e) 
                       {
                          e.printStackTrace();
                        }
                          disp.close();
   }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                     doGet(rq,rp);
                           }
}
 
WEB.XML
 
<servlet>
   <servlet-name>MySqlAndOperater</servlet-name>
   <servlet-class>MySqlAndOperater</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlAndOperater</servlet-name>
   <url-pattern>/MySqlAndOperater</url-pattern>
</servlet-mapping>

MySql Alias Servlet Example

By Dinesh Thakur

This function use to make a Alias name of any column to use as a reference in mySql.

First i import all the required packages from java Library. Then i made a class named ‘MySqlAliasServlet’ extends the ‘HttpServlet’.Then i use serviceMethod() which will getting the request from doGet()Method for Output. Then load all the required Drivers. After then i declare some required variables for accessing database. Like Connection this variable use to use to make a link between the database and the actual code its will make a bridge between both procedures with i can access the database. With this i also declare the resultSet variable that will help user to fetch the value from required columns and rows with its object. The other one variable i declare is preparedStatement this will use to execute the Query like executeQuery() as (SELECT last_name, salary, salary*12 annum FROM worker).and in the last i call the doGet()Method which will bring the Output on the Web Browser.

While show Output on a Web Browser i use ‘HTML’ Code and some of these tags to get output in the tabular form it makes the output a manner  form.

  
EXAMPLE
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlAliasServlet extends HttpServlet
 {
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
    {
      rp.setContentType("text/html");
      PrintWriter disp = rp.getWriter();
      String driver = "com.mysql.jdbc.Driver";
      String url = "jdbc:mysql://localhost/dbase";
      String uid = "root";
      String psw = "root";
      Connection con=null;
      PreparedStatement ps = null;
      ResultSet rs;
      try
        {
          Class.forName(driver);
          con = DriverManager.getConnection(url,uid,psw);
          ps=con.prepareStatement("SELECT last_name, salary, salary*12 annum FROM worker");
          rs = ps.executeQuery();
              String title = "Annual Salary ";
          String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
              disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
          "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
              "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Last Name</th><th>Salary</th><th>Annual Salary</th>\n"+ "</body> </html>");
          while(rs.next())
            {
              String nm = rs.getString("last_name");
                  int sal = rs.getInt("salary");
                  int c=rs.getInt("annum");
              disp.println("<tr><td align=\"center\">" + nm +  "<td align=\"center\">" + sal +"<td align=\"center\">" + c +"</td></tr>" );  
            }     
             }        
                  catch(Exception e) 
                        {
                          e.printStackTrace();
                        }
                          disp.close();
      }
                      public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                        {
                               doGet(rq,rp);
                        }
  }
 
WEB.XML
 
<servlet>
   <servlet-name>MySqlAliasServlet</servlet-name>
   <servlet-class>MySqlAliasServlet</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlAliasServlet</servlet-name>
   <url-pattern>/MySqlAliasServlet</url-pattern>
</servlet-mapping>

MySql AddTime Function in Java Servlet

By Dinesh Thakur

This function use to Add Time With Particular Interval in the table or required space.

First i made a table with required fields and values in that into a database named ‘dbase’ with the reference of mySql(php Myadmin).Then i import all the required packages from java library. Then i made a class named ‘MySqlAddTime’ extends the ‘HttpServlet’.Then i use serviceMethod() which will use to getting request from the doGet()Method for Output. Then load all the required Drivers. Then i declare some mandatory variables like connection (Connection variable is been used to make a bridge or link between database and actual code(java code)).as after creating link between both platforms i declare resultSet which will be used to fetch the values from required fields according to user.the other one variable is preparedStatement the object of this variable is ‘ps’ this will use to execute the query like executeQuery() as(SELECT ADDTIME(‘2014-05-30 22:46:38.997546’, ‘1 1:1:1.000002’)). to in the end i use doGet()Method for getting value on the web Browser.

For getting value in a manner way i use ‘HTML’ code tags which will present the Output on the Web Browser in a tabular form.  

EXAMPLE 
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlAddTime extends HttpServlet
 {
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
    {
      rp.setContentType("text/html");
      PrintWriter disp = rp.getWriter();
      String driver = "com.mysql.jdbc.Driver";
      String url = "jdbc:mysql://localhost/dbase";
      String uid = "root";
      String psw = "root";
      Connection con=null;
      PreparedStatement ps = null;
      ResultSet rs;
      try
       {
         Class.forName(driver);
         con = DriverManager.getConnection(url,uid,psw);
         ps=con.prepareStatement("SELECT ADDTIME('2014-05-30 22:46:38.997546', '1 1:1:1.000002')");
         rs = ps.executeQuery();
             String title = "To Add Time With Perticular Interval";
         String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
             disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
         "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
             "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Required Time  </th>\n"+ "</body> </html>");
         while(rs.next())
           {
             String curr = rs.getString(1);
                 disp.println("<tr><td align=\"center\">" + curr +"</td></tr>" );  
           }     
       }  
                 catch(Exception e) 
                   {
                         e.printStackTrace();
                   }
                         disp.close();
    }
                     public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                       {
                             doGet(rq,rp);
                       }
 }
 
WEB.XML
 
<servlet>
   <servlet-name>MySqlAddTime</servlet-name>
   <servlet-class>MySqlAddTime</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlAddTime</servlet-name>
   <url-pattern>/MySqlAddTime</url-pattern>
</servlet-mapping>

MySql AddDate Function in Java Servlet

By Dinesh Thakur

First we make a table in database named ‘dbase’ with the reference of mySql (php myAdmin).After then we import all required packages from the Java Library. We make a class named ‘MySqlAddDate’ extends the ‘HttpServlet’. Then after we use service()Method which is use to getting request form doGet() method for output. Then load all the required Drivers. The next step to declare the variables like connection(this variable will use to create a link between database and actual code(java code).while linking the database with code we need resultSet variable which will be the responsible for fetching the value from the desired columns and rows. The other variable will be the preparedStatement which will use to execute the query like executeQuery() as (Select DATE_ADD(NOW(),INTERVAL 45 DAY)).and after using all the variables we use doGet()Method for show output on a web browser.

While getting Output on the Web Browser we use ‘HTML’ Code or some of these tags to present Output in tabular form.

 
EXAMPLE
 
 import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlAddDate extends HttpServlet
 {
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
    {
      rp.setContentType("text/html");
      PrintWriter disp = rp.getWriter();
      String driver = "com.mysql.jdbc.Driver";
      String url = "jdbc:mysql://localhost/dbase";
      String uid = "root";
      String psw = "root";
      Connection con=null;
      PreparedStatement ps = null;
      ResultSet rs;
      try
       {
         Class.forName(driver);
         con = DriverManager.getConnection(url,uid,psw);
         ps=con.prepareStatement("Select DATE_ADD(NOW(),INTERVAL 45 DAY) AS dt");
         rs = ps.executeQuery();
             String title = "To Add Date With Perticular Interval";
         String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
             disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
         "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
             "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Added Date  </th>\n"+ "</body> </html>");
         while(rs.next())
           {
             String curr = rs.getString(1);
                 disp.println("<tr><td align=\"center\">" + curr +"</td></tr>" );  
           }     
       }  
                 catch(Exception e) 
                  {
                        e.printStackTrace();
                  }
                        disp.close();
     }
                    public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                     doGet(rq,rp);
                           }
}
 
web.XML
 
<servlet>
   <servlet-name>MySqlAddDate</servlet-name>
   <servlet-class>MySqlAddDate</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlAddDate</servlet-name>
   <url-pattern>/MySqlAddDate</url-pattern>
</servlet-mapping>

MySql ACOS Function in Java Servlet

By Dinesh Thakur

The function ‘ACOS’ returns the cosine of numeric expression.

First we make a table in database named ‘dbase’ with required fields with in the reference of mySql(php myAdmin).Next step will be to import the required packages from the java library. After then we make a class named ‘MySqlACOSFunction’ which is extends the ‘HttpServlet’.And then we use serviceMethod() which will access the granting for getting request from doGet()Method. Then load all the required Drivers.After that we declare some mandatory variable to access control on the program like Connection (The Connection variable will be used to create a link between the database and the actual code(backend and the frontend).the other variable is resultSet this variable is responsible for the fetching value from required columns and rows. and the last variable known as preparedStatement is used to execute the query like executeQuery() as (Select ACOS(‘1’)).and to present the output we use doGet()Method which provide the output on the Web Browser.

To make output efficient we use ‘HTML’ Code or some of it’s tag which will bring the output in tabular form or in a manner way on the web Browser.

 
EXAMPLE
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlACOSFunction extends HttpServlet
 {
    public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
     {
       rp.setContentType("text/html");
       PrintWriter disp = rp.getWriter();
       String driver = "com.mysql.jdbc.Driver";
       String url = "jdbc:mysql://localhost/dbase";
       String uid = "root";
       String psw = "root";
       Connection con=null;
       PreparedStatement ps = null;
       ResultSet rs;
       try
        {
          Class.forName(driver);
          con = DriverManager.getConnection(url,uid,psw);
          ps=con.prepareStatement("Select ACOS('1')");
          rs = ps.executeQuery();
              String title = "Using ACOS Function";
          String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
              disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
          "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
              "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value</th>\n"+ "</body> </html>");
          while(rs.next())
             {
               String val = rs.getString(1);
                   disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
             }     
            }         
                   catch(Exception e) 
                         {
                           e.printStackTrace();
                         }
                           disp.close();
      }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                     doGet(rq,rp);
                           }
 }
 
Web.XML
 
<servlet>
   <servlet-name>MySqlACOSFunction</servlet-name>
   <servlet-class>MySqlACOSFunction</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlACOSFunction</servlet-name>
   <url-pattern>/MySqlACOSFunction</url-pattern>
</servlet-mapping>

Java Servlet MySql ABSOLUTE Function

By Dinesh Thakur

The Function ‘ABS(Absolute)’ will show the Absolute value of a content or Number.

First i import all the required java packages from java library. Then after i made a class named ‘MySqlABSOLUTEFunction’ extends the ‘HttpServlet’.i use the serviceMethod() which is responsible for the getting request from doGet() method. Then after need to load all the drivers as required. And to declare the mandatory variables like Connection (the variable Connection will use to establish the link between database and java code(actual code).ResultSet the variable Resultset does the job to fetch the value from required columns and rows.preparedStatement this variable use to execute the query like executeQuery() as (Select ABS(‘-39’)) and after then i use doGet() method to get the output on the web Browser.

To get the Output in an impressive look, i use some tags of ‘HTML’ Code which will bring the Output in tabular form on a web browser. 

EXAMPLE 
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySqlABSOLUTEFunction extends HttpServlet
 {
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
   {
      rp.setContentType("text/html");
      PrintWriter disp = rp.getWriter();
      String driver = "com.mysql.jdbc.Driver";
      String url = "jdbc:mysql://localhost/dbase";
      String uid = "root";
      String psw = "root";
      Connection con=null;
      PreparedStatement ps = null;
      ResultSet rs;
      try
       {
         Class.forName(driver);
         con = DriverManager.getConnection(url,uid,psw);
         ps=con.prepareStatement("Select ABS('-39')");
         rs = ps.executeQuery();
             String title = "Using ABS(Absolute) Function";
         String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
             disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
         "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
             "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Value</th>\n"+ "</body> </html>");
         while(rs.next())
           {
             String val = rs.getString(1);
             disp.println("<tr><td align=\"center\">" + val +"</td></tr>" );  
           }     
       }  
                 catch(Exception e) 
                        {
                          e.printStackTrace();
                        }
                          disp.close();
     }
                      public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                     doGet(rq,rp);
                           }
}
web. XML
<servlet>
   <servlet-name>MySqlABSOLUTEFunction</servlet-name>
   <servlet-class>MySqlABSOLUTEFunction</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>MySqlABSOLUTEFunction</servlet-name>
   <url-pattern>/MySqlABSOLUTEFunction</url-pattern>
</servlet-mapping>

Java Servlet UCASE Query

By Dinesh Thakur

The function ‘UCASE’ (Upper case) will convert the string or content (Column value’s) into Upper Case from Lower case.

For this we make a table named ‘worker’ into a database named ‘dbase’ and required columns are filled with values within the reference of mysql(Php myAdmin).after then we import all the required java packages from java Library. After we make a class named ‘JavaServletUCASE’ which is been extends the “HttpServlet’. then we use serviceMethod() bring request from doGet() method. After that we need to load all the required drivers simultaneously. The variables which are required to make Program Complete have to be declared like Connection variable this will establish the link between database and actual code also known as frontEnd and backEnd concept. The other one is resultSet this variable is responsible for the getting the values from required destination (Columns and rows). The next one is preparedStatement that will responsible for execute the given query like executeQuery() as (Select name,UCASE(name) as UC from worker). And in the last Instance we need to use doGet() method which will bring the output on a web browser.

On the web browser for making Output a manner able look we will use ‘HTML’ code and tags to bring the Output in tabular form as required.  

 EXMPLE 
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JavaServletUCASE extends HttpServlet
 {
   public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
   {
     rp.setContentType("text/html");
     PrintWriter disp = rp.getWriter();
     String driver = "com.mysql.jdbc.Driver";
     String url = "jdbc:mysql://localhost/dbase";
     String uid = "root";
     String psw = "root";
     Connection con=null;
     PreparedStatement ps = null;
     ResultSet rs;
     try
       {
        Class.forName(driver);
        con = DriverManager.getConnection(url,uid,psw);
        ps=con.prepareStatement("Select name,UCASE(name) as UC from worker");
        rs = ps.executeQuery();
                  String title = "Employee's Name in Upper Case";
        String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
              disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
        "<body bgcolor=\"#f4efef\">\n" + "<h3 align=\"center\">" + title + "</h3>\n" + "<ul>\n" +
                            "<table width=\"20%\" border=\"1\" align=\"center\">\n" + "</body> </html>");
        while(rs.next())
            {
             String nm = rs.getString("UC");
                       disp.println("<tr><td align=\"center\">"+ nm +          "</td></tr>" );   
            }     
                 }    
                       catch(Exception e) 
                                {
                                  e.printStackTrace();
                                }
                                        disp.close();
            }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                            {
                                        doGet(rq,rp);
                            }
 }
 
web.XML
 
<servlet>
   <servlet-name>JavaServletUCASE </servlet-name>
   <servlet-class>JavaServletUCASE </servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>JavaServletUCASE</servlet-name>
   <url-pattern>/JavaServletUCASE </url-pattern>
</servlet-mapping>

Java Servlet Sort DESC Order

By Dinesh Thakur

This function will sort the selected column into descending order.

First we make a table named ‘org’ into a database named ‘dbase’ within the reference of mySql(php myAdmin).Now we need to import all the java packages required by user. After calling all the packages we make a class named ‘JavaServletSortDESC’ extends the ‘HttpServlet’.now to use serviceMethod() which will getting the request from doGet() for output. After then load all the required Drivers. Some mandatory variable also to be define like resultset this variable will responsible for getting the value from desired Columns and rows. Connection variable which will establish the link between the database and actual code(frontEnd and backEnd).and the PreparedStatement this variable does execute the given query like executeQuery() as (SELECT last_name, job_id, dept_id, hire_date FROM org ORDER BY hire_date DESC).and in the end the doGet() method has to be used while getting the Output on the web Browser.

On the web browser the output seems to be normal But to get the Output in a designer or efficient look we use ‘HTML’ code or Tags for getting output in tabular form on a web browser.    

  
EXAMPLE
 
JavaServletSortDESC
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JavaServletSortDESC extends HttpServlet
     {
         public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
           {
               rp.setContentType("text/html");
               PrintWriter disp = rp.getWriter();
              String driver = "com.mysql.jdbc.Driver";
              String url = "jdbc:mysql://localhost/dbase";
              String uid = "root";
              String psw = "root";
              Connection con=null;
              PreparedStatement ps = null;
              ResultSet rs;
              try
                 {
                     Class.forName(driver);
                     con = DriverManager.getConnection(url,uid,psw);
                     ps=con.prepareStatement("SELECT last_name, job_id, dept_id, hire_date FROM org ORDER BY hire_date DESC");
                     rs = ps.executeQuery();
                              String title = "Employee's Data In Descending Order";
                    String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                              disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                    "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                              "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Last Name</th><th>Job Id</th><th>Department ID</th><th>Hire Date</th>\n"+ "</body> </html>");
                   while(rs.next())
                             {
                                 String l_name = rs.getString("last_name");
                                            int j_id = rs.getInt("job_id");
                                            int d_id = rs.getInt("dept_id");
                                                      int dt = rs.getInt("hire_date");
                                  disp.println("<tr><td align=\"center\">"+ l_name +"<td align=\"center\">" + j_id +  "<td align=\"center\">" + d_id + 
                                  "<td align=\"center\">" + dt + "</td></tr>" );  
                           }      
                        }         
                                          catch(Exception e) 
                                                      {
                                                          e.printStackTrace();
                                                      }
                                                                 disp.close();
                                      }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                     doGet(rq,rp);
                           }
  }
web.XML
<servlet>
   <servlet-name>JavaServletSortDESC </servlet-name>
   <servlet-class>JavaServletSortDESC </servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>JavaServletSortDESC</servlet-name>
   <url-pattern>/JavaServletSortDESC </url-pattern>
</servlet-mapping>

Java Servlet Sort ASC Order

By Dinesh Thakur

The function ‘Sort Ascending by Order’ will does the sorting order by given command(column) in ascending Order.

First we make a table named ‘org’ into a databse named ‘dbase’ within the reference of mySql(php Myadmin).Now To make it happen first we need to import all required java packages from java library. Then to make a class named ‘JavaServletSortASC’ which is been extends the ‘HttpServlet’.Now we need to declare serviceMethod() which getting the request from doGet() for Output.And after that we need to load all the drivers for mandatory Conditions.after that all the required variable must have to be declared like connection,resultset,preparedStatement. Connection variable use to establish connection between database and java code (frontEnd and BackEnd).the variable named Resultset interface provides getting methods for retrieving column values from the given rows.the variable. preparedStatement will execute the given query like executeQuery() like as ‘SELECT last_name, job_id, dept_id, hire_date FROM org ORDER BY hire_date ASC’. And now we will use the doGet() method to fetch the result on a web Browser.

On the web Browser for an efficient look we use the ‘HTML’ coding and some of these tags to present the output in tabular form.

 EXAMPLE 
JavaServletSortASC
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.util.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JavaServletSortASC extends HttpServlet
    {
         public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
         {
             rp.setContentType("text/html");
            PrintWriter disp = rp.getWriter();
            String driver = "com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://localhost/dbase";
            String uid = "root";
            String psw = "root";
           Connection con=null;
           PreparedStatement ps = null;
           ResultSet rs;
           try
            {
              Class.forName(driver);
              con = DriverManager.getConnection(url,uid,psw);
              ps=con.prepareStatement("SELECT last_name, job_id, dept_id, hire_date FROM org ORDER BY hire_date ASC");
              rs = ps.executeQuery();
                        String title = "Employee's Data In Ascending Order";
              String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                        disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
              "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                        "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Last Name</th><th>Job Id</th><th>Department ID</th><th>Hire Date</th>\n"+   "</body></html>");
             while(rs.next())
                 {
                     String l_name = rs.getString("last_name");
                               int j_id = rs.getInt("job_id");
                               int d_id = rs.getInt("dept_id");
                                         int dt = rs.getInt("hire_date");
                     disp.println("<tr><td align=\"center\">"+ l_name +"<td align=\"center\">" + j_id +  "<td align=\"center\">" + d_id + 
                     "<td align=\"center\">" + dt + "</td></tr>" );  
                }     
                   }  
                              catch(Exception e) 
                                        {
                                            e.printStackTrace();
                                       }
                                                 disp.close();
               }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                     doGet(rq,rp);
                           }
  }
 
web.XML
 
<servlet>
   <servlet-name>JavaServletSortASC</servlet-name>
   <servlet-class>JavaServletSortASC</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>JavaServletSortASC</servlet-name>
   <url-pattern>/JavaServletSortASC</url-pattern>
</servlet-mapping>

Java Servlet REVERSE String

By Dinesh Thakur

The query ‘Reverse’ will reverse all the selected content or String(Column).

 

In the first Instance we made a table named ‘worker’ into the database named ‘dbase’ in the reference of MySql(php MyAdmin).Then We have to import all The Required Packages From java Library. now to make a class named ‘JavaServletREVERSE’ extends ‘HttpServlet’. use the service Method() which will bring the Request from doGet() for Output generating. Load the mandatory Drivers. now to declare some mandatory variables like Resultset,Connection,preparedStatement the objects of these variable will be used to access them all like connection variable will be used to make connection between Databse and actual code(Java code).the variable named result set will represents the output resulted from a given query. Now to preparedStatement which will help to execute query like executeQuery() like as (Select name,REVERSE(name) as UC from worker). and in the last step the doGet() method will be declared to fetch the Output on Web Browser.

Now on web browser to present the output in efficient look we need to use ‘HTML’ code and some of it’s tag to present Output in a tabular form. which make Output in a designer preference to show on a web browser. 

EXAMPLE 
JavaServletREVERSE
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JavaServletREVERSE extends HttpServlet
    {
         public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
        {
             rp.setContentType("text/html");
             PrintWriter disp = rp.getWriter();
             String driver = "com.mysql.jdbc.Driver";
             String url = "jdbc:mysql://localhost/dbase";
             String uid = "root";
             String psw = "root";
             Connection con=null;
             PreparedStatement ps = null;
             ResultSet rs;
             try
              {
                 Class.forName(driver);
                 con = DriverManager.getConnection(url,uid,psw);
                 ps=con.prepareStatement("Select name,REVERSE(name) as UC from worker");
                 rs = ps.executeQuery();
                          String title = "Employee's Name in Reverse Order Case";
                String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                      disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f4efef\">\n" + "<h3 align=\"center\">" + title + "</h3>\n" + "<ul>\n" +
                                    "<table width=\"20%\" border=\"1\" align=\"center\">\n" + "</body> </html>");
                while(rs.next())
                 {
                    String nm = rs.getString("UC");
                             disp.println("<tr><td align=\"center\">"+ nm +    "</td></tr>" );   
                }      
                      }           
                            catch(Exception e) 
                                      {
                                        e.printStackTrace();
                                     }
                                             disp.close();
                        }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                     doGet(rq,rp);
                           }
  }
 
 
Web.XML
 
<servlet>
   <servlet-name>JavaServletREVERSE </servlet-name>
   <servlet-class>JavaServletREVERSE </servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>JavaServletREVERSE</servlet-name>
   <url-pattern>/JavaServletREVERSE </url-pattern>
</servlet-mapping>

Java Servlet MySql Alias Query

By Dinesh Thakur

The Query ‘Alias’ will create an alias(Subsitute) of an column name and the reference of column will use to fetch output.

As mentioned we need to make an table named ’emp’ into a database named ‘dbase’ within the reference of Mysql(php myAdmin).after that all the required java packages should have been imported in the starting of program.then after we make a class named ‘JavaServletMySqlAlias’ extends the ‘HttpServlet’. next step will be to use serviceMethod() this method help to getting request call the doGet() for Get a Request. next step will be to load driver with the object of ‘resultset’ some variable are also to be declared like Connection(this variable help to make connection between database and source code for accessing the data from database,Resultset this will does the job to present output table from given query,PreparedStatement will help to execute the selected Query like executeQuery() as (SELECT name, job FROM emp as DV).and in the last step we use doGet() Method to present output on a web browser.

Now on a web browser the Output will be shown as normal but in efficient mode if we need to present The result we will use some kind of ‘HTML’ Coding tags which convert the normal Output into a designer Look or in a tabular form.  

EXAMPLE 
JavaServletMySqlAlias
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JavaServletMySqlAlias extends HttpServlet
   {
      public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
         {
             rp.setContentType("text/html");
            PrintWriter disp = rp.getWriter();
            String driver = "com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://localhost/dbase";
            String uid = "root";
            String psw = "root";
            Connection con=null;
            PreparedStatement ps = null;
            ResultSet rs;
            try
             {
                Class.forName(driver);
                con = DriverManager.getConnection(url,uid,psw);
                ps=con.prepareStatement("SELECT name, job FROM emp as DV");
                rs = ps.executeQuery();
                         String title = "Employee's Data In Alias Mode";
               String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                     disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
               "<body bgcolor=\"#f4efef\">\n" + "<h3 align=\"center\">" + title + "</h3>\n" + "<ul>\n" +
                                   "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Name</th><th>Job</th>\n"+ "</body> </html>");
               while(rs.next())
               {
                  String nm = rs.getString("name");
                                                String jb = rs.getString("job");
                            disp.println("<tr><td align=\"center\">"+ nm +     "<td align=\"center\">" + jb +"</td></tr>" );    
              }      
                    } 
                           catch(Exception e) 
                                     { 
                                         e.printStackTrace();
                                    }
                                              disp.close();
               }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                     doGet(rq,rp);
                           }
 }
 
Web.XML
 
<servlet>
   <servlet-name>JavaServletMySqlAlias</servlet-name>
   <servlet-class>JavaServletMySqlAlias</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>JavaServletMySqlAlias</servlet-name>
   <url-pattern>/JavaServletMySqlAlias</url-pattern>
</servlet-mapping>

Java Servlet MySql LIMIT Query

By Dinesh Thakur

The Query ‘LIMIT’ will fetch the limited coloumns as required.

In the starting of Program we need to import all the Required Packages from the java library.then to declare a class named ‘JavaServletLIMITQuery’ which is been extends ‘HttpServlet’. after then we have to use serviceMethod() which is responsible for the Getting request for Output. then load drivers and create an Object of resultSet which will useful to show the table result as an Output.Connection variable also to be declared for interact between database and actual code as like in between frontEnd and backEnd.now on other hand preparedStatement will be use for executing query like executeQuery() like this (SELECT last_name, salary FROM inq LIMIT 2). and to fetch the result on web browser doGet() method will be initialize.

Now to getting Output in designer Look we need to use ‘HTML’ tags to put all the Output in Tabular form on a web Browser.  

   
EXAMPLE
 
JavaServletLIMITQuery.java
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JavaServletLIMITQuery extends HttpServlet
   {
          public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
           {
              rp.setContentType("text/html");
              PrintWriter disp = rp.getWriter();
              String driver = "com.mysql.jdbc.Driver";
             String url = "jdbc:mysql://localhost/dbase";
             String uid = "root";
             String psw = "root";
             Connection con=null;
             PreparedStatement ps = null;
             ResultSet rs;
             try
                {
                   Class.forName(driver);
                   con = DriverManager.getConnection(url,uid,psw);
                    ps=con.prepareStatement("SELECT last_name, salary FROM inq LIMIT 2");
                   rs = ps.executeQuery();
                             String title = "Employee's Info With The Refrence Of In Beetween Query ";
                   String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
                            disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
                  "<body bgcolor=\"#f4efef\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" +
                            "<table width=\"50%\" border=\"1\" align=\"center\">\n" + "<th>Last Name</th><th>Salary</th>\n"+ "</body> </html>");
                  while(rs.next())
                      {
                          String l_name = rs.getString("last_name");
                                    int sal = rs.getInt("salary");
                         disp.println("<tr><td align=\"center\">" + l_name +  "<td align=\"center\">" + sal +"</td></tr>" );  
                    }     
                       }          
                                  catch(Exception e) 
                                             {
                                                e.printStackTrace();
                                             }
                                                     disp.close();
                                      }
                                   public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                                   {
                                                 doGet(rq,rp);
                                   }
   }
 
web.XML
 
<servlet>
   <servlet-name>JavaServletLIMITQuery</servlet-name>
   <servlet-class>JavaServletLIMITQuery</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>JavaServletLIMITQuery</servlet-name>
   <url-pattern>/JavaServletLIMITQuery </url-pattern>
</servlet-mapping>

MySql SELECT LIKE Query using Java servlet

By Dinesh Thakur

This function ‘Like’ (R%) will found the desired Criteria according to Condition.

In order to select rows that match a particular character pattern we use the LIKE operator. This character matching operation is called as wildcard search. The following symbols are used for matching the pattern % (percentage). This symbol represents any sequence of zero or more characters. _ (underscore) this symbol is used for any single character search. The % and _ symbols can be used in any combination with literal characters.

Now we have to import all required java packages. then we need to declared a class named ‘JavaServletLikeQuery’ which is been extends ‘HttpServlet’. Now we need to serviceMethod() which is responsible for the getting request for desired result.then after need to load all the drivers as required procedure.Now we need to declare the variables like Connection(this variable does the role of interaction between  frontEnd and backEnd in other words it will make connection between database and actual code of java).resultset which will produce table of data from its own object.preparedStatement this variable will use to execute the Query like executeQuery().preparedStatement will execute below query (SELECT name FROM worker WHERE name LIKE ‘R%’).And after that the doGet() will have to use for fetching result on a web Browser.

Now on the browser to get an output in designer look we just use the ‘HTML’ code tags which will present the Output in Tabular form for an efficient look. 

EXAMPLE 
 
JavaServletLikeQuery.java
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JavaServletLikeQuery extends HttpServlet
 {
    public void service(HttpServletRequest rq, HttpServletResponse rp)throws IOException, ServletException
     {
       rp.setContentType("text/html");
       PrintWriter disp = rp.getWriter();
       String driver = "com.mysql.jdbc.Driver";
       String url = "jdbc:mysql://localhost/dbase";
       String uid = "root";
       String psw = "root";
       Connection con=null;
       PreparedStatement ps = null;
       ResultSet rs;
       try
        {
          Class.forName(driver);
          con = DriverManager.getConnection(url,uid,psw);
          ps=con.prepareStatement("SELECT name FROM worker WHERE name LIKE 'R%'");
          rs = ps.executeQuery();
                  String title = "Employee's Name Starts From 'R'";
          String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
              disp.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" +
          "<body bgcolor=\"#f4efef\">\n" + "<h3 align=\"center\">" + title + "</h3>\n" + "<ul>\n" +
                          "<table width=\"20%\" border=\"1\" align=\"center\">\n" + "</body> </html>");
          while(rs.next())
            {
              String nm = rs.getString("name");
                      disp.println("<tr><td align=\"center\">"+ nm +           "</td></tr>" );   
            }       
            
                }     
                  catch(Exception e) 
                              {
                                e.printStackTrace();
                              }
                                    disp.close();
                                     
    }
                       public void doPost(HttpServletRequest rq,HttpServletResponse rp)throws IOException,ServletException
                           {
                                     doGet(rq,rp);
                           }
 }

web.XML

<servlet>  
   <servlet-name>JavaServletLikeQuery</servlet-name>
   <servlet-class>JavaServletLikeQuery</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
   <servlet-name>JavaServletLikeQuery</servlet-name>
   <url-pattern>/JavaServletLikeQuery </url-pattern>
</servlet-mapping>

Steps for Using Sessions in Servlets

By Dinesh Thakur

Usually the following four steps are followed while using sessions in servlets.

1. Accessing the Session object associated with the current request: In this step, invoke the getSession () method of the HttpServletRequest to return the HttpSession object.

2. Store objects into a Session object using setAttribute () method.

3. Discard session data if necessary-Call removeAttribute () to discard a specific value. Call invalidate () to discard the entire session.

Now let us consider a program where we create a servlet that uses a session to keep track of how many times a client has visited the webpage. In addition, the servlet also displays the information related to the session such sessionID, creation time, last accessed time.

import java.io.*;

import java.util.Date;

import javax.servlet.*;

import javax.servlet.http.*; 

public class SessionSevlet extends HttpServlet

{

     public void doGet (HttpServletRequest request, HttpServletResponse

    response) throws ServletException, IOException)

     {

          response.setContentType (“text/html”);

          PrintWriter out = response.getWriter ();

          HttpSession session = request.getSession ();

          Integer count = (Integer) session.getAttribute (“count”);

          if (count == null)

          count = new Integer (l);

          else

          count =new Integer (count.intValue () +1);

          session.setAttribute (“count”, count);

          out.println (“<html> <head> <title> Session Demo </title> </head>”);

          out.println (“<body> <h2> Session Information </h2>”);

          out.println (“No. of times you have visited this page =” +count);

          out.println (“<h3> Current session details: </h3>”);

          out.println (“Session id:” +session.getId () +”<br/>”);

          out.println (“New session:” +session.isNew () +”<br/>”);

          out.println (“Timeout:” +session.getMaxInactiveInterval () +”<br/>”);

          out.println (“Creation time:” +new Date (session.getCreationTime () +”<br/>”);

          out.println (“Last access time:” +new Date (session.getLastAccessedTime ()

           +”<br/>”);

          out.println (“</body> </html>”);

       }

}                  

Explanation : In the above program, the statement

HttpSession session = request.getSession ();

returns a session associated with the user making the request. If the session does not exists it creates a new session. This is the first step required for creating and tracking HttpSession. Then the statement,

Integer count = (Integer) session.getAttribute (“count”);

returns a value associated with the session variable count. Since the getAttribute () method returns an Object type cast to it, the appropriate type before calling any methods on it. In our case, we have type cast it to Integer object because we want to display the value of count as an integer. The next statements,

if (count == null)

count = new Integer(l);

else

count = new Integer(count.intvalue () +l);

checks whether count object is null or not. It is null (i.e. there is no such object), the servlet starts a new count. Otherwise, it replaces the Integer with a new Integer whose value has been incremented by 1.

Finally, the servlet generates the html code that displays the number of times the client has visited the page along with other session information.

Advantages and Disadvantages of Cookies

By Dinesh Thakur

Cookies enable you to store the session information on the client side which has the following advantages,

• Persistence: One of the most powerful aspects of cookies is their persistence. When a cookie is set on the client’s browser, it can persist for days, months or even years. This makes it easy to save user preferences and visit information and to keep this information available every time the user returns to your site. Moreover, as cookies are stored on the client’s hard disk so if the server crashes they are still available.

• Transparent: Cookies work transparently without the user being aware that information needs to be stored.

• They lighten the load on the server’s memory.

 Disadvantages of Cookies

The following are the disadvantages of cookies :

• Sometimes clients disable cookies on their browsers in response to security or privacy worries which will cause problem for web applications that require them.

• Individual cookie can contain a very limited amount of information (not more than 4 kb).

• Cookies are limited to simple string information. They cannot store complex information.

• Cookies are easily accessible and readable if the user finds and reopens.

• Most browsers restrict the number of cookies that can be set by a single domain to not more than20 cookies (except Internet Explorer). If you attempt to set more than 20 cookies, the oldest cookies are automatically deleted.

Reading Cookies from the Client

By Dinesh Thakur

In order to read cookies that come back from the client (browser) in request header, you need to call getCookies () method of the HttpServletRequest. If the request contains no cookies this method returns null.

The following statement retrieve the cookies sent in the request header.

Cookie [] cookies = request.getCookies ();

Once you have an array of cookies, loop through the cookie array in order to retrieve information about each cookie.

The following code will retrieve the cookies sent by the client.

import.java.io.*;

import.javax.servlet.*;

import.javax.servlet.http.*;

public class RetrieveCookie extends HttpServlet

{

      Public void doGet (HttpServletRequest request, HttpServletResponse

     response) throws ServletException, IOException

      {

           PrintWriter out;

           //set content type and other response header files first

           response.setContentType (“text/html”);

         //write the data of the response

          out = response.getWriter ();

          //get cookie from HTTP request header

         Cookie [] cookies = request.getcookies ();

         out.println (“<HTML> <HEAD> <TITLE> Retrieving cookies </TITLE>

         </HEAD>”);

         out.println (“<BODY>”);         

         //check whether cookies exist

         if (cookies != null)                                             

         {

               for (int i=0; i<cookies.length; i++ )

               {

                   //display these cookies

                   out.println (“Cookie Name:” + cookies [i].getName ());

                   out.println (“Value:” +cookies [i].getValue ());

                   out.println (“<br>”);

               }

          }

          else

          {

               out.println (“No cookies exist”);

          }

          out.println (“</BODY> </HTML>”);

          out.close ();

      }

}

Output: Cookie Name: name Value: daljeet

              Cookie Name: email Value: [email protected]

                       

NOTE: When you loop through the cookie array. You might get irrelevant cookies in addition to the cookies send by you. If you want only to display the cookies send by you call getName () method on each cookie until you find one that matches with name of the cookie that you have set.

           

« Previous Page
Next Page »

Primary Sidebar

Servlet Tutorials

Servlet Tutorials

  • Servlet - Home
  • Servlet - Types
  • Servlet - Advantages
  • Servlet - Container
  • Servlet - API
  • Servlet - Chaining
  • Servlet - Life Cycle
  • Servlet - Developement Way
  • Servlet - Servlet Vs CGI
  • Servlet - Server Side
  • Servlet - HttpServletRequest
  • Servlet - Cookies Advantages
  • Servlet - JDBC Architecture
  • Servlet - HttpServlet
  • Servlet - HttpServletResponse
  • Servlet - Web Technology
  • Servlet - Applet Communication
  • Servlet - Html Communication
  • Servlet - HTTP POST Request
  • Servlet - HTTP GET Request

Other Links

  • Servlet - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW