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 找个美女做老婆 阅读(2141) 评论(1)  编辑  收藏

评论

# 点点滴滴滴滴滴 2014-05-29 01:03 生生世世事实上

生生世世生生世世三三三三三三三三三三  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

公告

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

常用链接

留言簿(5)

随笔档案

文章档案

搜索

最新评论

阅读排行榜

评论排行榜