Pin It

How to use various types of JSP tags in a JSP page ?

Program to demonstrate usage of various JSP tags in a JSP page

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.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.

<%@ 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>

<h2>The five types of JSP tags are as follows : </h2>

 <table cellspacing="0" cellpadding="5" border="1">
  <tr>   
   <th>Tag Name</th>
   <th>Tag Symbol</th>
   <th>Tag Usage</th>
  </tr>

  <tr>
   <td>JSP comment</td>
   <td>&lt;%-- --%&gt;</td>
   <td>JSP engine ignores code between this tag</td>
  </tr>

  <tr>
   <td>JSP directive</td>
   <td>&lt;%@ %&gt;</td>
   <td>JSP engine uses it as metadata for entire JSP page</td>
  </tr>
  
  <tr>
   <td>JSP expression</td>
   <td>&lt;%= %&gt;</td>
   <td>JSP engine uses it for displaying string value of evaluated expression</td>
  </tr>
  
  <tr>
   <td>JSP declaration</td>
   <td>&lt;%! %&gt;</td>
   <td>JSP engine uses it as for declaring instance variables and methods</td>
  </tr>
  
  <tr>
   <td>JSP scriptlet</td>
   <td>&lt;% %&gt;</td>
   <td>JSP engine uses it to insert block of Java statements</td>
  </tr>
 </table>
 
 
 <%-- This is comment tag , generally ignored by JSP engine --%>

 <%-- Below is the directive tag , used as a meta-data for entire JSP--%>
 <%@ page import="com.hubberspot.model.Customer"%>
 <%@ page import="java.io.*"%>



 <%-- Below is the JSP declaration tag used to declare methods and instance variables --%>
 <%!
       int counter = 0;
 
    public 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+"n" );
    out.println("Last Name : "+ lastName+"n" );
    out.println("Email Address : "+ emailAddress+"n" );
    out.close();
   } catch (IOException e) {   
    e.printStackTrace();
   }
   

  }  
   %>

 <%-- Below tag is the JSP Scriptlet tag which is used for inserting block
of java statements  --%>
 <%
  // 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("/WEB-INF/subscription.txt");

  // Using DataModel in a JSP
  Customer customer = new Customer(firstName, lastName, emailAddress);

  // Using the this to write info to file
  this.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>
   <%-- Below is the JSP expression used to display string value of an expression --%>
   <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>



Output of the program :


1. subscribe.html


























 2. email_entry.jsp






 3. subscription.txt

 

Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot