jquery获取后台数据简单例题
html部分:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>jqueryXmlImg.html</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=UTF-8">
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript">
function verify(){
var url="../AjaxServletImg?userName="+$("#userName").val();
//url=convertURL(url);
$.get(url,null,callback);
}
function callback(data){
$("#resultMess").html(data);
}
function convertURL(url){
var timestamp=(new Date()).valueOf();
if(url.indexOf("?")>=0){
url=url+"&t="+timestamp;
}else{
url=url+"?t="+timestamp;
}
return url;
}
</script>
</head>
<body>
<input type="text" name="userName" id="userName" value="1111111" />
<input type="button" name="btn1" id="btn1" onclick="verify();" value="测试" />
<div id="resultMess"></div>
</body>
</html>
后台servlet部分:
package com.wf.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class AjaxServletImg extends HttpServlet {
/**
* Constructor of the object.
*/
public AjaxServletImg() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* 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 {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
HttpSession sess=request.getSession();
Integer inte=(Integer) sess.getAttribute("total");
int temp=0;
if(inte==null){
sess.setAttribute("total",1);
}else{
temp=inte.intValue()+1;
sess.setAttribute("total",temp);
}
try{
String userName=request.getParameter("userName");
System.out.println(userName);
if(userName==null||userName.length()==0){
out.println("用户名不能为空!"+temp);
}else{
if("oppa".equals(userName)){
out.println("用户名"+userName+"已经存在"+temp);
}else{
out.println("用户名"+userName+"可以注册使用!"+temp);
}
}
}catch(Exception e){
e.printStackTrace();
}
out.flush();
out.close();
}
public void init() throws ServletException {
}
}
说明:url=convertURL(url);这是让浏览器不读缓存的方法
中文乱码问题解决方法:
方法一:
1.var url="../AjaxServletImg?userName="+encodeURI($("#userName").val());
前台encodeURI()转换下
2.后台String userName=request.getParameter("userName");
System.out.println(userName);
userName=new String(userName.getBytes("iso8859-1"),"UTF-8");
也再转换下
1和2结合方能解决中文乱码问题 这是方法一
方法二: