servlet与jsp交互问题
来源:互联网 发布:suse网络配置 编辑:程序博客网 时间:2024/05/16 05:14
最近一直困扰我的一个问题:
问题描述:
在jsp页面使用ajax返回数据时,后台的servlet一直取不到数据。(userid一直为空)
index.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><html><head><!-- meta使用viewport以确保页面可自由缩放 --><meta name="viewport" content="width=device-width, initial-scale=1"><!-- 引入 jQuery Mobile 样式 --><link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"><!-- 引入 jQuery 库 --><script src="http://code.jquery.com/jquery-1.11.3.min.js"></script><!-- 引入 jQuery Mobile 库 --><script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script><script type="text/javascript"> (function ($) { //扩展方法获取url参数 $.getUrlParam = function (name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; }; })(jQuery); //得到url参数 var userid = $.getUrlParam('userid'); alert(userid); //ajax var options = { url: "${pageContext.request.contextPath}/servlet/UserMsgServlet", type: "GET", dataType: "String", data:userid , async:true, success: function (data) { alert(data.detail); } }; $.ajax(options); </script><script type="text/javascript">/* $("#panelButton").click(function(){ $.getUrlParam = function (name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; }; })(jQuery); var userid = $.getUrlParam('userid'); $.ajax({ url:'${pageContext.request.contextPath}/servlet/UserMsgServlet', type:'get', async:true, data:{userid:userid}, datatype:'text/json', success:function(data){ $("#myPanels").append("p"+data+"</p>"); } }); }); */</script></head><body> <div data-role="page" id="pageone"> <!-- 面板内容 --> <div data-role="panel" id="myPanels"> <ul data-role="listview"> <li><a href="#">列表项</a></li> <li><a href="#">列表项</a></li> </ul> </div> <!-- 头部 --> <div data-role="header"> <!--头部功能--> <div class="ui-grid-a"> <div class="ui-block-a"> <a id="panelButton" href="#myPanels" data-role="button" data-inline="true" data-icon="bars" >面板</a> </div> <div class="ui-block-b" style="text-align:right"> <a id="Search" href="${pageContext.request.contextPath }/servlet/UserMsgServlet" data-role="button" data-icon="Search">获取用户</a> </div> <!-- <div class="ui-block-c"> <input type="search" name="search" id="search" placeholder="搜索内容..."> </div> --> </div> </div> <!-- 中部 --> <div id= "middle "data-role="main" class="ui-content"></div> <!-- 底部 --> <div data-role="footer" style="text-align:center " data-position="fixed"> <div data-role="navbar" data-iconpos="bottom"> <ul> <li><a href="#" data-role="button" data-icon="info" >收件箱</a></li> <li><a href="#" data-role="button" data-icon="grid" >待阅</a></li> <li><a href="#" data-role="button" data-icon="star" >待办</a></li> </ul> </div> </div> </div></body></html>
UserMsgservlet的代码如下:
package oa.webservice.web;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import oa.webservice.test.ClientTest1;public class UserMsgServlet extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json,charse=utf-8"); String userid = request.getParameter("userid"); System.out.println(userid); String userName = ClientTest1.getClientTest1().oa_IsUserExist(); request.setAttribute("userName", userName); request.getRequestDispatcher("/index.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
先记录下来看看能不能解决。
知识点:ajax,servlet的作用域。
阅读全文
0 0
- servlet与jsp交互问题
- servlet与jsp交互中文乱码问题
- JSP与Servlet的交互
- 前台 jsp 与 后台 servlet 的数据交互问题
- 4、JSP与Servlet交互,JSP与JSP交互
- jstl与servlet动态jsp交互实现
- 010.JSP与Servlet交互深入详解
- jsp与servlet的交互、传值
- SERVLET与JSP Jsp中使用xmlhttp进行数据交互
- Servlet与JSP路径问题
- 前台与servlet交互乱码问题
- JSP如何与数据库交互?即JSP与后台Servlet之间的关系,如何交互
- Ajax、servlet、jsp、JSON数据交互以及跳转问题
- 使用Jquery--Jsp与servlet之间的数据交互
- 使用Jquery--Jsp与servlet之间的数据交互
- web 前段与后端的交互(JSP 和 Servlet)
- JSP与JS交互问题,值传递
- servlet与jsp,jsp与jsp,jsp与servlet的传递参数问题
- 个人总结php笔试题二
- Barcode生成条形码
- 利用微信接口获取用户的地理位置
- 如何通过EL,JSTL,JSP脚本元素在jsp页面显示
- Vue2.0-下篇
- servlet与jsp交互问题
- Android NavigationDrawer 开发详解-实现侧滑效果
- json字符串与对象相互转换
- 【Python2.7 学习笔记】函数(一)
- git 统计自己的代码行数
- elasticsearch5 head安装
- 同余方程
- 【Setting】win10+python3+tensorflow-gpu环境搭建
- MySQL高性能学习笔记