大家好,最近一段时间没发帖子了,有些退步,不过还是感谢很多牛人对我所发的很多问题的解答。下面我把我最新的问题给发出来,希望大家帮忙指正错误。
这是一个关于AJAX学习的小例子,《Struts+Spring+Hibernate开发入行真功夫》电子工业出版社 一书。上面的一段讲解AJAX的代码,我在实现的时候出现了问题,主要是参数没有返回回来,请各位牛人帮我看看问题到底出在哪里。工程的名字是ajaxDemo(1)首先是WebRoot下的index.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script>
function createXMLHttp(){
var request;
var browser=navigator.appName;
if(browser=="Microsoft Internet Explorer"){
request=new ActiveXObject("Microsoft.XMLHttp");
return request;
}else{
request=new XMLHttpRequest();
return request;
}
}
var xhr=createXMLHttp();
function HelloSunYang(){
var url="Hello.do?name=" + document.forms[0].name.value;
//tiaozhuan
alert(url);
xhr.open("GET",url,true);
xhr.onreadystatechange=getHello ;
xhr.send();
}
function getHello(){
//panduan XMLHttpRequest zhuangtai
if(xhr.readyState==4){
//shezhibianliang helloStr dezhi
var helloStr=xhr.responseTest;
alert(helloStr);
document.getElementById("hello").innerHTML=helloStr;
}
}
</script>
</head>
<body>
This is my JSP page. <br>
<table>
<tr>
<td>hello sunyang</td>
</tr>
<tr><td><form> your name: <input type="text" name="name" value=""/></form></td></tr>
<tr>
<td>
<input type="button" name="" value="hello" onclick="return HelloSunYang();"/>
</td>
</tr>
<tr>
<td>
<div id="hello"></div>
</td>
</tr>
</table>
</body>
</html>
(2)然后是配置文件WebRoot\WEB-INF下的web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>helloAJAX</servlet-name>
<servlet-class>sunyang.Hello</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>helloAJAX</servlet-name>
<url-pattern>/servlet/Hello.do</url-pattern>
</servlet-mapping>
</web-app>
(3)然后是src\sunyang下的sverlet文件:
package sunyang;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.RequestDispatcher;
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 name=request.getParameter("name");
String result="Hello"+name;
System.out.println(result);
request.setAttribute("name", result);
RequestDispatcher rd=request.getRequestDispatcher("hello.jsp");
rd.forward(request, response);
System.out.println(request);
System.out.println(response);
} /**
* 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 occurs
*/
public void init() throws ServletException {
// Put your code here
}}
(4)最后是显示文本WebRoot下的hello.jsp:
<html>
<body>
<%=request.getAttribute("name") %>
</body>
</html>请大家帮我看看问题出在哪里,值传不过去啊。
这是一个关于AJAX学习的小例子,《Struts+Spring+Hibernate开发入行真功夫》电子工业出版社 一书。上面的一段讲解AJAX的代码,我在实现的时候出现了问题,主要是参数没有返回回来,请各位牛人帮我看看问题到底出在哪里。工程的名字是ajaxDemo(1)首先是WebRoot下的index.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script>
function createXMLHttp(){
var request;
var browser=navigator.appName;
if(browser=="Microsoft Internet Explorer"){
request=new ActiveXObject("Microsoft.XMLHttp");
return request;
}else{
request=new XMLHttpRequest();
return request;
}
}
var xhr=createXMLHttp();
function HelloSunYang(){
var url="Hello.do?name=" + document.forms[0].name.value;
//tiaozhuan
alert(url);
xhr.open("GET",url,true);
xhr.onreadystatechange=getHello ;
xhr.send();
}
function getHello(){
//panduan XMLHttpRequest zhuangtai
if(xhr.readyState==4){
//shezhibianliang helloStr dezhi
var helloStr=xhr.responseTest;
alert(helloStr);
document.getElementById("hello").innerHTML=helloStr;
}
}
</script>
</head>
<body>
This is my JSP page. <br>
<table>
<tr>
<td>hello sunyang</td>
</tr>
<tr><td><form> your name: <input type="text" name="name" value=""/></form></td></tr>
<tr>
<td>
<input type="button" name="" value="hello" onclick="return HelloSunYang();"/>
</td>
</tr>
<tr>
<td>
<div id="hello"></div>
</td>
</tr>
</table>
</body>
</html>
(2)然后是配置文件WebRoot\WEB-INF下的web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>helloAJAX</servlet-name>
<servlet-class>sunyang.Hello</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>helloAJAX</servlet-name>
<url-pattern>/servlet/Hello.do</url-pattern>
</servlet-mapping>
</web-app>
(3)然后是src\sunyang下的sverlet文件:
package sunyang;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.RequestDispatcher;
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 name=request.getParameter("name");
String result="Hello"+name;
System.out.println(result);
request.setAttribute("name", result);
RequestDispatcher rd=request.getRequestDispatcher("hello.jsp");
rd.forward(request, response);
System.out.println(request);
System.out.println(response);
} /**
* 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 occurs
*/
public void init() throws ServletException {
// Put your code here
}}
(4)最后是显示文本WebRoot下的hello.jsp:
<html>
<body>
<%=request.getAttribute("name") %>
</body>
</html>请大家帮我看看问题出在哪里,值传不过去啊。
var url="servlet/Hello.do?name=" + document.forms[0].name.value;
responseText写成了responseTest
unction getHello(){
var helloStr=xhr.responseText;
}
路径要加/
RequestDispatcher rd=request.getRequestDispatcher("/hello.jsp");
xhl.send(),改为xhl.send(null);
服务器端向页面发送数据不正确,应该如下:
PrintWriter out=response.getWriter();
out.println("hello"+name);
out.flush();
out.close();
var url="servlet/Hello.do?name=" + document.forms[0].name.value;
这个应该怎么改呢?
<url-pattern>/Hello.do </url-pattern> 就可以了
可是为什么呢,能不能给解答一下,谢谢
其实最要命的还是路径问题
var url="Hello.do?name=" + document.forms[0].name.value;
改成了var url="servlet/Hello.do?name=" + document.forms[0].name.value;
与配置文件一致
不过如上面我说道的问题:
“
将 <url-pattern>/servlet/Hello.do </url-pattern> 改成
<url-pattern>/Hello.do </url-pattern> 就可以了
可是为什么呢,能不能给解答一下,谢谢
”
其实这个不是主要问题
主要是后面的hello.java中
RequestDispatcher rd=request.getRequestDispatcher("hello.jsp");
hello.jsp的路径由于“<url-pattern>/servlet/Hello.do </url-pattern>”的原因被默认成了/servlet/hello.jsp了。所以就出现找不到的异常情况了
感觉配置路径真够麻烦的。