Листинг 7.5. Код метода doGet (QuestionnaireServ.java)
public void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); PrintWriter out = new PrintWriter(resp.getOutputStream()); String qname = req.getParameter("qname") ; // System.out.println("Start doGet"); if( qname == null || qname.length() == 0 ){ errorMsg( out, "Bad QNAME data", null); return; } // MUST have qname = name of xml file String src = qProp.getProperty( qname ); if( src == null ) { errorMsg( out, "Bad QNAME lookup", null ); return ; } String userid = "unknown" ; // customer or student id or unknown String tmp = req.getParameter("userid"); if( tmp != null ) userid = tmp; String usertype = "unknown" ; // "student" "customer" etc etc tmp = req.getParameter("usertype"); if(tmp != null ) usertype = tmp ; DOMlibrary lib = DOMlibrary.getLibrary(); System.out.println("DOMlibrary initialized, try for " + src ); Document doc = lib.getDOM( src ); if( doc == null ){ errorMsg( out, "DOM doc failed - unable to continue", null ); return ; } HttpSession session = req.getSession( true ); // if not new must be re-entering - could recover here Interpreter terpret = new Interpreter( doc, handler ); session.putValue( "xmldocument", terpret ); // session.setAttribute("xmldocument",terpret ); // the putValue method was used in the 2.1 API but is now // a deprecated method, // Recorder rb = new Recorder(userid, usertype, session.getId(), src ); rb.setMethods( doc ); session.putValue("recorder", rb ); //session.setAttribute("recorder", rb ); try { // terpret.doIntro( out ); // includes head and Form footer( out ); }catch(Exception e){ errorMsg( out, "doGet ", e ); }
После введения в анкету, как видно из листинга 7.6, все запросы и ответы проходят через метод doPost. После извлечения из объекта HttpSession объектов Interpreter и Recorder данные запроса собираются и с помощью объектов Interpreter и Recorder генерируется ответ.