@@ -0,0 +1,2245 @@
+package cn.itcast.servlet;%0A%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.text.SimpleDateFormat;%0Aimport java.util.Date;%0A%0Aimport javax.servlet.ServletException;%0Aimport javax.servlet.http.HttpServlet;%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0A%0Aimport org.dom4j.Document;%0Aimport org.dom4j.Element;%0Aimport org.dom4j.io.OutputFormat;%0Aimport org.dom4j.io.SAXReader;%0Aimport org.dom4j.io.XMLWriter;%0A%0Apublic class MyServlet extends HttpServlet %7B%0A%0A%09public void doGet(HttpServletRequest request, HttpServletResponse response)%0A%09%09%09throws ServletException, IOException %7B%0A%09%09request.setCharacterEncoding(%22UTF-8%22);%0A%09%09String userName = request.getParameter(%22userName%22);%0A%09%09String words = request.getParameter(%22words%22);%0A%09%09if (userName==%22%22%7C%7Cwords==%22%22) %7B%0A%09%09%09response.sendRedirect(%22/personweb/index.html%22);%0A%09%09%7D else %7B%0A%09%09%09try %7B%0A%09%09%09%09storage(userName, words);%0A%09%09%09%7D catch (Exception e) %7B%0A%09%09%09%09%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A%09%09%09//response.setStatus(303);%0A%09%09%09//response.setHeader(%22location%22, %22/personweb/index.html%22);%0A%09%09%09response.sendRedirect(%22/personweb/refresh.html%22);%0A%09%09%7D%0A%09%7D%0A%0A%09public void doPost(HttpServletRequest request, HttpServletResponse response)%0A%09%09%09throws ServletException, IOException %7B%0A%0A%09%09doGet(request, response);%0A%09%7D%0A%09/**%0A%09 * %CD%BB%CF%A2%D0%B4xml%C4%BC%0A%09 * dom4j%CA%BD%D0%B6%D0%B4%0A%09 * @param userName%0A%09 * @param words%0A%09 * @throws Exception%0A%09 */%0A%09public void storage(String userName,String words) throws Exception %7B%0A%09%09Date d = new Date();%0A%09%09SimpleDateFormat sdf = new SimpleDateFormat(%22yyyyMMdd HH:mm:ss%22);%0A%09%09String date = sdf.format(d);%0A%09%09SAXReader reader = new SAXReader();%0A%09%09Document document = reader.read(%22../webapps/personweb/xml/data.xml%22);%0A%09%09Element root = document.getRootElement();%0A%09%09Element user = root.addElement(%22user%22);%0A%09%09Element username = user.addElement(%22username%22);%0A%09%09username.setText(userName);%0A%09%09Element userwords = user.addElement(%22userwords%22);%0A%09%09userwords.setText(words);%0A%09%09Element time = user.addElement(%22time%22);%0A%09%09time.setText(date);%0A%09%09OutputFormat prettyFormat = OutputFormat.createPrettyPrint();%0A%09%09prettyFormat.setEncoding(%22UTF-8%22);%0A%09%09XMLWriter writer = new XMLWriter(new FileOutputStream(%22../webapps/personweb/xml/data.xml%22), prettyFormat);%0A%09%09writer.write(document);%0A%09%09writer.close();%0A%09%7D%0A%0A%7D%0A

add sevlet