<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Java學習

    java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已經搬家了,新的地址是 http://www.javaly.cn 如果有對文章有任何疑問或者有任何不懂的地方,歡迎到www.javaly.cn (Java樂園)指出,我會盡力幫助解決。一起進步

     

    AJAX+JSP+SERVLET入門例子

    1.       servlet Hello.java

    package com;

    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 Hello extends HttpServlet {

        /**

         * Constructor of the object.

         */

        public Hello() {

           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 {

           String date = request.getParameter("birthDate");

           System.out.println(date);

           this.returnResultXml2(response, "<message> hello,world " + date + "你好! </message>");

           //response.setContentType("text/html");

           /*PrintWriter out = response.getWriter();

           out.println("<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"">");

           out.println("<HTML>");

           out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");

           out.println("  <BODY>");

           out.print("    This is ");

           out.print(this.getClass());

           out.println(", using the GET method");

           out.println("  </BODY>");

           out.println("</HTML>");

           out.flush();

           out.close();*/

        }

        /**

         * The doPost method of the servlet. <br>

         *

         * This method is called when a form has its tag value method equals to post.

         *

         * @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 doPost(HttpServletRequest request, HttpServletResponse response)

               throws ServletException, IOException {

               doGet(request,response);

        }

        /**

         * Initialization of the servlet. <br>

         *

         * @throws ServletException if an error occure

         */

        public void init() throws ServletException {

           // Put your code here

        }

        public void returnResultXml2(HttpServletResponse response, String resultxml)

        {

           try

           {

               response.setContentType("text/xml; charset=UTF-8");

               response.setHeader("Cache-Control", "no-cache");

               response.getWriter().println(resultxml);

               response.getWriter().flush();

           } catch (IOException e)

           {

               e.printStackTrace();

           }

        }

    }

    2.       jsp index.jsp

    <%@ page language="java" %>

    <%@ page contentType="text/html; charset=GB2312"%>

    <html>

    <head>

    <title>hello World</title>

    </head>

    <body bgcolor="#ffffff">

    <center>

    <font size="5" color="blue">各種字體大小顯示</font><br><a href="validdate.html">validate</a><br>

    </center>

    <br>

    <hr>

    <br>

    <div align="center">

    <%

       for(int i=1; i<=6;i++)

           out.println("<h" + i + ">hell  World!</h" + i + ">");

     %>

    <div>

    </body>

    </html>

    3.       xml  web.xml

    <?xml version="1.0" encoding="UTF-8"?>

    <web-app version="2.4"

        xmlns="http://java.sun.com/xml/ns/j2ee"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

        <servlet>

           <servlet-name>ValidationServlet</servlet-name>

           <servlet-class>ajaxbook.chap4.ValidationServlet</servlet-class>

        </servlet>

      <servlet>

        <servlet-name>Hello</servlet-name>

        <servlet-class>com.Hello</servlet-class>

      </servlet>

      <servlet-mapping>

        <servlet-name>Hello</servlet-name>

        <url-pattern>/hello</url-pattern>

      </servlet-mapping>

      <welcome-file-list>

        <welcome-file>index.jsp</welcome-file>

      </welcome-file-list>

    </web-app>

    4.       html validate.html

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >

    <html>

        <head>

           <title>Using Ajax for validation</title>

           <script type="text/javascript">

    var xmlHttp;

    function createXMLHttpRequest() {

    if (window.ActiveXObject) {

    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

    }

    else if (window.XMLHttpRequest) {

    xmlHttp = new XMLHttpRequest();

    }

    }

    function validate() {

    createXMLHttpRequest();

    var date = document.getElementById("birthDate");

    alert(validate);

    var url = "hello?birthDate=" + escape(date.value);

    xmlHttp.open("GET", url, true);

    xmlHttp.onreadystatechange = callback;

    xmlHttp.send(null);

    }

    function callback() {

    if (xmlHttp.readyState == 4) {

    if (xmlHttp.status == 200) {

    alert(xmlHttp.responseText);

    var message = xmlHttp.responseXML.getElementsByTagName("message");

    var value = message[0].firstChild.nodeValue;

    setMessage(value,"true");

    }

    }

    }

    function setMessage(message, isValid) {

    var messageArea = document.getElementById("dateMessage");

    var fontColor = "red";if (isValid == "true") {

    fontColor = "green";

    }

    messageArea.innerHTML = "<font color=" + fontColor + ">" + message + " </font>";

    }

    </script>

        </head>

        <body>

           <h1>

               Ajax Validation Example

           </h1>

           Birth date:

           <input type="text" size="10" id="birthDate" ondblclick="" />

           <input type="button" value="press" onclick="validate()" />

           <div id="dateMessage"></div>

        </body>

    </html>

    5.       配置服務器

    Tomcat server : Enable

                                填寫路徑

    JDK              :填寫名稱和路徑

    Launch mode: debug mode;

    6.       設置項目項目與服務器的關聯

    Project Deployment

    選擇tomcat服務。

    posted on 2009-10-12 17:27 找個美女做老婆 閱讀(2264) 評論(1)  編輯  收藏

    評論

    # 點點滴滴滴滴滴 2014-05-29 01:03 生生世世事實上

    生生世世生生世世三三三三三三三三三三  回復  更多評論   


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    導航

    統計

    公告

    本blog已經搬到新家了, 新家:www.javaly.cn
     http://www.javaly.cn

    常用鏈接

    留言簿(6)

    隨筆檔案

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲视频无码高清在线| 国产乱子精品免费视观看片| 亚洲综合色一区二区三区| 亚洲伊人久久综合影院| 成人免费无遮挡无码黄漫视频| 免费成人在线视频观看| 一级毛片aaaaaa视频免费看| 亚洲欧美日本韩国| 亚洲大香伊人蕉在人依线| 亚洲AV日韩精品久久久久| 亚洲精品无码专区2| 亚洲va国产va天堂va久久| 国产无遮挡吃胸膜奶免费看视频| 日本阿v免费费视频完整版| 国产白丝无码免费视频| 日本高清不卡aⅴ免费网站| 九九免费观看全部免费视频| 亚洲AV无码专区在线观看成人| 亚洲一区二区三区国产精品无码| 亚洲国产精品国自产电影| 亚洲精品你懂的在线观看| 久久久久亚洲精品无码网址| 亚洲A∨精品一区二区三区| 女人让男人免费桶爽30分钟| 野花高清在线电影观看免费视频 | 国产成人AV片无码免费| www免费黄色网| 一级做a爰全过程免费视频毛片| 美女扒开尿口给男人爽免费视频| 亚洲精品乱码久久久久久蜜桃图片| 亚洲人配人种jizz| 亚洲最大的成人网站| 亚洲一区精彩视频| 亚洲国产欧美国产综合一区| 亚洲av永久中文无码精品| 亚洲精品成a人在线观看☆| 亚洲av成人片在线观看| 精品女同一区二区三区免费播放 | 在线日本高清免费不卡| 最近免费字幕中文大全视频| 四虎在线成人免费网站|