Server Java 四 :如何在用JSF在服务器端处理Ajax请求
来源:互联网 发布:c语言的架构 编辑:程序博客网 时间:2024/06/05 19:21
Server Java 四 :如何在用JSF在服务器端处理Ajax请求
- 博客分类:
- ArcGISServer
服务器端的处理才是整个事件的Key,那么我们来看看整个请求,回复的过程,我们通过下面的图看看我们服务器端的代码怎么样才可以plug in进去:
看了这个图您肯定已经明白了,我们有机会得到客户端发送过来的HTTP请求,得到这个请求,得到请求里面的参数,我们就可以捋起袖子,大干一番了。
我们用什么方式去处理呢,最简单的一个方法是自己写一个servlet,在servlet的Dopost方法里面,写上处理这个请求的代码,并且把结果以XML的方式返回回去,那么我们这里用一个简单的例子:
- public void doPost(HttpServletRequest req, HttpServletResponse res)
- throws java.io.IOException {
- String action = req.getParameter("action");
- String item = req.getParameter("item");
- if ((action != null)&&(item != null)) {
- // Add or remove items from the Cart
- if ("add".equals(action)) {
- cart.addItem(item);
- } else if ("remove".equals(action)) {
- cart.removeItems(item);
- }
- }
- // Serialize the Cart's state to XML
- String cartXml = cart.toXml();
- // Write XML to response.
- res.setContentType("application/xml");
- res.getWriter().write(cartXml);
- }
public void doPost(HttpServletRequest req, HttpServletResponse res) throws java.io.IOException {String action = req.getParameter("action");String item = req.getParameter("item");if ((action != null)&&(item != null)) { // Add or remove items from the Cart if ("add".equals(action)) { cart.addItem(item); } else if ("remove".equals(action)) { cart.removeItems(item); }}// Serialize the Cart's state to XMLString cartXml = cart.toXml();// Write XML to response.res.setContentType("application/xml");res.getWriter().write(cartXml);}
这对于了解服务器端编程的人来说,是最简单不过的了。当然,我们用了JSF框架,肯定不会再通过Servlet技术去做这件事情。我们这里用JSF的PhaseListener进行处理。什么是PhaseListener,我们来看一下PhaseListener的JavaDoc:
- public interface PhaseListener
- extends java.util.EventListener, java.io.Serializable
- An interface implemented by objects that wish to be notified at the beginning and ending of processing for each standard phase of the request processing lifecycle.
public interface PhaseListenerextends java.util.EventListener, java.io.SerializableAn interface implemented by objects that wish to be notified at the beginning and ending of processing for each standard phase of the request processing lifecycle.
原来,这个接口捕捉到每个Request的声明周期,在请求开始处理和请求处理结束时我们可以插入我们自己的代码。这个接口的两个最重要的方法如下:
- void afterPhase(javax.faces.event.PhaseEvent event)
- Handle a notification that the processing for a particular phase has just been completed. void beforePhase(javax.faces.event.PhaseEvent event)
- Handle a notification that the processing for a particular phase of the request processing lifecycle is about to begin.
void afterPhase(javax.faces.event.PhaseEvent event) Handle a notification that the processing for a particular phase has just been completed. void beforePhase(javax.faces.event.PhaseEvent event) Handle a notification that the processing for a particular phase of the request processing lifecycle is about to begin.
处理结束和处理开始两个方法,我们可以在这两个方法里面写入我们自己的代码,对用户过来的请求进行处理。
比如,我们可以在afterPhase方法里面,插入下面的代码对数据进行处理:
- FacesContext facesContext = phaseEvent.getFacesContext();
- ExternalContext externalContext = facesContext.getExternalContext();
- Map paramMap = externalContext.getRequestParameterMap();
FacesContext facesContext = phaseEvent.getFacesContext(); ExternalContext externalContext = facesContext.getExternalContext();Map paramMap = externalContext.getRequestParameterMap();
通过paramMap里面可以得到所有的参数,当然,我们并不是对每一个请求都会进行处理,所以,在客户端Javascript发送Ajax请求得时候,可以添加一个自定义的参数,比如我们前面文档中的ajaxdemo=ajaxdemo参数,这样我们可以判断一下当前是不是我们要处理的请求:
- private final static String AJAX_DEMO = "ajaxdemo";
- if (! AJAX_DEMO.equals(paramMap.get(AJAX_DEMO)))
- return;
private final static String AJAX_DEMO = "ajaxdemo";if (! AJAX_DEMO.equals(paramMap.get(AJAX_DEMO))) return;
接下去的代码,就可以处理我们的各种各样的请求了,我们在这里要举的例子是如何得到当前地图的比例尺。下次讲座再深入探讨吧。
Ajax异步处理(服务器返回请求)
xmlhttprequest对象已经创建好了!
xmlHttp.onreadystatechange=callBack;
xmlHttp.open("POST","AjaxXML",true);
xmlHttp.send("username='"+name+"'");
这个是callback方法
function callBack()
{
alert(xmlHttp.readyState);
// 接受服务器端返回的数据
// readyState的值等于4是表示对象与服务器端交互成功的
if(xmlHttp.readyState == 4)
{ //status的值等于200,表示http与服务器交互成功
if(xmlHttp.status== 200)
{
//获取服务器端返回的数据
var domobj=xmlHttp.responseXML;
var message=domobj.getElementsByTagName("message");
if(message.length>0)
var text=message[0];
else
document.getElementById("result").innerHTML=xmlHttp.responseText;;
}
}
}
问题补充 2010-09-07 14:45
这个js访问的是一个servlet,代码如下:
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: alvin
* Date: 2010-9-6
* Time: 20:02:54
* To change this template use File | Settings | File Templates.
*/
public class AjaxXMLServer extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("gbk");
//修改点1---把Content-type改成text/xml (必须的)
response.setContentType("text/xml;charset=gb18030");
String name=request.getParameter("username");
System.out.println(name);
try{
PrintWriter out=response.getWriter();
StringBuilder builder=new StringBuilder();
builder.append("<message>");
if(name==null||name.length()==0)
{ builder.append("您输入的用户名为空,请查证后输入!").append("</message>");
// out.print("<script>location.href='index.jsp'</script>");
}else{
if(name.equals("mengxi"))
{
builder.append("您输入的用户名存在,请重新输入!").append("</message>");
// out.print("<a href='index.jsp'>返回</a>");
}else
{ builder.append("您输入的用户名不存在,恭喜你可以使用!").append("</message>");
// out.print("<script>alert('您输入的用户名不存在,该用户名可以使用!');</script>");
// out.print("<script>location.href='index.jsp'</script>");
}
}
}catch(IOException e)
{
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
{ try{
doPost(request, response);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
- Server Java 四 :如何在用JSF在服务器端处理Ajax请求
- 在JSF中如何使用AJAX
- 在JSF中如何使用AJAX
- 在JSF中如何使用AJAX
- 如何在JSF中处理转义字符?
- 在服务器端判断request是否来自Ajax的异步请求
- 在struts中处理AJAX请求
- 在struts中处理AJAX请求
- 在Web应用中,会用到大量的Ajax请求,在Ajax请求中存在的一个问题就是如何判断session超时,在网上找到的一个java中处理此问题方法:
- 在PHP中如何判断AJAX请求
- 如何在CakePHP中使用Ajax请求
- 如何在React中做Ajax 请求?
- 关于Ajax请求服务器端的处理
- 在服务器端判断request来自Ajax请求(异步)还是传统请求(同步)
- 在服务器端判断request来自Ajax请求(异步)还是传统请求(同步)
- 在服务器端判断request来自Ajax请求(异步)还是传统请求(同步)
- 在服务器端判断request来自Ajax请求(异步)还是传统请求(同步)
- 在服务器端判断request来自Ajax请求(异步)还是传统请求(同步)
- 让网站真正实现实时更新的Realtime技术,不用刷新网站、没有延时性
- poj 1088 动态规划
- Mudos 编程总结
- 算法系列——N皇后问题
- Web前端开发人员和设计师必读文章推荐(转载)
- Server Java 四 :如何在用JSF在服务器端处理Ajax请求
- 删除及重建oracle EM
- 第03章 面向对象 38 对象转型2
- 2015年之前需要阅读完的非技术书单
- 第03章 面向对象 39 多态1
- HttpSessionListener用法
- HDU Boring counting 树状数组
- 局部变量、全局变量、堆、堆栈、静态和全局
- 学习MongoDB--(6-1):聚合(初级聚合函数使用)