QuestionQuestion

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 append the form data to a random access file. After the form data has been appended to the file, respond back with the complete contents of the data file. The form must contain a minimum of three input fields. Attach your servlet source code file and random access data file to this assignment in one zipped file.

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 PreviewSolution 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)
{...
$35.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats