0% found this document useful (0 votes)
10 views5 pages

Java Servlet Student Management Form

The document is a Java servlet that handles student data through a web interface, allowing users to insert, select, update, and delete student records in a database. It connects to a Derby database and generates HTML forms for user interaction. The servlet processes user input and executes corresponding SQL commands based on the action selected by the user.

Uploaded by

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

Java Servlet Student Management Form

The document is a Java servlet that handles student data through a web interface, allowing users to insert, select, update, and delete student records in a database. It connects to a Derby database and generates HTML forms for user interaction. The servlet processes user input and executes corresponding SQL commands based on the action selected by the user.

Uploaded by

Tiya Kochar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import [Link].

*;
import [Link].*;
import [Link].*;
import [Link].*;

public class Student extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();

try
{
[Link]("[Link]");
Connection
conn=[Link]("jdbc:derby://localhost:1527/IBM_DB","ibm","ibm");

[Link]("<html>");
[Link]("<head>");
[Link]("<title>STUDENT FORM</title>");
[Link]("</head>");
[Link]("<body bgcolor=yellow><center>");
[Link]("<form name='f1'>");
[Link]("STUDENT ENTRY FORM");
[Link]("<table border='1' cellspacing='3' cellpadding='2'>");
[Link]("<tr ><td >Enter ID : <input type='text' value=' '
name='txtrno'/></td></tr> ");
[Link]("<tr ><td >Enter Name : <input type='text' value=' '
name='txtname'/> </td></tr>");
[Link]("<tr><td><input type ='submit' value='INSERT' name='btnInsert'/>
<input type ='submit' value='SELECT' name='btn_SELECT'/> <input type ='submit'
value='UPDATE' name='btn_UPDATE'/><input type ='submit' value='DELETE'
name='btn_DELETE'/></td></tr>");
[Link]("</table>");
[Link]("</form>");
[Link]("</center></body>");
[Link]("</html>");

String r,n;
r = [Link]("txtrno");
n = [Link]("txtname");

if([Link]("btnInsert")!=null)
{

try{
if(r!="" && n!="")
{
PreparedStatement ps=[Link]("insert into stud
values(?,?)");

[Link](1, r);
[Link](2, n);

int i=[Link]();

if(i>0)
{
[Link]("You are sucessfully registered");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>STUDENT</title>");
[Link]("</head>");
[Link]("<body ><center>");
[Link]("<form name='f2'>");
[Link]("<table border='1' cellspacing='3' cellpadding='2'>");
[Link]("<tr><td><h3><font color='red'>Roll No</font></h3></td>
<td><h3><font color='red'>Name</font></h3></td></tr>");
Statement stat=[Link]();
ResultSet rs=[Link]("select * from stud order by trim(rno) ");
while([Link]())
{
//[Link]([Link](1));
[Link]("<tr><td>" + [Link](1) + "</td><td>" + [Link](2) +
"</td></tr>");
}
[Link]("</table>");
[Link]("</form>");
[Link]("</center></body>");
[Link]("</html>");
}
}//close outer if
else
{
[Link]("Enter the rno. & Name");
return;

}
catch(Exception se)
{
[Link](se);
[Link]();
}

}//if close
else if([Link]("btn_SELECT")!=null)
{
[Link]("<html>");
[Link]("<head>");
[Link]("<title>STUDENT FORM</title>");
[Link]("</head>");
[Link]("<body><center>");
[Link]("<form name='f2'>");
[Link]("<table bgcolor='pink' width='40%' border='1' cellspacing='3'
cellpadding='2'>");
[Link]("<tr><td><h3><font color='red'>Roll No</font></h3></td>
<td><h3><font color='red'>Name</font></h3></td></tr>");
Statement stat=[Link]();
ResultSet rs=[Link]("select * from stud order by trim(rno) ");
while([Link]())
{
//[Link]([Link](1));
[Link]("<tr><td>" + [Link](1) + "</td><td>" + [Link](2) +
"</td></tr>");
}
[Link]("</table>");
[Link]("</form>");
[Link]("</center></body>");
[Link]("</html>");
[Link]();
[Link]();
//[Link]();

} //else if close

else if([Link]("btn_DELETE")!=null)
{

try{

if(r=="")
{
[Link]("Enter the Roll No First to delete the Record");
return;
}
PreparedStatement ps=[Link]("delete from stud where
rno='"+r+"'");

int i=[Link]();

if(i>0)
{
[Link]("Student Record Deleted Successfully.");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>STUDENT FORM</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<form name='f2'>");
[Link]("<table border='1' cellspacing='3' cellpadding='2'>");
[Link]("<tr><td>Roll No</td> <td>Name</td></tr>");
Statement stat=[Link]();
ResultSet rs=[Link]("select * from stud order by rno ");
while([Link]())
{
//[Link]([Link](1));
[Link]("<tr><td>" + [Link](1) + "</td><td>" + [Link](2) +
"</td></tr>");
}
[Link]("</table>");
[Link]("</form>");
[Link]("</body>");
[Link]("</html>");
}

}//try close
catch(Exception se)
{
[Link](se);
[Link]();
}

}//eles close

else if([Link]("btn_UPDATE")!=null)
{

try{

if(r=="")
{
[Link]("Enter the Roll No First to Update the Record");
return;
}
PreparedStatement ps=[Link]("update stud set
name='"+n+"' where rno='"+r+"'");

int i=[Link]();

if(i>0)
{
[Link]("Student Record Updated Successfully.");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>STUDENT FORM</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<form name='f2'>");
[Link]("<table border='1' cellspacing='3' cellpadding='2'>");
[Link]("<tr><td>Roll No</td> <td>Name</td></tr>");
Statement stat=[Link]();
ResultSet rs=[Link]("select * from stud order by rno ");
while([Link]())
{
//[Link]([Link](1));
[Link]("<tr><td>" + [Link](1) + "</td><td>" + [Link](2) +
"</td></tr>");
}
[Link]("</table>");
[Link]("</form>");
[Link]("</body>");
[Link]("</html>");
}

}//try close
catch(Exception se)
{
[Link](se);
[Link]();
}

}//eles close

}// try close


catch(Exception e)
{

[Link](e);
}

}
}

Common questions

Powered by AI

Using the doGet function implies that data manipulation and retrieval operations are handled through the HTTP GET method, which is designed primarily for data retrieval. This is not ideal for data manipulation actions such as INSERT, UPDATE, and DELETE because GET requests are idempotent and their URLs can be cached or logged, posing security and efficiency concerns. A better practice would be to use POST for operations that modify data to ensure these requests are secure and intended by the client .

Improvements to the HTML user interface could include adding proper labels to input fields, utilizing CSS for better layout and aesthetics, implementing form validation to guide user input, and providing success or error messages next to the relevant actions for clarity. These changes would enhance user experience by making the form more intuitive, visually appealing, and informative, which can reduce user error and improve interaction with the application .

The servlet ensures data persistence by executing SQL statements to perform CRUD (Create, Read, Update, Delete) operations on the database. For insertion, a PreparedStatement is prepared with 'insert into stud values(?,?)' to add new records. For selection, a ResultSet is used with the statement 'select * from stud order by trim(rno)'. Updating records involves executing 'update stud set name=? where rno=?', while deletion is done with 'delete from stud where rno=?' .

The servlet program employs try-catch blocks to handle exceptions that may occur during database operations. For each operation (INSERT, SELECT, UPDATE, DELETE), potential exceptions such as SQLExceptions are caught, and stack traces are printed using se.printStackTrace(), ensuring that errors are logged. However, user feedback is limited to basic error messages displayed on the HTML page, without detailed error handling or user-friendly notifications .

The servlet uses PreparedStatements for SQL query construction in the INSERT operation, which helps mitigate SQL injection risks by avoiding direct concatenation of input values into SQL strings. However, in the DELETE operation, the servlet constructs the query with string concatenation ('delete from stud where rno='+r), making it vulnerable to SQL injection if user input is not validated properly .

In this servlet, server-side validation is implemented through simple checks, like ensuring input fields are not empty before database operations. Server-side validation is crucial for security, as it checks inputs after submission where they can't be bypassed. However, client-side validation could enhance user experience by providing instant feedback and preventing unnecessary server requests when inputs are clearly invalid. Implementing both can create a more secure and user-friendly application .

To support internationalization, the servlet’s HTML output should include externalized text resources through resource bundles, allowing different languages to be supported without requiring code modification. Character encoding (like UTF-8) should be consistently used to correctly display international characters. Additionally, the layout should accommodate varying text lengths that come with translations .

In the Java servlet program, the Connection object is crucial for establishing a link between the servlet and the database. It is initialized using the DriverManager's getConnection method, which requires the database URL, username, and password as parameters to connect to the specified database. In this context, the URL 'jdbc:derby://localhost:1527/IBM_DB', along with the username 'ibm' and password 'ibm', are used to connect to the Derby database .

The servlet attempts to implement separation of concerns by isolating HTML generation, form data handling, and database manipulation within different segments of the code. However, it falls short as it tightly couples presentation (HTML) and business logic (database operations) in a single servlet, making maintenance and scalability difficult. A more robust design would separate these concerns into distinct classes or components .

Form data from the HTML page is retrieved using the request.getParameter method, which extracts the values entered by the user. In the servlet, parameters like 'txtrno' and 'txtname' correspond to the inputs for ID and Name respectively. This data is then utilized for performing database operations depending on which button (INSERT, SELECT, UPDATE, DELETE) is pressed, determining the operation to be executed .

You might also like