Program to demonstrate how to use model / Java / Pojo classes with JSP and Servlets
Model Class in Java :
package com.hubberspot.model;
public class Customer {
private String firstName;
private String lastName;
private String emailAddress;
public Customer() {
firstName = "";
lastName = "";
emailAddress = "";
}
public Customer(String firstName , String lastName , String emailAddress) {
this.firstName = firstName;
this.lastName = lastName;
this.emailAddress = emailAddress;
}
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 getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
}
Service Class in Java :
package com.hubberspot.utility;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import com.hubberspot.model.Customer;
public class CustomerService {
public static void write(Customer customer, String filePath) {
try {
File file = new File(filePath);
FileWriter writer = new FileWriter(file);
PrintWriter out = new PrintWriter(writer);
String firstName = customer.getFirstName();
String lastName = customer.getLastName();
String emailAddress = customer.getEmailAddress();
out.println("First Name : "+ firstName+"" );
out.println("Last Name : "+ lastName+"" );
out.println("Email Address : "+ emailAddress+"" );
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
subscribe.html
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39. |
<!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>Email Subscription</title>
</head>
<body>
<h1>Join Hubberspot's Email list</h1>
<p>
In order to join our email list, enter you name and email address
below </br> and Click Submit button.
</p>
<form action="email_entry.jsp" method="get">
<table cellspacing="5" border="0">
<tr>
<td align="right">First Name : </td>
<td><input type="text" name="firstName"></td>
</tr>
<tr>
<td align="right">Last Name : </td>
<td><input type="text" name="lastName"></td>
</tr>
<tr>
<td align="right">Email Address : </td>
<td><input type="text" name="emailAddress"></td>
</tr>
<tr>
<td></td>
<td align="left"></br><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
|
|
email_entry.jsp
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. |
<%@ 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>Email Subscription</title>
</head>
<body>
<%@ page import="com.hubberspot.model.Customer" %>
<%@ page import="com.hubberspot.utility.CustomerService" %>
<%
// Get the form requests
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String emailAddress = request.getParameter("emailAddress");
// Get the complete real path for subscription.txt
ServletContext context = request.getServletContext();
String filePath = context.getRealPath("/subscription.txt");
// Using DataModel in a JSP
Customer customer = new Customer(firstName, lastName, emailAddress);
// Using the CustomerService class to write info to file
CustomerService.write(customer, filePath);
%>
<h1>Thanks for joining Hubberspot's Email list</h1>
<p>Kindly have a look what you have entered :</p>
<table cellspacing="0" cellpadding="5" border="1">
<tr>
<td align="right">First Name :</td>
<td><%=customer.getFirstName()%></td>
</tr>
<tr>
<td align="right">Last Name :</td>
<td><%=customer.getLastName()%></td>
</tr>
<tr>
<td align="right">Email Address :</td>
<td><%=customer.getEmailAddress()%></td>
</tr>
</table>
<p>
To provide correct information, click 'back' on browser window </br>or
click on Back button below :
</p>
<form action="subscribe.html" method="post">
<input type="submit" value="Back">
</form>
</body>
</html>
|
|
How to run the application :
- Deploy the application on the server.
- Run and request for subscribe.html.
- After filling the necessary data , submit the data to email_entry.jsp.
- email_entry.jsp displays the information in it .
- A file subscrition.txt gets created and data is written to it by CustomerService class.
Output of the program :
1. subscribe.html
2. email_entry.jsp
3. subscription.txt