Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Requirements : Create a mock form using the following criteria. C reate a JavaBe

ID: 3820681 • Letter: R

Question

Requirements: Create a mock form using the following criteria. Create a JavaBean class, a HTML Form that lets users enter class data, a Servlet class that processes form inputs, and a JSP that receives processing results from the Servlet and display them back to users.

1. JavaBean class: Create a Java class in a package. This Java class should qualify as a JavaBean class, i.e., meet all the JavaBean requirements. It should have at least 2 Constructors, 4 private data fields, including one boolean type, and their corresponding get and set methods.

2. HTML Form: Create an html page that contains a Form that let users enter the data field values for the JavaBean class. The form action should request a Servlet class (using the Servlet’s URL pattern).

3. Servlet: Create a Servlet class to handle form inputs. It should get parameters from the form and then use them to create an instance of the JavaBean. It should record the JavaBean data field values in a .txt file in the WEB-INF folder with one record per line. After that, it should add the JavaBean instance as an attribute to the session and forward the request and response to the JSP.

4. JSP: Create a JSP page to display processing results sent by the Servlet. You can use JSP standard tags.

Please include screen shot of the code.

Explanation / Answer

form1.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<from action="Serv1" method="get">

   <table>
       <tr>
           <td>
               First Name:
           </td>
           <td>
               <input type="text" name="fnm"/>
           </td>
       </tr>
      
      
       <tr>
           <td>
               Last Name:
           </td>
           <td>
               <input type="text" name="lnm"/>
           </td>
       </tr>
      
       <tr>
           <td>
               Indian? (Y/N)
           </td>
           <td>
               <input type="text" name="ind"/>
           </td>
       </tr>
      
       <tr>
           <td>
               Phone number:
           </td>
           <td>
               <input type="text" name="phn" size="10"/>
           </td>
       </tr>
  
       <tr>
           <td>
               <input type="submit"/>
           </td>
       </tr>
   </table>
  
  
  


</from>

</body>
</html>

Serv1.java

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import phpDemo.Bean;

/**
* Servlet implementation class Serv1
*/
@WebServlet("/Serv1")
public class Serv1 extends HttpServlet {
   private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Serv1() {
super();
// TODO Auto-generated constructor stub
}

   /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       // TODO Auto-generated method stub
      
       String fnm=(String)request.getParameter("fnm");
       String lnm=(String)request.getParameter("lnm");
       String ind=(String)request.getParameter("ind");
       String phn=(String)request.getParameter("phn");
       boolean indian=true;
       if(ind.equals("N"))
       indian=false;
      
       Bean bean=new Bean(fnm, lnm, indian, phn);
      
       try{
           BufferedWriter bw=new BufferedWriter(new FileWriter("F:\Chegg\phpDemo\WebContent\WEB-INF\a.txt"));
          
           String content="First Name: "+fnm+""
                   + " Last Name: "+lnm+""
                           + " Indian?: "+ind+""
                                   + " Phone Number: "+phn;
          
           bw.write(content);
           bw.close();
       }catch(Exception e){}
      
       HttpSession s=request.getSession();
       s.setAttribute("fnm", fnm);
       s.setAttribute("lnm", lnm);
       s.setAttribute("ind", ind);
       s.setAttribute("phn", phn);
      
       response.sendRedirect("show.jsp");
   }

   /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       // TODO Auto-generated method stub
   }

}

Bean.java

package phpDemo;

public class Bean {

       private String firstName;
       private String lastName;
       private boolean indian;
       private String phone;
      
       public Bean(){
           firstName="";
           lastName="";
           indian=true;
           phone="";
       }
      
       public Bean(String fnm,String lnm,boolean ind,String phn){
           firstName=fnm;
           lastName=lnm;
           indian=ind;
           phone=phn;
       }
      
       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 boolean isIndian() {
           return indian;
       }
       public void setIndian(boolean indian) {
           this.indian = indian;
       }
       public String getPhone() {
           return phone;
       }
       public void setPhone(String phone) {
           this.phone = phone;
       }
      
}

show.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<%

String fnm=(String)session.getAttribute("fnm");
String lnm=(String)session.getAttribute("lnm");
String ind=(String)session.getAttribute("ind");
String phn=(String)session.getAttribute("phn");

%>


<h3><%=fnm %> <%=lnm %> IsIndian? <%=ind %> Phone: <%=phn %></h3>
</body>
</html>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote