7.14号学习itcast ajax视频 笔记5

sevelet 处理纯文本和xml的不同:最大的不同在于返回的格式,纯文本直接用out输出,
而xml需要对数据进行xml格式封装才能输出.
纯文本:

package zhc.love.dj;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2009-7-10
 * Time: 9:20:53
 * To change this template use File | Settings | File Templates.
 */
public class AjaxTest extends HttpServlet{
    protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
           try {

               httpServletResponse.setContentType("text/html;charset=utf-8");
               PrintWriter out =   httpServletResponse.getWriter();
               String old =    httpServletRequest.getParameter("name");
               if(old == null || old.length() == 0){
                   out.print("用户名不能为空");
               } else{
                   String name = old ;
                   if(name.equals("zhc")){
                       System.out.println(name);
                       out.println("用户名已经存在");
                   }else{
                       out.println("用户名可以注册");
                   }
               }


           }catch(Exception e){
               e.printStackTrace();
           }
    }

    protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        doGet(httpServletRequest,httpServletResponse);

    }
}

 

     2.xml

  

   

package zhc.love.dj;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2009-7-14
 * Time: 13:58:45
 * To change this template use File | Settings | File Templates.
 */
public class AjaxXMLTest extends HttpServlet {
    //跟接收文本信息基本差不多,只是最后接受语句有差别
     protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
           try {
               //修改处1==: text/html-->text/xml
               httpServletResponse.setContentType("text/xml;charset=utf-8");
               PrintWriter out =   httpServletResponse.getWriter();
               String old =    httpServletRequest.getParameter("name");
                //修改点2==:返回的格式需要拼装成xml格式
                   StringBuilder builder = new StringBuilder();
                   builder.append("<message>");
               if(old == null || old.length() == 0){
                   builder.append("用户名不能为空").append("</message>");
                   //out.print("用户名不能为空");
               } else{
                    String name = old ;
                   if(name.equals("zhc")){
                       //System.out.println(name);
                       builder.append("用户名已经存在").append("</message>");
                      // out.println("用户名已经存在");
                   }else{
                        builder.append("用户名可以注册").append("</message>");
                       //out.println("用户名可以注册");
                   }

               }
                out.println(builder.toString());

           }catch(Exception e){
               e.printStackTrace();
           }
    }

    protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        doGet(httpServletRequest,httpServletResponse);

    }
}

 

你可能感兴趣的:(html,Ajax,xml,servlet,idea)