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" οnclick="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结合方能解决中文乱码问题 这是方法一

方法二:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值