package servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; publicclass OperationName extends HttpServlet ......{ /** *//***//** *//** * Constructor of the object. */ public OperationName() ......{ super(); } /** *//***//** *//** * Destruction of the servlet. <br> */ publicvoid 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 */ publicvoid doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException ......{ String names = request.getParameter("names"); Collection students =null; Connection con =null; Statement st =null; ResultSet rs =null; try ......{ Class.forName("org.gjt.mm.mysql.Driver"); con = DriverManager.getConnection("jdbc:mysql:"+ "//127.0.0.1:3306/test?useUnicode=true&characterEncoding=GBK","root","eetrust"); st = con.createStatement(); rs = st.executeQuery("select count(*) from test where name=''"+names+"''"); StringBuffer result =new StringBuffer(); result.append("<OperationNames>"); System.out.println(result); while(rs.next()) ......{ int num = rs.getInt(1); result.append("<OperationName>"+ num +"</OperationName>"); } result.append("</OperationNames>"); String responseHtml = result.toString(); response.setCharacterEncoding( "UTF-8"); response.setContentType("text/xml;charset=UTF-8"); response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires", 0); response.getWriter().write(responseHtml); }catch(Exception e) ......{ e.printStackTrace(); }finally ......{ try ......{ rs.close(); st.close(); con.close(); }catch(Exception e) ......{ e.printStackTrace(); } } } /** *//***//** *//** * 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 */ publicvoid doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException ......{ this.doGet(request,response); } /** *//***//** *//** * Initialization of the servlet. <br> * * @throws ServletException if an error occure */ publicvoid init() throws ServletException ......{ // Put your code here } }