最近开始学习AJAX,这是我的第一个程序,虽然也是抄的但是还是可以吧!已经开始上手了!呵呵!现在我把代码共享出来,希望和我一样在AJAX门外的人,一起努力!
首行要写一个Java 类
package classmate;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginAction extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
}
/*
* 处理
请求方法.
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置接收信息的字符集
request.setCharacterEncoding("UTF-8");
//接收浏览器端提交的信息
String uname = request.getParameter("uname");
String psw = request.getParameter("psw");
//设置输出信息的格式及字符集
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
//创建输出流对象
PrintWriter out = response.getWriter();
//依据验证结果输出不同的数据信息
out.println("");
if(uname.equals("jenny") && psw.equals("hi")){
out.println("" + "热烈的欢迎您!" + "");
}else{
out.println("" + "对不起,登录失败!" + "");
}
out.println("");
out.close();
}
/*
* 处理 请求方法.
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
下面就是页面的显示了!
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
最后最重要的是XML的配置!
ms1
classmate.LoginAction
ms1
/login
阅读(1037) | 评论(0) | 转发(0) |