Aplikacja WWW - projekt - serwlety - Java Server Pages - 05.10.2009/03.10.2009
Utworzyć nowy projekt Web Application w środowisku NetBeans
W katalogu roboczym PSS_Project / Web umieścić (skopiować) pliki:
BookEdit.jsp,
BookList.jsp, BookAdd.jsp, index.jsp, Example.html
Dołączyć do projektu serwlety, podając lokalizację w pakiecie Source Packages / <default package>
Dołączyć serwlety wykonując wpisy w pliku web.xml (PSS_Project / Web / web-inf/ web.xml
<?xml version="1.0"
encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>NewServlet</servlet-name>
<servlet-class>NewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NewServlet</servlet-name>
<url-pattern>/NewServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
Możliwe jest ponadto edytowanie pliku web.xml w edytorze NetBeans:
Sposób dodania nowego serwletu:
Po utworzeniu nowego serwletu o nazwie NewServlet, projekt powinien mieć następujące wpisy:
Utworzyć nowy pakiet Javy o nazwie pss:
Dodać do projektu serwlet - BookEdit w podany powyżej:
Serwlet umieścić w pakiecie o nazwie pss.
Dodać w podany sposób następujące pliki z kodem serwletu - BookEdit oraz klasami Javy (pozostałe pliki).
Skompilować pliki z Source Packages - pss
Kod
BookEdit:
<html>
<head>
<title>Book add page</title>
</head>
<body>
<form name="edit" action="BookEdit" method="post">
<table border="1"> <tbody>
<tr>
<td>Author:</td>
<td>
<input type="text" name="author" value="">
</td>
</tr><tr>
<td>Title:</td>
<td><input type="text" name="title" value=""></td>
</tr><tr>
<td>Available:</td>
<td>
<input type="checkbox" name="available" value="true">
</td>
</tr><tr> <td colspan="2">
<input type="submit" name="btnSave" value="Save">
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
/*
* BookEdit.java
*
* Created
*/
/*
* Created
*/
package pss;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* @author
*/
public class BookEdit extends HttpServlet {
/**
* Constructor of the object.
*/
public BookEdit() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//get session of the request
HttpSession session = request.getSession();
//init the simulate DB
SimulateDB simulateDB = new SimulateDB();
//get parameter do of the request
String action = request.getParameter("do");
//get the id of the request
Long id = null;
if(request.getParameter("id") != null)
id = Long.valueOf(request.getParameter("id"));
//add / edit book
if(action.equals("add")){
//get the request dispatcher
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/BookAdd.jsp");
//forward to the jsp file to display the book list
dispatcher.forward(request, response);
}else if(action.equals("edit")){
//get the book from simulated DB
Book book = new Book();
if(id != null)
book = simulateDB.loadBookById(id.longValue(), session);
//set the book objekt in the request
request.setAttribute("book", book);
//get the request dispatcher
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/BookEdit.jsp");
//forward to the jsp file to display the book list
dispatcher.forward(request, response);
//delete book
}else if(action.equals("delete")){
//delete the book by id
simulateDB.deleteBookById(id.longValue(), session);
//redirect to the book list servlet
response.sendRedirect(request.getContextPath() + "/BookList.jsp");
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//get session of the request
HttpSession session = request.getSession();
//init the simulate DB
SimulateDB simulateDB = new SimulateDB();
//get book properties from the request
long id = 0;
try{ id = Long.parseLong(request.getParameter("id")); }
catch(NumberFormatException e){}
String author = request.getParameter("author");
String title = request.getParameter("title");
Boolean available = Boolean.valueOf(request.getParameter("available"));
//create a new book object
Book book = new Book(id, author, title, available.booleanValue());
//save the book to DB
simulateDB.saveToDB(book, session);
//redirect to the book list servlet
response.sendRedirect(request.getContextPath() + "/BookList.jsp");
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
Kod BookList.jsp:
<%@ page language="java" import="java.util.*" %>
<%@ page import="pss.*" %>
<html>
<head>
<title>Book list page</title>
</head>
<body>
<table border="1">
<tbody> <tr>
<td>ID</td>
<td>Author</td>
<td>Book name</td>
<td>Available</td>
<td> </td>
<td> </td>
</tr>
<%
HttpSession S = request.getSession();
ArrayList arrayList = (ArrayList)S.getAttribute("bookDB");
if(arrayList != null)
for (Iterator iter = arrayList.iterator(); iter.hasNext();)
{
Book element = (Book) iter.next();
out.println("<tr>");
out.println("<td>" + element.getId()+ "</td>");
out.println("<td>" + element.getAuthor() + "</td>");
out.println("<td>" + element.getTitle() + "</td>");
if(element.isAvailable())
out.println("<td><input type=checkbox name=available value=true disabled checked></td>");
else
out.println("<td><input type=checkbox name=available value=true disabled></td>");
out.println("<td><a href=BookEdit?do=edit&id=" + element.getId() + ">Edit</a></td>");
out.println("<td> <a href=BookEdit?do=delete&id=" + element.getId() + ">Delete</a></td>");
out.println("</tr>");
}
%>
</tbody>
</table>
<br>
<a href="BookEditExt" method=post>Add a new book</a>
</body></html>
Zadanie I
Przeanalizować kod wypisujący istniejące książki w bazie danych - SimulateDB, sprawdzić sposób edycji
Zadanie II
Dodać kod zapisujący informacje o
użytkownikach, osobach, produktach w podobny sposób - klasy Person,
Address, Customer, Product.
Zadanie III
Napisać program w postaci serwletu:
- pobierającego od użytkownika liczbę kolumn,
wierszy, wartość min /max (lewy panel),
- w panelu górnym wyświetlający w postaci
tabeli zadane liczby losowe
- w panelu dolnym wyświetlający w postaci listy zadane liczby losowe
Pliki: