Search.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<form method="post" action="SearchAction">
<table border="0" width="300" align="center" bgcolor="#e9fh">
<tr><td colspan=2 style="font-size:12pt;" align="center">
<h3>Search Details</h3></td></tr>
<tr><td ><b>Student ID:</b></td>
<td>: <input type="text" name="Id" id="Id">
</td></tr>
<tr><td colspan=2 align="center">
<input type="submit" value="Search" ></td></tr>
</table>
</form>
</html>
SearchView.jsp
<%@ page import="java.util.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<table width="700px" align="center"
style="border:1px solid #000000;">
<tr>
<td colspan=4 align="center"
style="background-color:teal">
<b>Employee Records</b></td>
</tr>
<tr style="background-color:lightgrey;">
<td><b>Id</b></td>
<td><b>Name</b></td>
<td><b>Course</b></td>
<td><b>Age</b></td>
<td><b>Address</b></td>
<td><b>Email</b></td>
</tr>
<%
int count = 0;
String color = "#F9EBB3";
if (request.getAttribute("pid_list") != null) {
ArrayList pid_list = (ArrayList) request.getAttribute("pid_list");
System.out.println(pid_list);
Iterator itr = pid_list.iterator();
while (itr.hasNext()) {
if ((count % 2) == 0) {
color = "#eeffee";
}
count++;
ArrayList al = (ArrayList) itr.next();
%>
<tr style="background-color:<%=color%>;">
<td><%=al.get(0)%></td>
<td><%=al.get(1)%></td>
<td><%=al.get(2)%></td>
<td><%=al.get(3)%></td>
<td><%=al.get(4)%></td>
<td><%=al.get(5)%></td>
</tr>
<%
}
}
if (count == 0) {
%>
<tr>
<td colspan=4 align="center"
style="background-color:#eeffee"><b>No Record Found..</b></td>
</tr>
<% }
%>
</table>
</body>
</html>
SearchAction.java (Servlet Page)
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SearchAction extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "education";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "admin";
Statement st=null;
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url + dbName, userName, password);
System.out.println("connected!.....");
String eid = request.getParameter("Id");
ArrayList al = null;
ArrayList pid_list = new ArrayList();
String query = "select * from education";
if(eid!=null && !eid.equals("")){
query = "select * from education where Id='" + eid + "' ";
}
System.out.println("query " + query);
st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
al = new ArrayList();
al.add(rs.getInt(1));
al.add(rs.getString(2));
al.add(rs.getString(3));
al.add(rs.getInt(4));
al.add(rs.getString(5));
al.add(rs.getString(6));
System.out.println("al :: " + al);
pid_list.add(al);
}
request.setAttribute("pid_list", pid_list);
RequestDispatcher view = request.getRequestDispatcher("view.jsp");
view.forward(request, response);
conn.close();
System.out.println("Disconnected!");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public String getServletInfo() {
return "getting records from database through servlet controller";
}// </editor-fold>
}
No comments:
Post a Comment