Choose Your Language

Saturday, 29 June 2013

Dictionary application using JAVA, JSP, AJAX and MySql with source code

Project Structure:

index.jsp

<%--
    Document   : index
    Created on : 29 Jun, 2013, 11:03:21 PM
    Author     : Aravind Sankaran
--%>

<%@page import="com.dictionary.common.LoadMyProperties"%>
<%@page import="com.dictionary.bean.DictionaryBean"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
DictionaryBean bean=new DictionaryBean();
            LoadMyProperties loadMyProperties=new  LoadMyProperties();          
            bean=loadMyProperties.getMyPropertiesFile(bean);
            String typeaword=bean.getTypeaword();
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Dictionary</title>
        <script type="text/javascript">

function loadXMLDoc1()
{
var xmlhttp;

var word=document.getElementById("word").value;

var urls="getmeaning.jsp?word="+word;
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4)
    {
       
        document.getElementById("err1").innerHTML=xmlhttp.responseText;

    }
  }
xmlhttp.open("GET",urls,true);
xmlhttp.send();
}
</script>
    </head>
    <body>
        <table>
            <tr>
            <td>
          <form method="post" action="/Dictionary/Login">
          <table>
                  <tr>
                      <td></td><td></td><td><b><a href="" style="text-decoration: blink;">Login here&nbsp;</a></b></td>
                  </tr>
                  <tr>
                      <td>User Name</td><td>&nbsp;</td><td><input type="text" name="username"></td>
                  </tr>
                  <tr>
                      <td>Password</td><td>&nbsp;</td><td><input type="password" name="password"></td>
                  </tr>
                  <tr><td>&nbsp;</td>
                      <td></td><td><input type="submit" value="login" ></td>
                  </tr>
              </table>
          </form>
              </td>
              </tr>
        </table>
        <p>
            <center>
                <table width="30%" border="0">
                <tr>
                    <td>&nbsp;</td>
                    <td><b>Dictionary: </b></td>  
                    <td><b>The universe in alphabetical order</b></td>
                    <td>&nbsp;</td>   
                </tr><br><br>
                <tr>
                    <td>&nbsp;</td>
                    <td></td>  
                    <td></td>
                    <td>&nbsp;</td>   
                </tr><br><br>
                <tr>
                    <td>&nbsp;</td>
                    <td><b><%=typeaword%></b></td>  
                    <td><input type="text" name="word" id="word"  onkeyup="loadXMLDoc1()"></td>
                    <td>&nbsp;</td>
                </tr> 
                </table>
            </center><br><br>
            <span id="err1"> </span>
        </p>
       
</body>
</html>

getmeaning.jsp

<%--
    Document   : getmeaning
    Created on : 29 Jun, 2013, 11:26:25 PM
    Author     : Aravind Sankaran
--%>

<%@page import="com.dictionary.common.LoadMyProperties"%>
<%@page import="com.dictionary.bean.DictionaryBean"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>

<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%String word=request.getParameter("word");%>

<%! static Connection con;%>

<%
DictionaryBean bean=new DictionaryBean();
            LoadMyProperties loadMyProperties=new  LoadMyProperties();          
            bean=loadMyProperties.getMyPropertiesFile(bean);
            String dbUrl=bean.getDbUrl();
            String dbName=bean.getDbName();
            String dbUserName=bean.getDbUserName();
            String dbPassword=bean.getDbPassword();
            String driverName=bean.getDriverName();
 String typeaword=bean.getTypeaword();
 String errorMessage=bean.getErrorMessage();
 String newline=bean.getNewline();
 String outputmeaning=bean.getOutputmeaning();
Class.forName(driverName);
con=DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
Statement s=con.createStatement();
ResultSet rs=s.executeQuery("select * from wordmeaning where word like '"+word+"%'");

%>

<!DOCTYPE html>
<%

 if(rs.next())
                    { 
    String words=rs.getString("word");    
    String meanings=rs.getString("meaning");
                        out.println("<center>");
                        out.println("<table width='30%'>");
                        out.println("<tr>");
                        out.println("<td><b><font size='5' color=black>"+words+":-</font></b></td>");
                        out.println("<td>&nbsp;&nbsp;&nbsp;</td>");
                        out.println("<td><b><font color=red></font></b></td>");                       
                        out.println("</tr>");
                       
                        out.println("<tr>");
                        out.println("<td></td>");
                        out.println("<td>&nbsp;&nbsp;&nbsp;</td>");
                        out.println("<td></td>");                       
                        out.println("</tr>");
                       
                        out.println("<tr>");
                        out.println("<td><b><font color=black>"+outputmeaning+"</font></b></td>");
                        out.println("<td>&nbsp;&nbsp;&nbsp;</td>");
                        out.println("<td><b><font color=red>"+meanings+"</font></b></td>");                       
                        out.println("</tr>");
                        out.println("</table>");
                        out.println("<center>");
                       
                        out.println("<tr>");
                        out.println("<td></td>");
                        out.println("<td><font color=black>"+newline+"</font></b></td>");
                        out.println("<td></td>");                       
                        out.println("</tr>");
                 
}else {
                        out.println("<center>");
                        out.println("<table>");
                        out.println("<tr>");
                        out.println("<td><b><font color=black>"+errorMessage+"</font></b></td>");                                              
                        out.println("</tr>");
                        out.println("</table>");
                        out.println("</center>");
                    }
rs.close();
s.close();
con.close();

%>

admin.jsp

<%--
    Document   : admin
    Created on : 29 Jun, 2013, 11:28:15 PM
    Author     : Aravind Sankaran
--%>

<%@page import="com.dictionary.common.LoadMyProperties"%>
<%@page import="com.dictionary.bean.DictionaryBean"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% String un=request.getParameter("un");%>
<%
DictionaryBean bean=new DictionaryBean();
            LoadMyProperties loadMyProperties=new  LoadMyProperties();          
            bean=loadMyProperties.getMyPropertiesFile(bean);
            String typeaword=bean.getTypeaword();
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <p>
          <form method="post" action="/Dictionary/AddNewWords">       
     
      <center>
              <table width="50%" border="0">
              <tr>
                  <td>&nbsp;</td>
                  <td><b>Word</b></td>  
                  <td><input type="text" name="word"  ></td>
                  <td>&nbsp;</td>
              </tr>
              <tr>
                  <td>&nbsp;</td>
                  <td><b></b></td>  
                  <td></td>
                  <td>&nbsp;</td>
              </tr>
              <tr>
              <td>&nbsp;</td>
              <td><b>Meaning</b></td>  
              <td><textarea name="meaning" rows="5" cols="18"></textarea></td>
              <td>&nbsp;</td>
              </tr>
              <tr>
              <td>&nbsp;</td>
              <td><b></b></td>  
              <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="SUBMIT"></td>
              <td>&nbsp;</td>
              </tr> 
              </table>
              </form>
        </p>
    </body>
</html>

com.dictionary.bean package

1.DictionaryBean.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dictionary.bean;

/**
 *
 * @author Aravind Sankaran
 */
public class DictionaryBean {
   
private String  userName;
private String userPassword;
private String  dbName;
private String dbUserName;
private String dbPassword;;
private String dbUrl;
private String word;
private String meaning;
private String driverName;
private String typeaword;
private String errorMessage;
private String newline;
private String outputmeaning;
private boolean valid;
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public String getDbName() {
        return dbName;
    }

    public String getDriverName() {
        return driverName;
    }

    public void setDriverName(String driverName) {
        this.driverName = driverName;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public String getNewline() {
        return newline;
    }

    public void setNewline(String newline) {
        this.newline = newline;
    }

    public String getOutputmeaning() {
        return outputmeaning;
    }

    public void setOutputmeaning(String outputmeaning) {
        this.outputmeaning = outputmeaning;
    }

    public String getTypeaword() {
        return typeaword;
    }

    public void setTypeaword(String typeaword) {
        this.typeaword = typeaword;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }


    public String getDbPassword() {
        return dbPassword;
    }

    public void setDbPassword(String dbPassword) {
        this.dbPassword = dbPassword;
    }

    public String getDbUrl() {
        return dbUrl;
    }

    public void setDbUrl(String dbUrl) {
        this.dbUrl = dbUrl;
    }

    public String getDbUserName() {
        return dbUserName;
    }

    public void setDbUserName(String dbUserName) {
        this.dbUserName = dbUserName;
    }

    public String getMeaning() {
        return meaning;
    }

    public void setMeaning(String meaning) {
        this.meaning = meaning;
    }

    public boolean isValid() {
        return valid;
    }

    public void setValid(boolean valid) {
        this.valid = valid;
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }
}

com.dictionary.common package 
1. AddNewWords.java

package com.dictionary.common;
import com.dictionary.bean.DictionaryBean;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AddNewWords extends HttpServlet {
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       // processRequest(request, response);
        String word=request.getParameter("word");
        String meaning=request.getParameter("meaning");
        DictionaryBean db=new DictionaryBean();
        db.setWord(word);
        db.setMeaning(meaning);
      
        db=Operations.addNewWord(db);
        if(db.isValid()){
            HttpSession session=request.getSession(true);
            session.setAttribute("admin", db);
            response.sendRedirect("admin.jsp");
        }else{
            response.sendRedirect("index.jsp");
        }
    }

}

2.DbConnect.java

package com.dictionary.common;
import com.dictionary.bean.DictionaryBean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *
 * @author Aravind Sankaran
 */
public class DbConnect {
static Connection con=null;      
    public static Connection getConnection(){
        try{
            DictionaryBean bean=new DictionaryBean();
            LoadMyProperties loadMyProperties=new  LoadMyProperties();          
            bean=loadMyProperties.getMyPropertiesFile(bean);
            String dbUrl=bean.getDbUrl();
            String dbName=bean.getDbName();
            String dbUserName=bean.getDbUserName();
            String dbPassword=bean.getDbPassword();
            String driverName=bean.getDriverName();
            Class.forName(driverName);
            try{
            con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
            System.out.println("connection occured");          
            }
            catch(SQLException se){
               
            }
        }catch(ClassNotFoundException e){
           
        }
      return con;
    }
}

3.LoadMyProperties.java

package com.dictionary.common;
import com.dictionary.bean.DictionaryBean;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 *
 * @author Aravind Sankaran
 */
public class LoadMyProperties {
 Properties configProp=new Properties();
     public DictionaryBean getMyPropertiesFile(DictionaryBean myBean){   
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("com/dictionary/resources/db.properties");
        try {
          configProp.load(in);
          String dbUrl=configProp.getProperty("dbUrl");
          String dbName=configProp.getProperty("dbName");
          String dbUserName=configProp.getProperty("dbUserName");
          String dbPassword=configProp.getProperty("dbPassword");
        
          String driverName=configProp.getProperty("driverName");
          String typeaword=configProp.getProperty("typeaword");
          String errorMessage=configProp.getProperty("errorMessage");
          String newline=configProp.getProperty("newline");
          String outputmeaning=configProp.getProperty("outputmeaning");
         
          myBean.setDbUrl(dbUrl);
          myBean.setDbName(dbName);
          myBean.setDbUserName(dbUserName);
          myBean.setDbPassword(dbPassword);
         
          myBean.setDriverName(driverName);
          myBean.setTypeaword(typeaword);
          myBean.setErrorMessage(errorMessage);
          myBean.setNewline(newline);
          myBean.setOutputmeaning(outputmeaning);
          myBean.setValid(true);
        } catch (IOException e) {
            e.printStackTrace();
        }      
      
        return myBean;
    }
}

 4.Login.java

package com.dictionary.common;
import com.dictionary.bean.DictionaryBean;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 public class Login extends HttpServlet {
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
      //  processRequest(request, response);
        String userName=request.getParameter("username");
        String userPassword=request.getParameter("password");
        DictionaryBean db=new DictionaryBean();
        db.setUserName(userName);
        db.setUserPassword(userPassword);      
        db=Operations.login(db);
        if(db.isValid()){
            HttpSession session=request.getSession(true);
            session.setAttribute("admin", db);
            String un=db.getUserName();
            response.sendRedirect("admin.jsp?un="+un);
        }else{
            response.sendRedirect("index.jsp");
        }
    }
}

5. Operations.java

package com.dictionary.common;
import com.dictionary.bean.DictionaryBean;
import java.sql.*;

/**
 *
 * @author Aravind Sankaran
 */
public class Operations {
 public static DictionaryBean login(DictionaryBean bean){
        String userName=bean.getUserName();
        String userPassword=bean.getUserPassword();
        String query="select * from login where username='"+userName+"' AND password='"+userPassword+"'";
        Connection con=DbConnect.getConnection();
        try {
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery(query);
            boolean more=rs.next();{
            if(!more){
                System.out.print("no more records");
                bean.setValid(false);
            }else if(more){
             
               String username=rs.getString("username");
               String password=rs.getString("password");
               bean.setUserName(username);               
               bean.setUserPassword(password);
               bean.setValid(true);
            }
        }
        } catch (SQLException ex) {
           System.out.println("error occured in Login Operations");
        }
        return bean;
    }
  
    public static DictionaryBean addNewWord(DictionaryBean bean){
        String word=bean.getWord();
        String meaning=bean.getMeaning();
        PreparedStatement ps= null;

        String query="insert into wordmeaning(word,meaning) values(?,?)";
        Connection con=DbConnect.getConnection();
        try {
            ps=(PreparedStatement) con.prepareStatement(query);
            ps.setString(1, word);
            ps.setString(2, meaning);           
            ps.executeUpdate();
         
            bean.setWord(word);         
            bean.setMeaning(meaning);
            bean.setValid(true);
        } catch (SQLException ex) {
           System.out.println("error occured in adding new word ");
        }
        return bean;
    }
}
}
com.dictionary.resources  package

db.properties

dbUrl=jdbc:mysql://localhost:3306/dictionary?
dbName=dictionary
dbUserName=root
dbPassword=root
driverName=com.mysql.jdbc.Driver
typeaword=Type a word:
errorMessage=If a word in the dictionary were misspelled, how would we know?
newline=-----------------------
outputmeaning=Meaning:

jar file:

mysql-connector-java-3.1.14-bin.jar

Output:




1 comment: