一个简单的ajax例子的问题

雪地漫步 2009-05-23 12:05:23
一个简单的ajax验证用户姓名的例子的问题:我一点击‘验证’按钮,本来应该出现另外的一个页面,结果我一点就弹出要迅雷下载,这是怎么回事啊?有满意答案了在给分了,急死了!
下面是源码:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="2.5">

<servlet>
<servlet-name>SrcServlet</servlet-name>
<servlet-class>SrcServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>SrcServlet</servlet-name>
<url-pattern>/SrcServlet</url-pattern>
</servlet-mapping>
</web-app>




SrcServlet.java
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;


public class SrcServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException {
try{
response.setContentType("html/text;charset-GB2312");
PrintWriter out = response.getWriter();
String old = request.getParameter("name");
if(old == null || old.length() == 0){
out.println("用户名不能为空!");
}else{
String name = new String(old.getBytes("ISO8859-1"));
if(name.equals("xuedi")){
out.println("用户名【"+name+"】已经存在,请使用其他用户名!");
}else{
out.println("用户名【"+name+"】尚未使用,可以使用该用户名!");
}
}
out.println("<br/><a href=\"index.html\">返回校验页面!</a>");
}catch(Exception e){
e.printStackTrace();
}
}
}




index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>hehe</title>
</head>
<body>
校验用户名是否存在的例子<br/>
<form action="SrcServlet" method="GET">
用户名:<input type="text" name="name" />
<input type="submit" value="校验" />
</form>
</body>
</html>
...全文
54 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
雪地漫步 2009-05-24
  • 打赏
  • 举报
回复
非常感谢了,恩,是乱码问题,不过怎么不能加分呢?不好意思了
tobeno2 2009-05-24
  • 打赏
  • 举报
回复
试试
response.setContentType("text/html;charset-GB2312");

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧