Create a new Web application titled <yourname>Week5. In this application you wil
ID: 3624122 • Letter: C
Question
Create a new Web application titled <yourname>Week5. In this application you will also create a virtual directory so that you do not have to use the word servlet as part of your form post URL. You will then be able to run your Web application servlet by using a URL similar to the following: http://localhost:7070/<yourname>Week5/<yourname>FormPost2. To create your virtual directory you may start by modifying the web.xml attached to this assignment. Next, create a Servlet 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 servlet, which in the doPost method will save the form data to a database. 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 servlet <yourName>FormPost2 and name the application <yourname>Week5. Create a Web archive file and attach to this assignment. Do not copy (cut and paste) any example code, create your own code and use the examples as a guide.Explanation / Answer
Dear..
Am giving you the sample code sniplet:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import oracle.jdbc.OracleResultSetMetaData;
public class FormPost2 extends HttpServlet
{
Connection con = null;
public FormPost2()
{
init();
}
public void init()
{
try
{
Class.forName ("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "student1", "pass");
Statement stmt = con.createStatement();
stmt.executeUpdate("CREATE TABLE MYTABLE (FNAME VARCHAR2(20), LNAME VARCHAR2(40), PHONE VARCHAR2(20))");
stmt.close();
}
catch (Exception e)
{
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("
");
out.println("");
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
if (con != null)
init();
String fname = request.getParameter("FNAME");
String lname = request.getParameter("LNAME");
String phone = request.getParameter("PHONE");
Statement stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO MYTABLE VALUES
('" + fname + "', '" + lname + "', '" + phone + "')");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
ResultSet rset = stmt.executeQuery("SELECT * FROM MYTABLE");
while (rset.next())
{
out.print("
");
out.println();
out.print("
");
out.println();
out.print("
");
out.println();
out.println();
}
out.println("");
out.close();
stmt.close();
}
catch (Exception e) {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println(e.getMessage());
out.println("");
out.close();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.