Ajax_03|使用jQuery执行load方法
package com.tx.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DemoServlet02 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("收到了请求");
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("给你一份数据");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Ajax_02_load</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 这些servlet代码是创建servlet时自动生成的 -->
<servlet>
<description></description>
<display-name>DemoServlet02</display-name>
<servlet-name>DemoServlet02</servlet-name>
<servlet-class>com.tx.servlet.DemoServlet02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DemoServlet02</servlet-name>
<url-pattern>/DemoServlet02</url-pattern>
</servlet-mapping>
</web-app>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
function load(){
$("#text01").load("/Ajax_02/DemoServlet02",function(responseText){ //load("Dome1");代表去加载这个路径的值.这里写的是servlet的相对路径地址
$("#text01").val(responseText);
});
}
</script>
</head>
<body>
<!-- 为什么传不了值到下面的文本框里面,是因为这a标签是一个超链接,一点击就会刷到别的页面上,所以本页面是有值,但很快就被刷掉了 -->
<!-- <h3><a href="" onclick="load()">使用jQuery执行load方法</a></h3> -->
<h3><input type="button" onclick="load()" value="使用jQuery执行load方法"/></h3>
<input type="text" id="text01" />
</body>
</html>
目录大纲 Directory outline