package com.sl.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sl.bean.MyConnection;
import com.sl.bean.Student;
import com.sl.bean.User;
import com.sl.service.InterStudentService;
import com.sl.service.StudentService;
@SuppressWarnings("serial")
public class Studentservlet extends HttpServlet {
/**
* Constructor of the object.
*/
public Studentservlet() {
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 {
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
Connection connection = MyConnection.getConnection();//创建数据库连接对象
String username = request.getParameter("username");//获取表单信息
String password = request.getParameter("password");//获取表单信息
System.out.println(username);
String sql="insert into admin values(?,?)";//定义SQL语句
try
{
PreparedStatement pre=connection.prepareStatement(sql);
pre.setString(1, username);
pre.setString(2, password);
pre.executeUpdate();
System.out.println("用户添加成功!");
} catch (SQLException e1)
{
e1.printStackTrace();
}
RequestDispatcher rep = request.getRequestDispatcher("/login.jsp");//转发
rep.forward(request, response);
/*response.sendRedirect("/login.jsp"); */ // 重定向
}
/**
* 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
{
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String Item = new String(request.getParameter("sub"));//跟据sub的值来判断是从那个按钮提交的表单信息
Connection connection = MyConnection.getConnection();//创建数据库连接对象
StudentService ser = new InterStudentService();//创建service层的对象
@SuppressWarnings("unused")
User us = new User();//创建一个管理用户属性对象
Student stu = new Student();//创建学生属性对象
/****start****管理员用户管理****start****/
//用户登陆
if (Item.equals("登陆"))
{
response.setContentType("text/html;charset=gbk");
@SuppressWarnings("unused")
PrintWriter out = response.getWriter();
String username = request.getParameter("username");//获取表单信息
String password = request.getParameter("password");//获取表单信息
username =URLDecoder.decode(username,"utf-8");//对表单数据进行转码
password =URLDecoder.decode(password,"utf-8");//对表单数据进行转码
System.out.println("username===================="+username);
System.out.println("password===================="+password);
String sql = "select*from admin";
boolean f = false;
try
{
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery(sql);
while(rs.next())
{
String userId = rs.getString("username");
String passwordId = rs.getString("password");
if (username.equals(userId)&& password.equals(passwordId))
{
f =true;
}
}
} catch (Exception e)
{
e.printStackTrace();
}
if (f)
{
RequestDispatcher rep = request.getRequestDispatcher("/loginok.jsp");//重定向
rep.forward(request, response);
}
else
{
RequestDispatcher rep = request.getRequestDispatcher("/loginerror.jsp");//重定向
rep.forward(request, response);
}
}
//注册
if (Item.equals("注册"))
{
System.out.println("zhuce==============");
String username = request.getParameter("username");//获取表单信息
String password = request.getParameter("password");//获取表单信息
System.out.println(username);
String sql="insert into admin values(?,?)";//定义SQL语句
try
{
PreparedStatement pre=connection.prepareStatement(sql);
pre.setString(1, username);
pre.setString(2, password);
pre.executeUpdate();
System.out.println("用户添加成功!");
} catch (SQLException e1)
{
e1.printStackTrace();
}
RequestDispatcher rep = request.getRequestDispatcher("/login.jsp");//重定向
rep.forward(request, response);
}
/****end****管理员用户管理****end****/
/****start****学生操作功能管理****start****/
//增加
if (Item.equals("增加"))
{
System.out.println("StudentServlet.doPost()增加");
String stuid = request.getParameter("stuid");
String stuname = request.getParameter("stuname");
String sex = request.getParameter("sex");
String birthday = request.getParameter("birthday");
//String age = request.getParameter("age");
String age = "22";
String email = request.getParameter("email");
//String nativ = request.getParameter("nativ");
String nativ = "余干县";
if (stuid !=null&&stuname!=null&& age!=null&&sex!=null&&birthday!=null&&nativ!=null)
{
stu.setStuid(stuid);
stu.setStuname(stuname);
stu.setSex(sex);
stu.setBirthday(birthday);
stu.setAge(age);
stu.setEmail(email);
stu.setNativ(nativ);
boolean is1 = ser.add(stu);
request.setAttribute("is", is1);
}
RequestDispatcher rep = request.getRequestDispatcher("/added.jsp");//重定向
rep.forward(request, response);
}
//查询
if (Item.equals("查询主界面"))
{
System.out.println("Stu查询查询查询查询查询dentservlet.doPost()");
List<Student> list = new ArrayList<Student>();
list=ser.querry();
request.setAttribute("list", list);
System.out.println("Studentservlet.doPost()=查询查询查询查询查询=====");
RequestDispatcher rep = request.getRequestDispatcher("/query.jsp");
rep.forward(request, response);
}
//修改
if (Item.equals("修改"))
{
System.out.println("Stu查询查询查询查询查询dentservlet.doPost()");
List<Student> list = new ArrayList<Student>();
String stuid = request.getParameter("stuid");
String stuname = request.getParameter("stuname");
String sex = request.getParameter("sex");
String birthday = request.getParameter("birthday");
//String age = request.getParameter("age");
String email = request.getParameter("email");
String nativ = request.getParameter("nativ");
if (stuid!=null&&stuname!=null&& sex!=null&&birthday!=null&&email!=null&&nativ!=null)
{
stu.setStuid(stuid);
stu.setStuname(stuname);
stu.setSex(sex);
stu.setBirthday(birthday);
//stu.setAge(age);
stu.s
- 1
- 2
- 3
前往页