JAVA AJAX教程第四章—AJAX和MVC的简单结合
2009-10-27 00:00:00 来源:WEB开发网好了,最关键也是最重要的部分Servlet,也就是MVC中C他都来干嘛呢,哦,原来他是用来读取数据,并将数据写成XML发送回客户端。
package classmate;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TipServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("UTF-8");
Integer key = Integer.valueOf(req.getParameter("key"));
DB db = new DB();
Product product = null;
try{
product = Product.GetDetail(db,key);
}catch(Exception e){
e.printStackTrace();
}
resp.setContentType("text/xml;charset=UTF-8");
resp.setHeader("Cache-Control", "no-cache");
PrintWriter out = resp.getWriter();
out.println("<response>");
out.println("<content>" + product.getContents() + "</content>");
out.println("</response>");
//System.out.println("<response><content>" + product.getContents() + "</content></response>");
out.close();
}
@Override
public void init(ServletConfig config) throws ServletException {}
}
完成这些,我们就完成了所有的代码了,大家一起试试看,是不是可以了。一个简单AJAX实例完成了,同时他也是一个MVC框架的。
最终效果如:
出处:http://sidneyyoung.blog.51cto.com/932743/212466
更多精彩
赞助商链接