什么样在jsp页面中使用ajax从服务器重返的json数据

作者:操作系统

作者写八个签到页面,输入顾客名和密码后决断,如果有就实践A,若无就实行B。但不管怎么着,它都要踏向到index.jsp页面。packagecom.servlet;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.;importjavax.servlet.;importjavax.servlet.;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;importcom.dao.UserDao;importcom.dao.UserDaoImpl;publicclassDengluServletextendsHttpServlet{//必要继续HttpServlet比量齐观写doGetdoPost方法privatestaticfinallongserialVersionUID=1L;privatefinalstaticLoglogger=LogFactory.getLog(DengluServlet.class卡塔尔;publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponseState of QatarthrowsServletException,IOException{doPost(request,response卡塔尔国;//将音讯使用doPost方法施行对应jsp页面中的form表单中的method}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse卡塔尔国throwsServletException,IOException{Stringname=request.getParameter("name"卡塔尔;//获得jsp页面传过来的参数Stringpwd=request.getParameter("pwd"卡塔尔国;UserDaoud=newUserDaoImpl(卡塔尔国;if(ud.login(name,pwd)卡塔尔国{logger.debug("intologin"卡塔尔(قطر‎;System.out.println("intologin"卡塔尔(قطر‎;request.setAttribute("xiaoxi","Welcome:" name卡塔尔(قطر‎;//向request域中放置消息request.getRequestDispatcher("/success.jsp"卡塔尔(قطر‎.forward(request,response卡塔尔(قطر‎;//转载到成功页面}else{logger.debug("intofalse"State of Qatar;System.out.println("intofalse"卡塔尔(قطر‎;response.sendRedirect("zhuce.jsp"卡塔尔(قطر‎;//重定向到注册页}}}

操作系统 1

操作系统 2

自己想通过ajax实现三个商品筛选的功能,通过ajax的措施将筛选规范传到servletservlet把询问到的商品新闻封装成一个ListProduct的成团,再转发成json数组字符串再次回到到ajax.笔者想在页面上用c:foreach去循环遍历那个json数组,以此实现动态刷新的筛选成效。但难题是不怕在js少将再次回到来的数码形成Object数组,在jsp页面是无法访谈到这些数组的,通过jsp表达式或然el表达式也无从在js少校数组存放在域里,假使自个儿想直接在js代码中去拼接html代码,又比较复杂。。。。这几个标题找麻烦自身好久了,希望能得到大神的解答。本来jsp用于呈现的数据是servlet存放的ListProduct数据。%ListProductrexiaophone=(ListProductState of Qatarrequest.getAttribute("rexiaophone"卡塔尔国;%c:forEachitems="${rexiaophone}"var="rexiao"end="2"以后使用ajax方式,数据只可以是json类型的数组。如何在页面中遍历JSP代码:%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%%@pageimport="java.util.*,com.cxsw.zms.po.*"%%@tagliburi=""prefix="c"%%PageProductModelallphone=(PageProductModel)request.getAttribute("allphone");%%ListProductrexiaophone=(ListProduct)request.getAttribute("rexiaophone");%!DOCTYPEhtmlhtmllang="en"metacharset="utf-8"headtitle手提式无线话机商号/title!--ALLSTYLESHEET--scriptsrc="js/jquery.js"/scriptlinkhref="bootstrap/css/bootstrap.min.css"rel="stylesheet"linkhref="css/font-awesome.min.css"rel="stylesheet"linkhref="css/style.css"rel="stylesheet"!--linkhref="css/blog-single.css"rel="stylesheet"linkhref="css/responsive.css"rel="stylesheet"--!--弹出框--linkrel="stylesheet"type="text/css"href="css/xcConfirm.css"/scriptsrc="js/xcConfirm.js"type="text/javascript"charset="utf-8"/scriptscripttype="text/javascript"functionfilter(){vara="";varobj=document.getElementsByName("brand");for(vari=0;iobj.length;i ){if(obj[i].checked){a =obj[i].value ",";}}//alert(a.substring(0,a.length-1));//window.location="${pageContext.request.contextPath}/GetProductsByBrandServlet?pro_kind=手机brand=" a.substring(0,a.length-1);varxmlHttp;xmlHttp=null;if(window.XMLHttpRequest){xmlHttp=newXMLHttpRequest();}if(xmlHttp!=null){xmlHttp.onreadystatechange=state_Change;xmlHttp.open("GET","${pageContext.request.contextPath}/GetProductsByBrandServlet?pro_kind=手机brand=" a.substring(0,a.length-1),false);xmlHttp.send(null);}functionstate_Change(卡塔尔国{if(xmlHttp.readyState==4卡塔尔国{//4="loaded"if(xmlHttp.status==200卡塔尔(قطر‎{//200="OK"varx=xmlHttp.responseText;varobj=eval(x卡塔尔国;alert(obj卡塔尔国;//怎么着在jsp页面中动用再次回到的数据obj}else{alert("Problemretrievingdata:" xmlState of Qatar;}}/script!--scripttype="text/javascript"functionfilter(){vara="";varobj=document.getElementsByName("brand");for(vari=0;iobj.length;i ){if(obj[i].checked){a =obj[i].value ",";}}//alert(a.substring(0,a.length-1));window.location="${pageContext.request.contextPath}/GetProductsByBrandServlet?pro_kind=手提式有线话机brand=" a.substring(0,a.length-1卡塔尔国;}/script--scripttype="text/javascript"window.onload=function(State of Qatar{if(%=allphone%==null卡塔尔国{window.location="${pageContext.request.contextPath}/PhonePageServlet";};}/script/headbody//在这里边使用ajax重临的数据c:forEachitems="${rexiaophone}"var="rexiao"end="2"divdiv!--spandivSale/div/span--spanimgsrc="/upload/${rexiao.pro_mainimg}"alt=""divahref=""i/i/aahref=""i/i/a/div/spandivdiv${rexiao.prod_name}divi/ii/ii/ii/ii/i/div/divh3${rexiao.pro_priint}/h3strong¥${rexiao.pro_price}/strongdivahref="javascript:(0)"onclick="testLogin(${rexiao.pro_itemnum}卡塔尔国"role="button"加入购物车/anbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;ahref="javascript:(0State of Qatar"onclick="testLogin(${rexiao.pro_itemnum}State of Qatar"role="button"立即购买/a/div/div/div/div/c:forEach/div/divajax后台代码:packagecom.cxsw.zms.servlet;importjava.io.IOException;importjava.io.OutputStream;importjava.io.PrintWriter;importjava.util.List;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.;importjavax.servlet.;importjavax.servlet.;importcom.cxsw.zms.po.Product;importcom.cxsw.zms.service.ProductMangement;importcom.cxsw.zms.service.ProductMangementImpl;importcom.google.gson.Gson;@WebServlet("/GetProductsByBrandServlet"卡塔尔(قطر‎publicclassGetProductsByBrandServletextendsHttpServlet{privatestaticfinallongserialVersionUID=1L;Stringbrand="";Stringpro_kind="";ListProductdata=null;ListProductrexiaophone=null;ProductMangementpm=newProductMangementImpl();protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{brand=request.getParameter("brand");pro_kind=request.getParameter("pro_kind");data=pm.ShaiXuanProduct(brand,pro_kind);if(pro_kind.equals("手机")){rexiaophone=pm.GetRexiaoPhone();Gsonjson=newGson();Stringa=json.toJson(data);PrintWriterout=response.getWriter();out.write(a);request.setAttribute("rexiaophone",rexiaophone);request.setAttribute("shaixuanphone",data);/*request.getRequestDispatcher("/shaixuanphone.jsp").forward(request,response);*操作系统,/}/*if(pro_kind.equals("台式机计算机"卡塔尔(قطر‎State of Qatar{request.setAttribute("shaixuanbijiben",data卡塔尔(قطر‎;request.getRequestDispatcher("/shuaixuanbijiben.jsp"卡塔尔(قطر‎.forward(request,responseState of Qatar;}*/}protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doGet(request,response);}}

本文由ca88发布,转载请注明来源

关键词: 新人 报错 P 方法 大佬