Xây dựng Project

Một phần của tài liệu Nghiên cứu các mô hình dịch vụ trong điện toán đám mây (Trang 59 - 65)

4. Xây dựng Project chạy trên đám mây của Google

4.2. Xây dựng Project

Trên nền tảng Eclipse và cụ thể ở đây là phiên bản Eclipse 4.4 (Luna) xây dựng project có tên QLDB ứng dụng vào việc quản lý danh bạ. Project sẽ có cấu trúc nhƣ sau:

Thƣ mục có tên QLDB đƣợc tạo ra chứa các file của ứng dụng:

- Thƣ mục src để chứa các mã nguồn java và các file xml cấu hình. - Thƣ mục war để chứa các file class đƣợc biên dịch từ file nguồn java. - Các file jar của bộ thƣ viện.

Trong thƣ mục war có chứa file query_result.jsp là file giao diện chính của ứng dụng để ngƣời sử dụng tƣơng tác có nội dung code nhƣ sau:

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

<%@ page import="java.util.*"%>

<%@ page import="edu.dartmouth.cs.gae_sample.data.*"%> <html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>QLDB</title>

<body> <%

String retStr = (String) request.getAttribute("_retStr"); if (retStr != null) { %> <%=retStr%><br> <% } %> <center> <b>TIM KIEM</b>

<form name="input" action="/query.do" method="get">

Ten: <input type="text" name="name"> <input type="submit" value="OK"> </form> </center> <b> --- ----<br> <%

ArrayList<Contact> resultList = (ArrayList<Contact>) request

.getAttribute("result"); if (resultList != null) {

for (Contact contact : resultList) { %> Ten:<%=contact.mName%>,&nbsp

Dia chi:<%=contact.mAddress%>,&nbsp

Sdt:<%=contact.mPhoneNumber%>&nbsp; &nbsp;&nbsp; <a href="/delete.do?name=<%=contact.mName%>">Xoa</a> <br> <% } } %> --- ----<br>

</b> QUAN LY DANH BA: <br>

<form name="input" action="/add.do" method="post"> Ten: <input type="text" name="name">

Dia chi: <input type="text" name="addr"> Sdt: <input type="text" name="phone"> <input type="submit" value="Them"> </form>

--- <br>

<form name="input" action="/update.do" method="post"> Ten: <input type="text" name="name">

Dia chi: <input type="text" name="addr"> Sdt: <input type="text" name="phone"> <input type="submit" value="Cap nhat"> </form>

</body> </html>

File AddServlet.java có chức năng thêm vào dữ liệu danh bạ một tên mới. Trƣớc khi thêm vào sẽ so sánh nếu giá trị của name=null thì sẽ quay về giao diện

ban đầu và đƣa ra thông báo “Ten khong hop le”. Ngƣợc lại sẽ lƣu tên mới và thông báo thành công. AddServlet.java có nội dung:

package edu.dartmouth.cs.gae_sample; import java.io.IOException; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import edu.dartmouth.cs.gae_sample.data.Contact; import edu.dartmouth.cs.gae_sample.data.ContactDatastore; public class AddServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

String name = req.getParameter("name"); String addr = req.getParameter("addr"); String phone = req.getParameter("phone"); if (name == null || name.equals("")) {

req.setAttribute("_retStr", "Ten khong hop le"); getServletContext().getRequestDispatcher("/query_result.jsp")

.forward(req, resp); return;

}

Contact contact = new Contact(name, addr, phone); boolean ret = ContactDatastore.add(contact); if (ret) {

req.setAttribute("_retStr", "Them " + name + " thanh cong");

ArrayList<Contact> result = new ArrayList<Contact>(); result.add(contact);

req.setAttribute("result", result); } else {

req.setAttribute("_retStr", name + " da thoat"); }

getServletContext().getRequestDispatcher("/query_result.jsp").forward( req, resp);

}

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

doGet(req, resp); }

File DeleteServlet.java thực hiện lệnh xóa một tên trong dữ liệu danh bạ: package edu.dartmouth.cs.gae_sample; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import edu.dartmouth.cs.gae_sample.data.ContactDatastore; public class DeleteServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

String name = req.getParameter("name"); ContactDatastore.delete(name);

resp.sendRedirect("/query.do"); }

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

doGet(req, resp); }

}

File QueryServlet.java thực hiện chức năng tìm kiếm của ứng dụng:

package edu.dartmouth.cs.gae_sample; import java.io.IOException; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import edu.dartmouth.cs.gae_sample.data.Contact; import edu.dartmouth.cs.gae_sample.data.ContactDatastore; public class QueryServlet extends HttpServlet {

/** * */

private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

String name = req.getParameter("name");

getServletContext().getRequestDispatcher("/query_result.jsp").forward( req, resp);

}

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

doGet(req, resp); }

}

File UpdateServlet.java thực hiện lệnh cập nhật lại thông tin của một đối

tƣợng đã có tên trong dữ liệu danh bạ và hiển thị thông tin mới. Nếu tên nhập vào chƣa có trong dữ liệu danh bạ thì sẽ quay về giao diện hiển thị danh sách các tên đã có trong dữ liệu: package edu.dartmouth.cs.gae_sample; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import edu.dartmouth.cs.gae_sample.data.Contact; import edu.dartmouth.cs.gae_sample.data.ContactDatastore; public class UpdateServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

String name = req.getParameter("name"); String addr = req.getParameter("addr"); String phone = req.getParameter("phone"); if (name != null && !name.equals("")) {

Contact contact = new Contact(name, addr, phone); ContactDatastore.update(contact);

}

resp.sendRedirect("/query.do"); }

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

doGet(req, resp); }

Một phần của tài liệu Nghiên cứu các mô hình dịch vụ trong điện toán đám mây (Trang 59 - 65)