建立一个 Derby 日历,第 2 部分: 嵌入选项(下)
2010-04-19 00:00:00 来源:WEB开发网和 GUI 中一样,也将页面分成左右两部分。左侧用简单的 HTML 表单输入事件。编译该 servlet 并将类文件放到类目录中,然后用浏览器打开:
http://www.yourserverhere.com/calendar/calendar
可以看到类似 图 9 的结果。
图 9. 添加事件信息
保存新事件
和登录 servlet 一样,使用 POST 方法处理输入,如 清单 24 所示。
清单 24. 调用事件 servlet 类
...
public final class MainFrame extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
...
}
private void leftSide(HttpServletRequest request,
HttpServletResponse response,
String username, String password)
throws IOException {
...
}
private void rightSide(HttpServletRequest request,
HttpServletResponse response,
String username, String password)
throws IOException {
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
String username = null;
String password = null;
Cookie[] cookies = request.getCookies();
for(int i=0; i<cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookie.getName().equals("username")){
username = cookie.getValue();
} else if (cookie.getName().equals("password")){
password = cookie.getValue();
}
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
int month = Integer.parseInt(request.getParameter("month"));
int day = Integer.parseInt(request.getParameter("day"));
int year = Integer.parseInt(request.getParameter("year"));
String title = request.getParameter("title");
String description = request.getParameter("description");
String remindersTo = request.getParameter("remindersTo");
EventClass newEvent = new EventClass(title, description,
remindersTo, month, day, year,
username, password);
doGet(request, response);
}
}
更多精彩
赞助商链接