package ajax;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
public class myServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
OutputStream out = resp.getOutputStream();
// out.write("hell world!".getBytes());
Map map=new HashMap();
map.put("name", "小名");
map.put("age",18);
map.put("sex", "男");
Object obj=JSON.toJSON(map);
out.write(obj.toString().getBytes());
out.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(req, resp);
}
}
package com.cvs.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.cvs.model.User;
import com.cvs.util.DBUtil;
public class UserDao {
public List<User> findAllUsers() {
List list = new ArrayList();
Connection conn = DBUtil.getConnection();
try {
PreparedStatement ps = conn.prepareStatement("select * from user ");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
User user = new User();
user.setName("");
user.setPassword("");
String name = rs.getString("username");
String password = rs.getString("password");
user.setName(name);
user.setPassword(password);
list.add(user);
}
rs.close();
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {// 无论有没有异常都会执行
DBUtil.closeConn();
}
return list;
}
public User findUserByName(String username) {
User user = new User();
user.setName("");
user.setPassword("");
Connection conn = DBUtil.getConnection();
try {
PreparedStatement ps = conn
.prepareStatement("select * from user where username=?");
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
String name = rs.getString("username");
String password = rs.getString("password");
user.setName(name);
user.setPassword(password);
}
rs.close();
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {// 无论有没有异常都会执行
DBUtil.closeConn();
}
return user;
}
public Boolean addUser(User user) {
Connection conn = DBUtil.getConnection();
boolean flg = false;
try {
PreparedStatement ps = conn
.prepareStatement("insert into user(username,password,sex,hometown,birthday,ismery,address) value (?,?,?,?,?,?,?)");
ps.setString(1, user.getName());
ps.setString(2, user.getPassword());
ps.setString(3, user.getSex());
ps.setString(4, user.getHometown());
ps.setString(5, user.getBirthday());
ps.setString(6, user.getSex());
ps.setString(7, user.getAddress());
flg = ps.execute();
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.closeConn();
}
return true;
}
}
package com.cvs.model;
public class User {
private int id;
private String name;
private String password;
private String sex;
private String birthday;
private String hometown;
private String address;//现在住址
private String isMer;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getIsMer() {
return isMer;
}
public void setIsMer(String isMer) {
this.isMer = isMer;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getHometown() {
return hometown;
}
public void setHometown(String hometown) {
this.hometown = hometown;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + ", sex=" + sex + ", birthday="
+ birthday + ", hometown=" + hometown + "]";
}
}
package com.cvs.servlert;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.cvs.dao.UserDao;
import com.cvs.model.User;
public class LoginServlert extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
String username=req.getParameter("username");
String password=req.getParameter("password");
UserDao userDao=new UserDao();
User user=userDao.findUserByName(username);
Map map=new HashMap();
if (null!=user&&user.getName().equals(username)&&user.getPassword().equals(password)){
map.put("status", 1);
map.put("msg", "登录成功!");
}else{
map.put("status", 0);
map.put("msg", "登录失败!!!");
}
OutputStream out= resp.getOutputStream();
String json= JSON.toJSONString(map);
out.write(json.getBytes());
out.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(req, resp);
}
}
package com.cvs.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
private static Connection conn=null;
//打开链接
public static Connection getConnection () {
//加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");//加载驱动
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/qqs", "root", "123456");//获取连接
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
//关闭链接
public static void closeConn() {
if(null!=conn) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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%>">
<script type="text/javascript" src="js/jquery.js"></script>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<input type="text" id="context"><span id="sp1">请输入用户名</span>
</body>
<script type="text/javascript">
$("#context").blur(function(){
$("#b001").click(function(){
$.ajax(
{
url:"asd",
success:function(data){
$("#sp1").text(data);
}
}
);
});
})
/*$("#b001").click(function(){
$.ajax(
{
url:"abc.html",
success:function(data){
$("#myid").html(data);
}
}
);
});*/
</script>
<html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'login.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 type="text/javascript" src="js/jquery.js">
</script>
</head>
<body>
<form action="">
<input name="username" id="username" type="text">
<input name="password" id="password" type="password">
<input type="button" id="b001" value="登录">
</form>
<script type="text/javascript">
$("#b001").click(function() {
var username = $("#username").val();
var password = $("#password").val();
$.ajax( {
url : "login",
dataType : "json",
data : {
"username" : username,
"password" : password
},
success : function(data) {
alert(data.status);
window.location.href = "success.jsp";
}
});
});
</script>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'success.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">
-->
</head>
<body>
登录成功!!!
</body>
</html>
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>
<servlet-name>abc</servlet-name>
<servlet-class>ajax.myServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/asd</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.cvs.servlert.LoginServlert</servlet-class>
</servlet>
<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>FindUsersServlet</servlet-name>
<servlet-class>com.cvs.servlert.FindUsersServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FindUsersServlet</servlet-name>
<url-pattern>/servlet/FindUsersServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>users</servlet-name>
<servlet-class>com.cvs.servlert.FindUsersServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>users</servlet-name>
<url-pattern>/users</url-pattern>
</servlet-mapping>
</web-app>