Create a new Web application titled <yourname>Week7. Next, create a JSP that dis
ID: 3532859 • Letter: C
Question
Create a new Web application titled <yourname>Week7. Next, create a JSP that displays a form when the doGet method is invoked. The form will contain a post action that directs the form post back to the same JSP, which in the doPost method will save the form data to a database using a Java Bean. Use your Oracle account to make the DB connection. After the form data has been saved to the database, respond back with a query from the database displaying all the current records contained in the database, in an appealing format. The form must contain a minimum of three input fields. The grade for this assignment will be based both on the functionality of the servlet and the appearance of the form post results. Name your JSP <yourName>FormPost4 and name the application <yourname>Week7. Create a Web archive file and attach to this assignment.
Form just needs to have First Name, Last name, and Email
Thanks
Explanation / Answer
VWFormPost4.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<FORM METHOD=POST ACTION="VWFormPost4.jsp">
<TABLE align="center" border="1">
<TR>
<TD>First Name:</TD>
<TD><INPUT TYPE="text" NAME="fname"></TD>
</TR>
<TR>
<TD>Last Name:</TD>
<TD><INPUT TYPE="text" NAME="lname"></TD>
</TR>
<TR>
<TD>Email:</TD>
<TD><INPUT TYPE="text" NAME="email"></TD>
</TR>
<TR>
<TD colspan="2"><INPUT TYPE="submit"></TD>
<TD></TD>
</TR>
</TABLE>
</FORM>
<jsp:useBean id="sample" class="myexample.bean" scope="page">
<jsp:setProperty name="sample" property="*"/>
</jsp:useBean>
<%
sample.insert();
ResultSet rs = sample.select();
if(rs !=null) {
out.print("<table align="center">");
out.println("<tr>");
out.println("<th> First Name</th>");
out.println("<th> Last Name</th>");
out.println("<th>Email</th>");
while(rs.next()) {
out.println("<tr>");
out.println("<td>" + rs.getString(1) + "</td>");
out.println("<td>" + rs.getString(2) + "</td>");
out.println("<td>" + rs.getString(3) + "</td>");
out.println("</tr>");
}
out.print("</table>");
}
%>
</BODY>
</HTML>
DBBean.java
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
public class DBBean {
private String firstName;
private String lastName;
private String email;
private Connection connection = null;
private ResultSet rs = null;
private Statement st = null;
private String connectionURL = "jdbc:odbc:oracle";
public DBBean() {
try {
// Load the database driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Get a Connection to the database
connection = DriverManager.getConnection(connectionURL, "scott",
"tiger");
} catch (Exception e) {
System.out.println("Exception is ;" + e);
}
}
public void insert()
{
try
{
String sql = "insert into person values('" + firstName + "','"
+ lastName + "','" + email + "')";
Statement s = connection.createStatement();
s.executeUpdate(sql);
s.close();
} catch (Exception e) {
System.out.println("Exception is ;" + e);
}
}
public ResultSet select()
{
ResultSet rs = null;
try
{
String sql = "select * from person";
Statement s = connection.createStatement();
rs = s.executeQuery(sql);
} catch (Exception e) {
System.out.println("Exception is ;" + e);
} finally {
return rs;
}
}
public void close() {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.