Question
Here is the code:
import java.io.*;
import java.util.Scanner;
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("<html><body>");
out.println("<form action='" + request.getRequestURL() + "' method='post' >");
out.println("First Name:");
out.println("<input type='text' name='FNAME' />");
out.println("<br>");
out.println("Last Name:");
out.println("<input type='text' name='LNAME' />");
out.println("<br>");
out.println("Phone:");
out.println("<input type='text' name='PHONE' />");
out.println("<input type='submit' value='Submit' />");
out.println("</form>");
out.println("</body></html>");
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("<html><body>");
ResultSet rset = stmt.executeQuery("SELECT * FROM MYTABLE");
while (rset.next()) {
out.print("<pre>");
out.print("First Name: " + rset.getString(1));
out.print("</pre>");
out.println();
out.print("<pre>");
out.print("Last Name: " + rset.getString(2));
out.print("</pre>");
out.println();
out.print("<pre>");
out.print("Phone: " + rset.getString(3));
out.print("</pre><br>");
out.println();
out.println();
}
out.println("</body></html>");
out.close();
stmt.close();
}
catch (Exception e) {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println(e.getMessage());
out.println("</body></html>");
out.close();
}
}
Solution Preview
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated constructor stub
String fn = request.getParameter("FirstName");
String mn = request.getParameter("MidName");
String ln = request.getParameter("LastName");
response.setContentType("text/html");
PrintWriter _FileWriter = new PrintWriter(new FileOutputStream ("c:\\temp\\yohannesweek4.dat",true));
_FileWriter.println( fn + "|" + mn + "|" + ln + "\n" );
_FileWriter.close();
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
InputStream ist = new FileInputStream("c:\\temp\\yohannesweek4.dat");
BufferedReader istream = new BufferedReader(new InputStreamReader(ist));
String text = istream.readLine(); //just read the first line in the text file
out.println("<html>");
out.println("<body>");
while(text!=null)
{...