4/11/2012

J2EE#2 Target: JSP + Servlet + Service + DAO + JDBC (Oracle) + SP (3)

In J2EE#2 Target: JSP + Servlet + Service + DAO + JDBC (Oracle) + SP (2),
I've realized JSP + Servlet + Service. Now the remaining is Database part. DAO is a design pattern used to deal with the database.
What needs to be mentioned first is that, in J2EE, when adding external JAR files, we need to copy the JAR files under the directory "WEB-INF\lib". So first we copy the JAR files "ojdbc14.jar" under this directory.
Then new a new package under src "com.company.daos".
Copy "JDBCUtil.java" under the package. Please refer to my previous post:

JDBC configuration and implementation (Oracle&Eclipse)


Then New a java file "HelloDao.java".
Code:
01 package com.company.daos;
02
03 import java.util.*;
04 import java.sql.*;
05 import oracle.jdbc.OracleTypes;
06 import com.company.beans.User;
07
08 public class HelloDao {
09
10     public void save(User user{
11         try {
12             Connection conn = JDBCUtil.getConnection();
13             String sp = "{? = call saveUser(?,?)}";
14             CallableStatement cs = conn.prepareCall(sp);
15             cs.registerOutParameter(1, Types.INTEGER); // output
16             cs.setString(2, user.getName()); // input
17             cs.setInt(3, user.getAge());
18             cs.execute();
19             cs.close();
20
21         } catch (Exception e{
22             e.printStackTrace();
23         }
24     }
25
26     public List<User> query() {
27         List<User> list = new ArrayList<User>();
28         String sp = "{? = call queryUser()}";
29         try {
30             Connection conn = JDBCUtil.getConnection();
31             CallableStatement cs = conn.prepareCall(sp);
32             cs.registerOutParameter(1, OracleTypes.CURSOR);    
33             cs.execute();
34             ResultSet rs = (ResultSetcs.getObject(1);
35            
36             while (rs.next()) {
37                 User user = new User();
38                 user.setName(rs.getString("Name"));
39                 user.setAge(rs.getInt("Age"));
40                 list.add(user);
41             }
42             rs.close();
43         } catch (Exception e{
44             e.printStackTrace();
45         }
46         return list;
47     }
48
49 }

The code are immigrated from "OracleTest3.java". Refer to 

Statement, PreparedStatement, CallableStatement


Actually, until now, the DAO + JDBC(Oracle) + SP are done. The left part is that we need to modify the code and connect all the parts together.


As a result, the web page will show one message with the input name and age, along with one table that contains all the name and age stored in the database.
We can see that the return data can also be wrapped with JavaBeans. One field is the message and the other field is the list of User. So first, we new "UserInfo.java" in package "com.company.beans", with code:
01 package com.company.beans;
02 import java.util.*;
03 
04 public class UserInfo {
05 
06     private String msg;
07     private List<User> users;
08     public String getMsg() {
09         return msg;
10     }
11     public void setMsg(String msg) {
12         this.msg = msg;
13     }
14     public List<User> getUsers() {
15         return users;
16     }
17     public void setUsers(List<User> users) {
18         this.users = users;
19     }    
20 } 


And we should modify "HelloService.java" to connect to DAO. We should also use the thought of Singleton. Code is like this:
01 package com.company.services;
02 import com.company.beans.*;
03 import com.company.daos.*;
04
05 public class HelloService {
06     private HelloDao hd;
07
08     public HelloService(){
09         //System.out.println("Create an instance of HelloService");
10         if(hd == null){
11             hd = new HelloDao();
12         }
13     }
14     public String sayHello1(User user){
15         StringBuffer sb = new StringBuffer();
16         sb.append("<HTML><body>");
17         sb.append("<h2><font color = blue>");
18         sb.append("Hello " + user.getName() + ", with age " + user.getAge() + ", welcome to J2EE!");
19         sb.append("</h2></font>");
20         sb.append("</body></HTML>");
21         return sb.toString();
22     }
23     public String sayHello2 (User user){
24         return "Hello " + user.getName() + "(" + user.getAge() + "), welcome to J2EE!";
25     }
26    
27     public UserInfo process(User user){
28         hd.save(user);
29         UserInfo userInfo = new UserInfo();
30         userInfo.setMsg(this.sayHello2(user));
31         userInfo.setUsers(hd.query());
32         return userInfo;
33     }
34 }



Now we change "HelloServlet.servlet" and "hello.jsp" to show the result page.
In "hello.jsp", we modify the method in form to "post" to test doPost function.


11 <form action = "HelloServlet" method="post">


In "HelloServlet.servlet", we add below code to doPost method:


1 protected void doPost(HttpServletRequest request, HttpServletResponse responsethrows ServletException, IOException{
2         // TODO Auto-generated method stub
3         User user = parse(request);
4         request.setAttribute("userInfo", hs.process(user));
5         String url = "/result.jsp";
6         RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher(url);
7         dispatcher.forward(request, response);
8     }



At last, we create "result.jsp" to show the result.


01 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
02     pageEncoding="ISO-8859-1"%>
03 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04 <html>
05 <head>
06 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
07 <title>Insert title here</title>
08 </head>
09 <body>
10 <%@page import = "com.company.beans.User" %>
11 <jsp:useBean id = "userInfo" scope= "request" class = "com.company.beans.UserInfo"/>
12
13     <h2><font color="blue"><jsp:getProperty name = "userInfo" property = "msg"/></font></h2>
14     <table width = "200" border = "1">
15     <tr>
16         <th>Name</th>
17         <th>Age</th>
18     </tr>
19     <%
20        for (User user:userInfo.getUsers()){
21            out.print("<tr>");
22            out.print("<td>" + user.getName() + "</td>");
23            out.print("<td>" + user.getAge() + "</td>");
24            out.print("</tr>");
25           
26        }
27     %>
28 </body>
29 </html>



Until now, we are almost done. We have connected all the parts.


JSP + Servlet + Service + DAO + JDBC (Oracle) + SP
The only two left parts are that, first, we do not have input validation, second, we should prevent manually out.print HTML tags. We should only out.print specific value. To realize this, we can use JSTL to display result.


1. Apply Validation
To make basic logic algorithm at the front end, we need to use JavaScript.
New "validate,js" under Web Content:
Right click on "Web Content" => New => Other => Web => JavaScript


Code:


01 function validate_required(field, alertMsg{  // Validate a required field
02     with (field{
03         if (value==null || value==""{
04             alert("Please fill in the '"+alertMsg+"' box.");
05             return false;
06         }
07         return true;
08     }
09 }
10 function validate_int(field, alertMsg{  // Validate an integer field
11     with (field{
12         var num = parseInt(value);
13         if (isNaN(num) || num<0{
14             alert(alertMsg);
15             return false;
16         }
17         return true;
18     }
19 }
20 function validate_Form(thisForm{   // Validate the form
21     with (thisForm{
22            /* code */
23         if(!validate_required(name, "Name")){
24            name.focus();
25            return false;
26         }
27         if(!validate_required(age, "Age")){
28            age.focus();
29            return false;
30         }
31         if(!validate_int(age, "Invalid age")){
32            age.focus();
33            return false;
34         }
35     }
36     return true;
37 }



Modify "hello.jsp": 


01 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
02     pageEncoding="ISO-8859-1"%>
03 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04 <html>
05 <head>
06 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
07 <title>Insert title here</title>
08 <script language="javascript" src="validate.js"></script>
09 </head>
10 <body>
11 <h2><font color="blue">Sample01: JSP + Servlet + Service + DAO + JDBC + SP</font></h2>
12 <form action="HelloServlet" method="post" onsubmit="return validate_Form(this);">
13     <table>
14         <tr>
15             <td>Name :</td>
16             <td><input type="text" name="name" /></td>
17         </tr>
18         <tr>
19             <td>Age :</td>
20             <td><input type="text" name="age" /></td>
21         </tr>
22         <tr>
23             <td></td>
24             <td>
25                 <input type="reset" value="Clear" />
26                 <input type="submit" value="Submit" />
27             </td>
28         </tr>
29     </table>
30 </form>
31 </body>
32 </html>



Now we can check the validation:




It works fine.


2. Apply JSTL


To use JSTL, we need to add external jar files.
Copy "c.tld" under "WEB-INF", "jstl.jar" and "standard.jar" under "WEB-INF=>lib"


Modify "Web.xml", add block:


01 <?xml version="1.0" encoding="UTF-8"?>
02 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
03   <display-name>Sample01</display-name>
04   <jsp-config>
05     <taglib>
06         <taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
07         <taglib-location>/WEB-INF/c.tld</taglib-location>
08       </taglib>
09   </jsp-config>

10   <welcome-file-list>
11     <welcome-file>index.html</welcome-file>
12     <welcome-file>index.htm</welcome-file>
13     <welcome-file>index.jsp</welcome-file>
14     <welcome-file>default.html</welcome-file>
15     <welcome-file>default.htm</welcome-file>
16     <welcome-file>default.jsp</welcome-file>
17   </welcome-file-list>
18   <servlet>
19     <description></description>
20     <display-name>HelloServlet</display-name>
21     <servlet-name>HelloServlet</servlet-name>
22     <servlet-class>com.company.servlets.HelloServlet</servlet-class>
23   </servlet>
24   <servlet-mapping>
25     <servlet-name>HelloServlet</servlet-name>
26     <url-pattern>/HelloServlet</url-pattern>
27   </servlet-mapping>
28 </web-app>



Modify "result.jsp":


01 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
02     pageEncoding="ISO-8859-1"%>
03 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04 <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
05 <html>
06 <head>
07 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
08 <title>Insert title here</title>
09 </head>
10 <body>
11 <%@page import = "com.company.beans.User" %>
12 <jsp:useBean id = "userInfo" scope= "request" class = "com.company.beans.UserInfo"/>
13
14     <h2><font color="blue">${userInfo.msg}</font></h2>
15     <table width = "200" border = "1">
16     <tr>
17         <th>Name</th>
18         <th>Age</th>
19     </tr>
20     <c:forEach var="user" items = "${userInfo.users}">
21         <tr>
22             <td>${user.name}</td>
23             <td>${user.age}</td>
24         </tr>
25     </c:forEach>

26     </table>
27 </body>
28 </html>



Until now, all the codes are done.
Result:


We successfully realize :


JSP + Servlet + Service + DAO + JDBC (Oracle) + SP

No comments:

Post a Comment