prototype Ajax + Servlet (attached Demo)

2009-03-05  来源:本站原创  分类:AJAX  人气:1429 

Test WEB project set up
prototype.js location WebContent / js / prototype.js
Create test2.jsp location WebContent/test/test2.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
	pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
<script src="js/prototype.js" type="text/javascript"></script>
</head>

<script language="javascript">

    var vXHR;

    function createXHR()
    {

        try
        {
            vXHR = new ActiveXObject("Microsoft.XMLHTTP");

        }
        catch (e)
        {
            try
            {
                vXHR = new ActiveXObject("MSXML2.XMLHTTP");

            }
            catch (e)
            {
                vXHR = false;
            }
        }
        if (!vXHR && typeof XMLHttpRequest != 'undefined')
        {
            vXHR = new XMLHttpRequest();

        }
        return vXHR;
    }

    function getInfo()
    {
        createXHR();
        var textValue = document.getElementById("text1").value;
        var vUrl = "/Test/prototypeservlet?test=" + textValue;

        vXHR.open("get", vUrl, true);
        vXHR.send(null);
        vXHR.onreadystatechange = infoBack;

    }

    function infoBack()
    {
        if (vXHR.readyState == 4)
        {
            if (vXHR.status == 200)
            {
                var s = vXHR.responseText;
                document.getElementById("text1").value = s;
            }
        }
    }
</script>
<body>
<input type="text" name="text" />
<input type="button" name="button" value="Click on the "
	onclick="getInfo();" />
</body>
</html>

Create PrototypeServlet.java location com.test.servlet

package com.test.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;

public class PrototypeServlet extends HttpServlet
{

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public void init() throws ServletException
    {

        System.out.println("hello");
    }

    public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException
    {
        String textValue = request.getParameter("test");
        System.out.println("textValue="+textValue);
        PrintWriter out = null;
        try
        {
            out = response.getWriter();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        out.print("hello:"+textValue);

    }

    public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException
    {
        doGet(request, response);
    }

}

web.xml configuration

<servlet>
		<servlet-name>PrototypeServlet</servlet-name>
		<servlet-class>com.test.servlet.PrototypeServlet</servlet-class>
		<init-param>
			<param-name>debug</param-name>
			<param-value>true</param-value>
		</init-param>
	</servlet>

	<servlet-mapping>
		<servlet-name>PrototypeServlet</servlet-name>
		<url-pattern>/prototypeservlet/*</url-pattern>
	</servlet-mapping>
相关文章
  • prototype Ajax + Servlet (attached Demo) 2009-03-05

    Test WEB project set up prototype.js location WebContent / js / prototype.js Create test2.jsp location WebContent/test/test2.jsp <%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <!DO

  • Ajax servlet to achieve 2009-08-12

    Ajax servlet to achieve

  • prototype ajax applications 2008-10-27

    1. Client <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' st

  • prototype Ajax.Updater 2008-12-26

    var url = "/ edit / find_topic"; var myajax = new Ajax.Updater ( (success: 'ul'), / / ul for a control's ID url, ( parameters: 'keyword ='+$(" keyword "). value, asynchronous: true, method: 'get', evalScripts: true ) ); The Ajax.Update

  • Ajax + Servlet (JSP) Basic Usage 2010-03-25

    To use Ajax, first of all have to create the XMLHttpRequest object: var request = false; try{ request = new XMLHttpRequest();//Mozilla,Safari,Opera,IE7 , Etc. }catch(e){//IE7 Previous versions try{ request = new ActiveXObject("Msxml2.XMLHTTP");/

  • Between jquery and ajax servlet in the Chinese garbled strange solution 2010-07-29

    Our project uses the character encoding is an all-around Taiwan utf8, I thought everything will be fine like this, nothing will funnel the. But who knows ajax data transmission over the Chinese, or garbled. The original client so $. Get ("Discrete? Q

  • ajax + servlet simple example 2010-10-21

    Reference line to do a Ajax + jsp + servlet example of a simple Not more than that, all in the code First jsp page code: <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC &quo

  • prototype ajax external javascript 2010-03-26

    Updated with new Ajax.updater a file, this file has <script src="/files/inc/tests.js" type="text/javascript"> </ script> <script type="text/javascript"> alert (11); </ Script> Data returned by ajax, fo

  • Ajax Servlet using synchronous communication with the background 2010-06-19

    function createXmlHttpRequest(){ var oXmlHttp; if(window.XMLHttpRequest){ // Non-IE browser oXmlHttp = new XMLHttpRequest(); if(oXmlHttp.overrideMimeType){ // For Firefox browsers , Pass parameters problems, setting MimeType oXmlHttp.overrideMimeType

  • ajax+servlet实例 2010-12-30

    <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-fa

  • Dynamically generated table, and add or delete rows and columns - Based on prototype (source code attached) 2010-03-26

    In web development, often encountered on the Table the following requirements: Copy table row, delete the current line, dynamically generated forms and add or delete columns, etc. This paper is a personal summary of work in peacetime. This article is

  • ajax servlet, but does not implement the use of solution 2010-05-10

    Method 1: xmlHttp.open ("GET", "default.aspx? param =" + Math.random (), true); Or xmlHttp.open ("GET", "default.aspx? param =" + new Date (). getTime (), true); Method 2: xmlHttp.open ("GET", "defaul

  • With the background using Ajax Servlet asynchronous communication 2010-06-19

    function createXmlHttpRequest(){ var oXmlHttp; if(window.XMLHttpRequest){ // Non-IE browser oXmlHttp = new XMLHttpRequest(); if(oXmlHttp.overrideMimeType){ // For Firefox browsers , Pass parameters problems, setting MimeType oXmlHttp.overrideMimeType

  • Prototype of the Ajax support 2010-07-27

    2.3 Prototype of the Ajax support As an Ajax development framework, Prototype on Ajax development provided strong support. In the Prototype, the with the Ajax-related classes and objects, including: Ajax, Ajax.Responsders, Ajax.Base, Ajax.Request, Aj

  • JSP + Servlet + Ajax user information to achieve the background instance of the asynchronous test 2010-08-12

    Here are the contents of JSP front form submitted to the background by Ajax Servlet asynchronous calibrate (check a variety of ways, including the extraction of database information, check user name is repeated, etc.), asynchronous form in the JSP pa

  • Ajax asynchronous call servlet - Note the path 2010-11-01

    -------------- Jsp - <% @ Page language = "java" import = "java.util .*" pageEncoding = "UTF-8"%> <% String path = request.getContextPath (); String basePath = request.getScheme ()+"://"+ request.getServerN

  • ajax garbled solution summary 2009-03-20

    First, javascript use java the way the characters deal with, the internal use are to handle all unicode characters, Second, utf-8 characters are of each (unicode characters) to use 3 bytes to store. Third, using utf-8 to send data is not garbled, and

  • Flv to try: demonstration ajax implementation of puff at the effect of RJS 2009-05-13

    It's really annoying, ah, finally get Some message RJS Introduction What is RJS? What is RJS? Ajax allows the user and the interaction between the browser experience is more like desktop programs. Since Ajax allows the browser to launch long-distance

  • Tomcat 7 run Servlet 3 2010-04-02

    The first to point out: Tomcat 7 is still on the trunk, and no official release. But would like to try, if not impossible, in the SVN in the Tomcat 7 detection, and then the local compilation on the list. Of course, you can use NETBEANS 6.8, the use

  • ajax determine whether the user exists 2010-08-09

    login.html <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title> User login </title> </head> <script type="text/javascript" src="js/validateAjax.