📄 userformservlet.java
字号:
/*
* @(#)UserFormServlet.java
*
* Copyright (C) 2007 Sergey Bredikhin
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package olivax.webmail;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class UserFormServlet extends HttpServlet {
static final long serialVersionUID = 3335075694621323006L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = null;
try {
request.setCharacterEncoding(Context.clntCp);
response.setContentType("text/html; charset=" + Context.clntCp);
out = response.getWriter();
HttpSession session = request.getSession();
Context myContext = (Context) session.getAttribute("myContext");
if(myContext == null) {
myContext = new Context();
session.setAttribute("myContext", myContext);
}
if(!myContext.isAdminLoggedIn()) {
Context.forward(this, request, response, "/admin");
}
myContext.setNoCache(response);
// Get parameters
String id = "";
String name = "";
String comments = "";
String action = request.getParameter("action");
if(action == null)
action = "";
if(action.equalsIgnoreCase("insert") || action.equalsIgnoreCase("update") ||
action.equalsIgnoreCase("edit") || action.equalsIgnoreCase("delete"))
{
id = request.getParameter("id");
name = request.getParameter("name");
comments = request.getParameter("comments");
if(id == null)
id = "";
if(name == null)
name = "";
if(comments == null)
comments = "";
try {
myContext.storage.logon();
olivax.webmail.User user = new olivax.webmail.User();
if(action.equalsIgnoreCase("insert") || action.equalsIgnoreCase("update")) {
if(action.equalsIgnoreCase("update"))
user.id = id;
user.name = name;
user.comments = comments;
myContext.storage.saveUser(user);
Context.forward(this, request, response, "/user_form?action=new");
} else if(action.equalsIgnoreCase("delete")) {
myContext.storage.deleteUser(id);
Context.forward(this, request, response, "/user_list");
} else if(action.equalsIgnoreCase("edit")) {
user = myContext.storage.getUser(id);
name = user.name;
comments = user.comments;
}
} finally {
myContext.storage.close();
}
}
// Main page
StringBuffer sb = new StringBuffer();
sb.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
sb.append("<html>\r\n");
sb.append("<head>\r\n");
sb.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=").append(Context.clntCp).append("\">\r\n");
// sb.append("<meta http-equiv=\"Content-Language\" content=\"ru-RU\">\r\n");
sb.append("<meta HTTP-EQUIV=\"PRAGMA\" content=\"NO-CACHE\">\r\n");
sb.append("<title>\r\n");
sb.append(myContext.getI18nString("_str_user_form_title")).append("\r\n");
sb.append("</title>\r\n");
sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"./oliva.css\">\r\n");
sb.append("</head>\r\n");
sb.append("<body link=\"#000033\" alink=\"#000033\" vlink=\"#000033\">\r\n");
out.print(sb.toString());
//Logo
WebPage webPage = new WebPage();
webPage.addPane(myContext.getWebLogo());
//Title
WebPane webPane = new WebPane();
webPane = new WebPane();
webPane.setColor(WebPane.COLOR_CCCCCC);
webPane.setWidth("100%");
webPane.setHeight("35");
webPane.setContent("<font size=\"3\" face=\"arial, tahoma, verdana\"><strong>" +
myContext.getI18nString("_str_user_form_title") + "</strong></font>");
webPage.addPaneToSameRow(webPane);
//Sublogo
webPane = new WebPane();
webPane.setColor(WebPane.COLOR_CCFF99);
webPane.setWidth("20%");
webPane.setHeight("35");
webPane.setContent("<font size=\"4\" color=\"#999966\" face=\"arial, tahoma, verdana\"><strong><i>" +
myContext.getSenderDomain() + "</i></strong></font>");
webPage.addPaneToSameRow(webPane);
//Menu
sb = new StringBuffer();
sb.append("<font size=\"2\">\r\n");
sb.append("<a href=\"./user_form?action=search\">").append(myContext.getI18nString("_str_user_search_link")).append("</a> \r\n");
sb.append("<a href=\"./user_form?action=new\">").append(myContext.getI18nString("_str_user_new_link")).append("</a> \r\n");
sb.append("<a href=\"./dom_form?action=search\">").append(myContext.getI18nString("_str_dom_search_link")).append("</a> \r\n");
sb.append("<a href=\"./schema\">").append(myContext.getI18nString("_str_schema_link")).append("</a> \r\n");
sb.append("<a href=\"./admin\">").append(myContext.getI18nString("_str_admin_link")).append("</a> \r\n");
sb.append("<a href=\"./admin?action=logout\">").append(myContext.getI18nString("_str_admin_exit_link")).append("</a> \r\n");
sb.append("</font>\r\n");
WebPane menuPane = new WebPane();
menuPane.setColor(WebPane.COLOR_CCCC99);
menuPane.setContent(sb.toString());
webPage.addPane(menuPane);
// Main pane
sb = new StringBuffer();
if(action.equalsIgnoreCase("search")) {
sb.append("<form action=\"./user_list\" method=\"post\">");
sb.append("<input type=\"hidden\" name=\"action\" value=\"search\">");
} else {
sb.append("<form action=\"./user_form\" method=\"post\">");
}
if(action.equalsIgnoreCase("new")) {
sb.append("<input type=\"hidden\" name=\"action\" value=\"insert\">");
} else if(action.equalsIgnoreCase("edit")) {
sb.append("<input type=\"hidden\" name=\"action\" value=\"update\">");
sb.append("<input type=\"hidden\" name=\"id\" value=\"").append(id).append("\">");
}
sb.append("<table border=\"0\" cellspacing=\"1\" cellpadding=\"1\" class=\"basic\">");
sb.append("<tr>");
sb.append("<td class=\"hdr\" bgcolor=\"#ccccff\" width=\"110\"><font size=\"2\">").append(myContext.getI18nString("_str_user_name")).append(":</font></td>");
sb.append("<td class=\"data\" bgcolor=\"#eeeeff\">");
sb.append("<input type=\"text\" name=\"name\" size=\"40\" value=\"").append(name).append("\">");
sb.append("</td>");
sb.append("</tr>");
sb.append("<tr>");
sb.append("<td class=\"hdr\" bgcolor=\"#ccccff\" width=\"110\"><font size=\"2\">").append(myContext.getI18nString("_str_user_comment")).append(":</font></td>");
sb.append("<td class=\"data\" bgcolor=\"#eeeeff\">");
sb.append("<input type=\"text\" name=\"comments\" size=\"40\" value=\"").append(comments).append("\">");
sb.append("</td>");
sb.append("</tr>");
if(action.equalsIgnoreCase("search")) {
sb.append("<tr>");
sb.append("<td class=\"hdr\" bgcolor=\"#ccccff\" width=\"110\"><font size=\"2\">").append(myContext.getI18nString("_str_user_sort_order")).append(":</font></td>");
sb.append("<td class=\"data\" bgcolor=\"#eeeeff\">");
sb.append("<select name=\"sort_order\">");
sb.append("<option value=\"name\" selected>").append(myContext.getI18nString("_str_user_name")).append("</option>");
sb.append("<option value=\"comments\">").append(myContext.getI18nString("_str_user_comment")).append("</option>");
sb.append("</select>");
sb.append("</td>");
sb.append("</tr>");
}
sb.append("</table><br>");
if(action.equalsIgnoreCase("search")) {
sb.append("<input type=\"submit\" value=\"").append(myContext.getI18nString("_str_user_search")).append("\">");
} else {
sb.append("<input type=\"submit\" value=\"").append(myContext.getI18nString("_str_user_save")).append("\">");
}
sb.append("</form>");
webPane = new WebPane();
webPane.setColor(WebPane.COLOR_9999CC);
webPane.setHeight("200");
webPane.setContent(sb.toString());
webPage.addPane(webPane);
//Menu
webPage.addPane(menuPane);
webPage.show(out);
sb = new StringBuffer();
sb.append("</body>\r\n");
sb.append("</html>\r\n");
out.print(sb.toString());
out.flush();
out.close();
} catch (Exception e) {
try {
Context.sendErrorRedirect(this, request, response, "/error", e);
} catch (Exception exc) {
exc.printStackTrace();
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -