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 找個美女做老婆 閱讀(2269) 評論(1)  編輯  收藏

          評論

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

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


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


          網站導航:
           

          導航

          統計

          公告

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

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 蚌埠市| 虹口区| 金川县| 南华县| 天镇县| 大城县| 莱芜市| 浑源县| 新田县| 台江县| 常德市| 商丘市| 台南县| 岳西县| 平凉市| 苍山县| 如东县| 增城市| 界首市| 临汾市| 三江| 扶风县| 拜城县| 双江| 尼勒克县| 墨江| 西贡区| 若尔盖县| 嫩江县| 昭通市| 富阳市| 大名县| 漠河县| 沧源| 东阳市| 固始县| 荣昌县| 大石桥市| 巢湖市| 思南县| 西吉县|