JSP

 JSP

INSERT JAVA PAGE

package com.gd.controller;


import java.io.IOException;

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;  

import java.sql.*;


@WebServlet("/Insert")


public class Insert extends HttpServlet{

@Override

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        

        try {

        Class.forName("com.mysql.cj.jdbc.Driver");

String url = "jdbc:mysql://localhost:3306/emp?characterEncoding=latin1";

            String username = "root";

            String password = "1234";

            Connection con = DriverManager.getConnection(url, username, password);

            if (con != null) {

                System.out.println("connected !");

                Statement st = con.createStatement();

                

                String fname = request.getParameter("fname");

            String sname = request.getParameter("sname");

            String id = request.getParameter("id");

            

            String query="insert into employee values('"+fname+"','"+sname+"','"+id+"')";

                st.execute(query);

                

                String resultpage = "insertresult.jsp";

            RequestDispatcher rd = request.getRequestDispatcher(resultpage);

            rd.forward(request, response);

            }

            else {

            System.out.println("Not Connected");

            }

} catch (Exception e) {

// TODO Auto-generated catch block

System.out.println(e);

}

        

       

    }

}


VIEW JAVA PAGE

package com.gd.controller;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gd.model.Employee;

/**
 * Servlet implementation class Viewdata
 */
@WebServlet("/Viewdata")
public class Viewdata extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Viewdata() {
        super();
        // TODO Auto-generated constructor stub
    }

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//System.out.println("hello get");
ArrayList<Employee> arr = new ArrayList<Employee>(); 
try {
String connectionURL = "jdbc:mysql://localhost:3306/emp?characterEncoding=latin1";
    Connection connection = null; 
    Class.forName("com.mysql.cj.jdbc.Driver");
    connection = DriverManager.getConnection(connectionURL, "root", "1234");
    if(!connection.isClosed())
    {
    System.out.println("Connected");
    String query = "select * from employee";
    Statement st = connection.createStatement();
    ResultSet rs = st.executeQuery(query);
    while(rs.next()) {
    String fname = rs.getString("fname");
    String sname = rs.getString("sname");
    String id = rs.getString("id");
   
    Employee emp = new Employee();
    emp.setFname(fname);
    emp.setSname(sname);
    emp.setId(id);
    arr.add(emp);
    }
    request.setAttribute("arr", arr);
    RequestDispatcher rd = request.getRequestDispatcher("/view.jsp");
            rd.forward(request, response);
    }
    
}
catch(Exception ex) {
System.out.println("Unable to connect");
}
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("hello get");
}

}


INSERT.JSP PAGE

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert Your Record</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
</head>
<body>
<div align ="center" style = "font-size : 20px;">
<hr/>
        <h1>Enter Details!</h1>
        <form action ="Insert" method="post">
            <b>Enter First Name :</b>
            <br/>
            <input type="text" name ="fname" required />
            <br/>
            <br/>
            <b>Enter Second Name :</b> 
            <br/>
            <input type="text" name ="sname" required />
            <br/>
            <br/>
            <b>Enter Email Id :</b> 
            <br/>
            <input type="text" name ="id" required />
            <br/>
            <br/>
            <input type ="submit" value ="Submit Details" style = "font-size : 17px; font-weight: bold;" class="btn btn-primary"/>
        </form>
        <br/>
        <hr />
        <a href = "Viewdata">
        <input type = "button" value = "View Records" style = "font-size : 17px; font-weight: bold;" class="btn btn-dark"/>
        </a>
        <br/>
        <hr/>
        </div>
 <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-pprn3073KE6tl6bjs2QrFaJGz5/SUsLqktiwsUTF55Jfv3qYSDhgCecCxMW52nD2" crossorigin="anonymous"></script>
</body>
</html>


VIEW.JSP PAGE

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 
<%@ page import="java.util.*" %>
<%@ page import="com.gd.model.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>View Your Records</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
</head>
<body style = "font-size : 20px;">
<div align = "center" class = "container">
<br />
<h1>Your Inserted Records Are!</h1>
<br />
<a href = "insert.jsp"><input type = "button" value = "Press Me To Move Registration Page" style = "font-size : 20px; font-weight : bold;" class = "btn btn-primary"/></a>
<br/>
<br/>
<table border = 1 cellspacing = 10 cellpadding = 10 class="table table-dark table-striped-columns">
<thead>
<tr>
<th>First Name</th>
<th>Second Name</th>
<th>Email Id</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<%  
// retrieve your list from the request, with casting 
ArrayList<Employee> list = (ArrayList<Employee>) request.getAttribute("arr");
// print the information about every category of the list
for(Employee emp: list) {
%>
<tr>
<td><% out.println(emp.getFname());%></td>
<td><%out.println(emp.getSname());%></td>
<td><%out.println(emp.getId());%></td>
<td><a href = "edit.jsp?fname=<% out.println(emp.getFname());%>"><button class ="btn btn-primary">Edit</button></a></td>
<td><a href = "del.jsp?fname=<% out.println(emp.getFname());%>"><button  class="btn btn-danger">Delete</button></a></td>
</tr>
<%
}
%>
</tbody>
</table>
</div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-pprn3073KE6tl6bjs2QrFaJGz5/SUsLqktiwsUTF55Jfv3qYSDhgCecCxMW52nD2" crossorigin="anonymous"></script>
  </body>
</html>

Comments

Popular posts from this blog

INTERRUPT CYCLE IN COMPUTER ORGANISATION AND ARCHITECTURE

Spring Boot

Addressing Modes in Computer organization and architecture