EventManager web 应用

首先,写一个servlet:

public class EventManagerServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		SimpleDateFormat dateFormatter = new SimpleDateFormat("dd.MM.yyyy");

		try {
			// Begin unit of work
			HibernateUtil.getSessionFactory().getCurrentSession()
					.beginTransaction();

			// Process request and render page...

			// Write HTML header
			PrintWriter out = response.getWriter();
			out.println("<html><head><title>Event Manager</title></head><body>");

			// Handle actions
			if ("store".equals(request.getParameter("action"))) {

				String eventTitle = request.getParameter("eventTitle");
				String eventDate = request.getParameter("eventDate");

				if ("".equals(eventTitle) || "".equals(eventDate)) {
					out.println("<b><i>Please enter event title and date.</i></b>");
				} else {
					createAndStoreEvent(eventTitle,
							dateFormatter.parse(eventDate));
					out.println("<b><i>Added event.</i></b>");
				}
			}

			// Print page
			printEventForm(out);
			listEvents(out, dateFormatter);

			// Write HTML footer
			out.println("</body></html>");
			out.flush();
			out.close();

			// End unit of work
			HibernateUtil.getSessionFactory().getCurrentSession()
					.getTransaction().commit();
		} catch (Exception ex) {
			HibernateUtil.getSessionFactory().getCurrentSession()
					.getTransaction().rollback();
			if (ServletException.class.isInstance(ex)) {
				throw (ServletException) ex;
			} else {
				throw new ServletException(ex);
			}
		}
	}

	private void listEvents(PrintWriter out, SimpleDateFormat dateFormatter) {
		// TODO Auto-generated method stub
		List result = HibernateUtil.getSessionFactory().getCurrentSession()
				.createCriteria(Event.class).list();
		if (result.size() > 0) {
			out.println("<h2>Events in database:</h2>");
			out.println("<table border='1'>");
			out.println("<tr>");
			out.println("<th>Event title</th>");
			out.println("<th>Event date</th>");
			out.println("</tr>");
			Iterator it = result.iterator();
			while (it.hasNext()) {
				Event event = (Event) it.next();
				out.println("<tr>");
				out.println("<td>" + event.getTitle() + "</td>");
				out.println("<td>" + dateFormatter.format(event.getDate())
						+ "</td>");
				out.println("</tr>");
			}
			out.println("</table>");
		}

	}

	private void printEventForm(PrintWriter out) {
		// TODO Auto-generated method stub
		out.println("<h2>Add new event:</h2>");
		out.println("<form>");
		out.println("Title: <input name='eventTitle' length='50'/><br/>");
		out.println("Date (e.g. 24.12.2009): <input name='eventDate' length='10'/><br/>");
		out.println("<input type='submit' name='action' value='store'/>");
		out.println("</form>");

	}

	private void createAndStoreEvent(String eventTitle, Date parse) {
		// TODO Auto-generated method stub
		Event theEvent = new Event();
		theEvent.setTitle(eventTitle);
		theEvent.setDate(parse);

		HibernateUtil.getSessionFactory().getCurrentSession().save(theEvent);

	}

}

同时,修改web.xml文件:

添加如下内容:

<servlet>
        <servlet-name>Event Manager</servlet-name>
        <servlet-class>cjq.hibernate.tutorial.web.EventManagerServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Event Manager</servlet-name>
        <url-pattern>/eventmanager</url-pattern>
    </servlet-mapping>
  
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>


此时,在tomcat下运行,会出现报错:

java.lang.NoClassDefFoundError: Could not initialize class util.HibernateUtil

网上查一下,是映射关系配置文件不正确,再反思一下自己的代码,之前Person类与Event类有建立一些关联,

然后,把所有的关联关系去掉,再重新发布,就不会有错了。

你可能感兴趣的:(EventManager web 应用)