DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX基礎知識 >> jquery訪問servlet並返回數據到頁面的方法
jquery訪問servlet並返回數據到頁面的方法
編輯:AJAX基礎知識     

本文實例講述了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="提交"  onclick="verify()"/> 
  </body>
</html>

希望本文所述對大家的Ajax程序設計有所幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved