<<<<<<<<<<<<<测试HbntTestSvlt.java>>>>>>>>>>>>>>>>>>>>>>>
<<<<<<<<<<<<<该servlet使用时的web.xml>>>>>>>>>>>>>>>>>>>>>>>/**//*
*
* @(#)CorpSMS.corpsms V1.0 2005-1-15
* Copyright 2003 NetEase, Inc. All rights reserved.
*
* coder: sweater
* email: wtshi@corp.netease.com
*
* graphic designer:
* email:
*
* fuction:向电话薄中添加组和电话号码
*
*/
![]()
package com.netease.wireless.groupsms.vo;
![]()
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
![]()
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
![]()
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
![]()
import com.netease.wireless.groupsms.hbnt.po.Cat;
import com.netease.wireless.groupsms.hbnt.util.HibernateUtil;
/**//**
*
* @author sweater
*/
public class HbntTestSvlt extends HttpServlet ...{
![]()
/**//**
* Constructor of the object.
*/
public HbntTestSvlt() ...{
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 ...{
![]()
response.setContentType("text/html");
PrintWriter out = response.getWriter();
![]()
out.println("<html>");
out.println("<head>");
out.println("<title>Hello Hibernate</title>");
out.println("</head>");
out.println("<body>");
out.println("Hello Hibernate!<br>");
![]()
try ...{
Session session = HibernateUtil.currentSession();
![]()
Transaction tx = session.beginTransaction();
![]()
Query query = session.createQuery("select cat from Cat as cat where cat.sex = :sex");
query.setCharacter("sex", 'F');
![]()
for (Iterator it = query.iterate(); it.hasNext();) ...{
Cat cat = (Cat) it.next();
out.println("Cat: " + cat.toString() + "<br>");
}
![]()
tx.commit();
![]()
HibernateUtil.closeSession();
} catch (HibernateException e) ...{
System.out.println(e.getMessage());
//System.out.println(e.printStackTrace());
}
![]()
out.println("</body>");
out.println("</html>");
}
![]()
/**//**
* 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 ...{
![]()
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
![]()
/**//**
* Returns information about the servlet, such as
* author, version, and copyright.
*
* @return String information about this servlet
*/
public String getServletInfo() ...{
return "This is my default servlet created by Eclipse";
}
![]()
/**//**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException ...{
// Put your code here
}
![]()
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<description>hbnttest</description>
<display-name>hbnttest</display-name>
<servlet-name>HbntTestSvlt</servlet-name>
<servlet-class>com.netease.wireless.groupsms.vo.HbntTestSvlt</servlet-class>
</servlet>
![]()
<servlet-mapping>
<servlet-name>HbntTestSvlt</servlet-name>
<url-pattern>/servlet/HbntTestSvlt</url-pattern>
</servlet-mapping>
![]()
</web-app>
