Friday, December 16, 2016

Create a Simple Java Web Application Using Servlet, JSP and JDBC

Create a Simple Java Web Application Using Servlet, JSP and JDBC

Brower         web app                         servlet
    
request >----------------------------------- insert,update,
 response<----------------------------------- delete,select.......
                                                               ..query
                                                                     |
                                                                     |   forward
                                                                     |
                                                                    jsp page show 

Frist You create a table in oracle ,mysql,Sql sver

create table USER_ACCOUNT
(
USER_NAME VARCHAR2(30) not null primary key,
GENDER    VARCHAR2(1) not null,
PASSWORD  VARCHAR2(30) not null
);


create table PRODUCT

(
CODE  VARCHAR2(20) not null primary key,
NAME  VARCHAR2(128) not null,
PRICE FLOAT not null
) ;





insert into user_account (USER_NAME, GENDER, PASSWORD)

values ('prabal', 'M', 'niit@123');
insert into user_account (USER_NAME, GENDER, PASSWORD)
values ('akash', 'M', 'niit@123');


insert into user_account (USER_NAME, GENDER, PASSWORD)
values ('maneesha', 'F', 'niit@1234');
insert into product (CODE, NAME, PRICE)
values ('P001', 'Java Core', 100);
insert into product (CODE, NAME, PRICE)
values ('P002', 'C program', 90);


insert into product (CODE, NAME, PRICE)

values ('P003', 'Devops', 9000);

Then Go to Eclipse and open Dynamic project
And add index.html


 then write a index.html
<html>
  <head>
     <meta charset="UTF-8">
     <title>Simple Web Application</title>
  </head>
  
  <body>
  
  
      
     <ul>
        <li><a href="home">Home</a></li>
        <li><a href="login">Login</a></li>
        <li><a href="productList">Product  List</a>
     </ul>
      
  </body>
</html>


Copy these libraries (
)into the WEB-INF/lib:

and add javax.servlet.jsp.jstl and javax.servlet.jsp.jstl-api and add
  • http://mvnrepository.com/artifact/org.glassfish.web/javax.servlet.jsp.jstl
then go to to src and add package name
and add java class
Class name USERACCOUNT
package com.simplewebapp.beans;
public class UserAccount {
   public static final String GENDER_MALE ="M";
   public static final String GENDER_FEMALE = "F";
    
   private String userName;
   private String gender;
   private String password;
    
   public UserAccount() {
        
   }
    
   public String getUserName() {
       return userName;
   }
   public void setUserName(String userName) {
       this.userName = userName;
   }
   public String getGender() {
       return gender;
   }
   public void setGender(String gender) {
       this.gender = gender;
   }
   public String getPassword() {
       return password;
   }
   public void setPassword(String password) {
       this.password = password;
   }
} 


AND ADD CLASS  Product

package com.simplewebapp.beans;
public class Product {
   private String code;
   private String name;
   private float price;
   public Product() {
   }
   public Product(String code, String name, float price) {
       this.code = code;
       this.name = name;
       this.price = price;
   }
   public String getCode() {
       return code;
   }
   public void setCode(String code) {
       this.code = code;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public float getPrice() {
       return price;
   }
   public void setPrice(float price) {
       this.price = price;
   }
}

then create a package For connection

and add class for oracle

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class OracleConnUtils {
  public static Connection getOracleConnection()
          throws ClassNotFoundException, SQLException {
      
      // Note: Change the connection parameters accordingly.
      String hostName = "localhost";
      String sid = "db11g";
      String userName = "system";
      String password = "oracle";
      return getOracleConnection(hostName, sid, userName, password);
  }
  public static Connection getOracleConnection(String hostName, String sid,
          String userName, String password) throws ClassNotFoundException,
          SQLException {
           
      Class.forName("oracle.jdbc.driver.OracleDriver");
      // URL Connection for Oracle
      // Example: jdbc:oracle:thin:@localhost:1521:db11g
      String connectionURL = "jdbc:oracle:thin:@" + hostName + ":1521:" + sid;
      Connection conn = DriverManager.getConnection(connectionURL, userName,
              password);
      return conn;
  }
}


Add a class ConnectionUtils


import java.sql.Connection;
import java.sql.SQLException;
public class ConnectionUtils {
   public static Connection getConnection()
             throws ClassNotFoundException, SQLException {
      
       // Here I using Oracle Database.
       return OracleConnUtils.getOracleConnection();
     }
    
   public static void closeQuietly(Connection conn) {
       try {
           conn.close();
       } catch (Exception e) {
       }
   }
   public static void rollbackQuietly(Connection conn) {
       try {
           conn.rollback();
       } catch (Exception e) {
       }
   }

}



No comments:

Post a Comment