package com.Servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.Mysql.DButil;
@WebServlet(name="XiaQi",urlPatterns="/XiaQi")
public class XiaQi extends HttpServlet{
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public XiaQi() {
super();
// TODO Auto-generated constructor stub
}
//老人用户登录
public void loginoldman(HttpServletRequest request, HttpServletResponse response) throws IOException {
String telphone = request.getParameter("username");
String password = request.getParameter("password");
// response.sendRedirect("/resthome/main/main.html");
String sql = "select * from t_lr where telphone = '" + telphone + "' and password = '" + password + "'";
//如果查询关键字不为空,则重定义SQL语句
ResultSet rs =DButil.executeQuery(sql);
try {
if(rs.next())
{
DButil.close();
HttpSession session=request.getSession();
session.setAttribute("telphone", telphone);
session.setAttribute("user", "老人");
response.sendRedirect("../oldman/oldpeople.html");
}else{
PrintWriter out = response.getWriter();
out.print("登录失败,请重新登陆!");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//管理用户登录
public void loginadmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
String telphone = request.getParameter("username");
String password = request.getParameter("password");
// response.sendRedirect("/resthome/main/main.html");
System.out.println(telphone);
String sql = "select * from t_yh where telphone = '" + telphone + "' and password = '" + password + "'";
//如果查询关键字不为空,则重定义SQL语句
ResultSet rs =DButil.executeQuery(sql);
try {
if(rs.next())
{
DButil.close();
HttpSession session=request.getSession();
session.setAttribute("telphone", telphone);
session.setAttribute("user", "管理员");
response.sendRedirect("../main/main.html");
}else{
PrintWriter out = response.getWriter();
out.print("登录失败,请重新登陆!");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//老人注册
public void zhuce(HttpServletRequest request, HttpServletResponse response) throws IOException {
//response.sendRedirect("/resthome/main/error.html");
String yhName = request.getParameter("yhName");
String sex = request.getParameter("sex");
String age = request.getParameter("age");
String birthday = request.getParameter("birthday");
String nation = request.getParameter("nation");
String xueli = request.getParameter("xueli");
String telphone = request.getParameter("telphone");
String password = request.getParameter("password");
String sql = "select * from t_lr where telphone = '" + telphone + "' ";
//如果查询关键字不为空,则重定义SQL语句
ResultSet rs =DButil.executeQuery(sql);
try {
if(rs.next()) {
response.sendRedirect("../main/error.html");
}
else {
String ssql = "INSERT INTO t_lr(yhName,age,sex,birthday,telphone,password,nation,xueli) VALUES ('" + yhName + "','" + age + "','" + sex+ "','" + birthday+ "','" + telphone+ "','" + password+ "','" + nation+ "','" + xueli+"')";
//如果查询关键字不为空,则重定义SQL语句
DButil.executeUpdate(ssql);
DButil.close();
response.sendRedirect("../main/login.jsp");
}
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//更改老人密码
public void updateOldPassword(HttpServletRequest request, HttpServletResponse response) throws IOException
{
String password = request.getParameter("password");
String telphone = request.getParameter("telphone");
// PrintWriter out = response.getWriter();
System.out.println(telphone+" "+password+" ");
String sql1 = "select * from t_lr where telphone = '" + telphone + "'";
ResultSet rs = DButil.executeQuery(sql1);
try {
if(rs.next())
{
String sql = "UPDATE t_lr SET password=? WHERE telphone=?";
PreparedStatement ps = DButil.executePreparedStatement(sql);
ps.setString(1, password);
ps.setString(2, telphone);
ps.executeUpdate();
//执行SQL语
ps.close();
PrintWriter out = response.getWriter();
out.print("更新密码成功");
}else
{
PrintWriter out = response.getWriter();
out.print("error");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DButil.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
//请求转发
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
String method = request.getParameter("method");
if(method.equals("loginoldman"))
{
loginoldman(request,response);
}
if(method.equals("loginadmin"))
{
loginadmin(request,response);
}
if(method.equals("zhuce"))
{
zhuce(request,response);
}
if(method.equals("updateOldPassword"))
{
updateOldPassword(request,response);
}
if(method.equals("zhuxiao"))
{
HttpSession session=request.getSession();
session.removeAttribute("username");
response.sendRedirect("/resthome/main/main_zhangweinan.html");
}
}
}