$("a").click(function () {
var txtContent=$(this).text();
然后通过servlet将取到的值进行查询
显示在新的jsp页面上
$("a").click(function () {
var txtContent=$(this).text();
location.href= “test.do?xxx=”+txtContent;
}
后端用
request.getAttribute("xxx");可以取出来查询完数据库之后早用request.setAttribute("sss",sss);存起来然后跳转页面
在跳转后的页面用jstl表达式来获取
这两个图片是mvc框架的,与servlet有点区别,不过传值都是一样的
可以通过ajax请求将取到的值提交给servlet。
<input id="username" type="text"/>
<button onclick="getFromServlet()"></button>
function getFromServlet(){
var text = $("#username").val();
$.ajax({
type: "GET",
url: "TestServlet.do?username="+text,
dataType: "text",
success: function(data){
//data就是返回的数据
}
});
}
public class TestServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/text; charset=utf-8");
PrintWriter out = response.getWriter();
//获取到jsp页面传过来的username
String username = request.getParameter("username");
}
}
$("a").click(function () {
var content=$(this).text();
$.get("你的服务器地址.jsp?v=" + content, function(result){
$("某个div").html(result);
});
});
你的服务器上用
request.getParameter("v") 可以获取
一般用AJax传值,不过新手可以先用表单体验下,有get和POST两种方式,后端用Springboot框架比较简单哦