jquery访问servlet并返回数据到页面

1,servlet:AjaxServlet.java

package com.panlong.servlet;


import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AjaxServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Integer total = (Integer) req.getSession().getAttribute("total");
		int temp = 0;
		if(total == null ){
			temp = 1;
		}else{
			temp = total.intValue() + 1;
		}
	req.getSession().setAttribute("total",temp);
		try {
			//1.取参数
			resp.setContentType("text/html;charset=GBK");
			PrintWriter out = resp.getWriter();
			
			String old = req.getParameter("name");
			
			//2、检查参数是否有问题
			//String name = new String(old.getBytes("iso8859-1"),"UTF-8");
			String name = URLDecoder.decode(old,"UTF-8");
			if("".equals(old) || old == null){
				out.println("用户名必须输入");
				
			}else{
				if("liling".equals(name)){
					out.println("恭喜登录成功");
					return;
				}else{
					out.println("该用户名未注册,您可以注册["+name+"]这个用户名"+temp);
				}
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		//3.检验操作
		
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

}


2,verify.js

function verify(){
	//解决中文乱码问题的方法1,页面端发出的数据作一次encodeURI,服务端使用new String(old.getBytes("iso8859-1"),"UTF-8");
	//解决中文乱码问题的方法2,页面端发出的数据作两次encodeURI,服务端使用String name = URLDecoder.decode(old,"UTF-8");
	var url = "servlet/AjaxServlet?name="+encodeURI(encodeURI($("#userName").val()));
	url = convertURL(url);
	$.get(url,null,function(data){
		$("#result").html(data);
	});
}
//给url地址增加时间蒫,难过浏览器,不读取缓存
function convertURL(url){
	//获取时间戳
	var timstamp = (new Date()).valueOf();
	//将时间戳信息拼接到url上
	if(url.indexOf("?") >=0){
		url = url + "&t=" + timstamp;
	}else{
		url = url + "?t=" + timstamp;
	}
	return url;
}


3,前台页面

<!DOCTYPE html>
<html>
  <head>
    <title>AJAX实例</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=GBK">
    <script type="text/javascript" src="js/verify.js"></script>
    <script type="text/javascript" src="js/jquery.js"></script>
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
   		<font color="blue" size="2">请输入用户名:</font> 
   		 <input type="text" id="userName" /><font color="red" size="2"><span id="result" >*</span></font><br/><br/>
   		 <!-- <div id="result"></div> -->
   		  <input type="submit" name="提交" value="提交"  οnclick="verify()"/>
   		 
  </body>
</html>


 

好的,我会尽力回答你的问题。 首先,你需要编写一个Servlet来处理Ajax请求,并返回数据。你可以使用doGet或doPost方法来处理请求。在该Servlet中,你需要查询数据库或其他数据源,然后将结果转换为JSON格式的数据。最后将JSON数据发送回Ajax请求。 下面是一个简单的Servlet示例代码: ```java import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.google.gson.Gson; public class AjaxDataServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<User> userList = getUsers(); String json = new Gson().toJson(userList); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); out.print(json); out.flush(); } private List<User> getUsers() { List<User> userList = new ArrayList<User>(); // 查询数据库或其他数据源 // 然后将结果转换为User对象 // 最后将User对象添加到userList中 return userList; } } class User { private String name; private int age; public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } ``` 然后,在HTML页面中,你需要使用jQuery发起Ajax请求,并将响应数据展示在dataTable表格中。下面是一个简单的HTML页面示例代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>DataTable Example</title> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.18/datatables.min.css"/> </head> <body> <table id="userTable" class="display" style="width:100%"> <thead> <tr> <th>Name</th> <th>Age</th> </tr> </thead> </table> <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.18/datatables.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $('#userTable').DataTable( { "ajax": { "url": "ajaxDataServlet", "dataSrc": "" }, "columns": [ { "data": "name" }, { "data": "age" } ] } ); } ); </script> </body> </html> ``` 在这个示例中,我们使用了jQuery和DataTables插件来实现Ajax请求和表格展示。在文档准备就绪时,我们通过DataTable初始化一个表格,并使用ajax属性指定Ajax请求的URL。响应数据将自动填充到表格中。 以上就是一个简单的Ajax请求访问servlet,响应给html页面中的dataTable表格,并展示数据的实现方法。希望能够帮到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值