0% found this document useful (0 votes)
3 views4 pages

Pra 8

The document outlines a practical assignment for a servlet application that retrieves employee information based on an employee ID. It includes an HTML form for user input and Java code for processing the request, connecting to a MySQL database, and displaying the results in a table format. The servlet handles GET requests to fetch and display employee details from the database.

Uploaded by

pateldivy875
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Pra 8

The document outlines a practical assignment for a servlet application that retrieves employee information based on an employee ID. It includes an HTML form for user input and Java code for processing the request, connecting to a MySQL database, and displaying the results in a table format. The servlet handles GET requests to fetch and display employee details from the database.

Uploaded by

pateldivy875
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

BE 5th Sem [223SBEIT30016] Adv.

Java programming[CT506A-N]

PRACTICAL-8

AIM: Assume that the information regarding the salary and age for all employees of an
organization is available in a database. Develop a servlet application which takes the
employee ID of an employee as a request parameter and displays the employee information

CODE:

index.html:

<!DOCTYPE html>

<!--

To change this license header, choose License Headers in Project Properties.

To change this template file, choose Tools | Templates

and open the template in the editor.

--><html>

<head>

<title>JDBC Example Using Servlet</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>

<body>

<form action="Search1">

Enter your ID:<input type="text" name="id"/><br/>

<input type="submit" value="search"/>

</form>

</body>

</html>

search.java:
import java.io.IOException;

import java.io.PrintWriter;

import java.sql.ResultSetMetaData;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

IT Dept, LDRP-ITR Page 1


BE 5th Sem [223SBEIT30016] Adv.Java programming[CT506A-N]

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.sql.*;

public class Search1 extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

try (PrintWriter out = response.getWriter()) {

out.println("<!DOCTYPE html>");

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet search</title>");

out.println("</head>");

out.println("<body>");

out.println("<h1>Servlet search at " + request.getContextPath() + "</h1>");

out.println("</body>");

out.println("</html>");

the left to edit the code.">

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

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

int id1=Integer.valueOf(id);

out.print(id1);

try{

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

IT Dept, LDRP-ITR Page 2


BE 5th Sem [223SBEIT30016] Adv.Java programming[CT506A-N]

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/emp?useSSL=false", "root",
"root");

PreparedStatement ps=con.prepareStatement("select * from student4 where id=?");

ps.setInt(1,id1);

out.print("<table width=50% border=1>");

out.print("<caption>Result:</caption>");

ResultSet rs=ps.executeQuery();

ResultSetMetaData rsmd=rs.getMetaData();

int total=rsmd.getColumnCount();

out.print("<tr>");

for(int i=1;i<=total;i++)

out.print("<th>"+rsmd.getColumnName(i)+"</th>"); }

out.print("</tr>");

while(rs.next())

out.print("<tr><td>"+rs.getInt(1)+"</td><td>"+rs.getString(2)+"</td><td>"+rs.getInt(3)+"</
td></tr>"); }

out.print("</table>");

}catch (ClassNotFoundException | SQLException e2) {}

finally{out.close();}

} @Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

@Override

public String getServletInfo() {

return "Short description";}

IT Dept, LDRP-ITR Page 3


BE 5th Sem [223SBEIT30016] Adv.Java programming[CT506A-N]

OUTPUT:

IT Dept, LDRP-ITR Page 4

You might also like