项目结构:
首先创建登录和注册界面,创建Login.jsp,register.jsp
Login.jsp
<%@ 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 lang="en">
<head>
<base href="<%=basePath%>">
<title>学生信息管理系统</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="static/Login.css">
</head>
<body>
<div id="login">
<h1>登录账号</h1>
<form method="post" action="LoginSrevlet">
<input type="text" required="required" placeholder="用户名" name="u"></input>
<input type="password" required="required" placeholder="密码" name="p"></input>
<button class="but" type="submit">安全登录</button>
<label for="password">
没有账号请
<a href="/StudentManagerSystem/register.jsp" style="color:blue">注册</a>
</label>
<br>
<label>
<%
String state=(String)session.getAttribute("A2");
session.removeAttribute("A2");//获取完之后,移出Session,防止创建使用重复
if(state!=null){//判断state是否有东西,有东西打印
out.print(state);
}
%>
</label>
</form>
</div>
</body>
</html>
register.jsp:
<%@ 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>注册页面</title>
<link rel="stylesheet" type="text/css" href="static/register.css">
<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>
<div id="login">
<h1>注册账号</h1>
<form method="post" action="RegistSrevlet">
<input type="text" required="required" placeholder="用户名" name="u"></input>
<input type="password" required="required" placeholder="密码" name="p"></input>
<button class="but" type="submit">注册</button>
<label for="password">
返回
<a href="/StudentManagerSystem/Login.jsp" style="color:blue">登入</a>
界面
</label>
<br>
<label>
<%
String state=(String)session.getAttribute("A1");
session.removeAttribute("A1");//获取完之后,移出Session,防止创建使用重复
if(state!=null){//判断state是否有东西,有东西打印
out.print(state);
}
%>
</label>
</form>
</div>
</html>
Login.css:
html{
width: 100%;
height: 100%;
overflow: hidden;
font-style: sans-serif;
}
body{
width: 100%;
height: 100%;
font-family: 'Open Sans',sans-serif;
margin: 0;
background-color: #4A374A;
}
#login{
position: absolute;
top: 50%;
left:50%;
margin: -150px 0 0 -150px;
width: 300px;
height: 300px;
}
#login h1{
color: #fff;
text-shadow:0 0 10px;
letter-spacing: 1px;
text-align: center;
}
h1{
font-size: 2em;
margin: 0.67em 0;
}
input{
width: 299px;
height: 36px;
/* margin-bottom: 10px;
outline: none;
padding: 10px; */
font-size: 13px;
color: #fff;
text-shadow:1px 1px 1px;
border-top: 1px solid #312E3D;
border-left: 1px solid #312E3D;
border-right: 1px solid #312E3D;
border-bottom: 1px solid #56536A;
border-radius: 4px;
background-color: #2D2D3F;
}
.but{
width: 300px;
min-height: 20px;
display: block;
background-color: #4a77d4;
border: 1px solid #3762bc;
color: #fff;
padding: 9px 14px;
font-size: 15px;
line-height: normal;
border-radius: 5px;
margin: 0;
}
register.css:
html{
width: 100%;
height: 100%;
overflow: hidden;
font-style: sans-serif;
}
body{
width: 100%;
height: 100%;
font-family: 'Open Sans',sans-serif;
margin: 0;
background-color: #01CCFF;
}
#login{
position: absolute;
top: 50%;
left:50%;
margin: -150px 0 0 -150px;
width: 300px;
height: 300px;
}
#login h1{
color: #fff;
text-shadow:0 0 10px;
letter-spacing: 1px;
text-align: center;
}
h1{
font-size: 2em;
margin: 0.67em 0;
}
input{
width: 299px;
height: 36px;
/* margin-bottom: 10px;
outline: none;
padding: 10px; */
font-size: 13px;
color: #fff;
text-shadow:1px 1px 1px;
border-top: 1px solid #312E3D;
border-left: 1px solid #312E3D;
border-right: 1px solid #312E3D;
border-bottom: 1px solid #56536A;
border-radius: 4px;
background-color: #2D2D3F;
}
.but{
width: 300px;
min-height: 20px;
display: block;
background-color: #4a77d4;
border: 1px solid #3762bc;
color: #fff;
padding: 9px 14px;
font-size: 15px;
line-height: normal;
border-radius: 5px;
margin: 0;
}
登录页面:
注册页面:
主页面,zhuyemian.jsp:
<%@ page language="java" import="java.util.*" import="com.bean.StudentBean" 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>学生信息管理系统</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script type="text/javascript">
//增加
function submitit1(){
document.form1.action="AddStudentServlet"
//document.form1.submit();
}
//更改
function submitit3(){
document.form1.action="ChangeStudentServlet"
//document.form1.submit();
}
//删除
function submitit2(){
document.form2.action="DelStudentServlet"
// document.form2.submit();
}
//查找
function submitit4(){
document.form2.action="ShowStudentServlet"
// document.form2 .submit();
}
</script>
<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>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<h3 class="text-center">
学生信息管理系统
</h3>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<form class="form-horizontal" role="form" name="form1" method="post">
<div class="form-group">
<label for="inputEmail31" class="col-sm-2 control-label">学号:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail31" name="A" />
</div>
</div>
<div class="form-group">
<label for="inputPassword32" class="col-sm-2 control-label">姓名:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputPassword32" name="B"/>
</div>
</div>
<div class="form-group">
<label for="inputPassword33" class="col-sm-2 control-label">班级:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputPassword33" name="C"/>
</div>
</div>
<div class="form-group">
<label for="inputPassword34" class="col-sm-2 control-label">年级:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputPassword34" name="D"/>
</div>
</div>
<div class="form-group">
<label for="inputPassword35" class="col-sm-2 control-label">宿舍:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputPassword35" name="E"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" onClick=submitit1();>添加学生信息</button>
<button type="submit" class="btn btn-default" onClick=submitit3();>更改学生信息</button>
<div class="alert alert-danger">
<a href="#" calss="alert-link">
<%
String state=(String)session.getAttribute("B1");
session.removeAttribute("B1");//获取完之后,移出Session,防止创建使用重复
if(state!=null){//判断state是否有东西,有东西打印
out.print(state);
}
%>
</a>
</div>
</div>
</div>
</form>
<form class="form-horizontal" role="form" name="form2" method="post">
<div class="form-group">
<label for="inputEmail36" class="col-sm-2 control-label">学号:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail36" name="A"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" onClick=submitit2();>删除学生信息</button>
<button type="submit" class="btn btn-default" onClick=submitit4();>查找学生信息</button>
<div class="alert alert-danger">
<a href="#" calss="alert-link">
<%
state=(String)session.getAttribute("B2");
session.removeAttribute("B2");//获取完之后,移出Session,防止创建使用重复
if(state!=null){//判断state是否有东西,有东西打印
out.print(state);
}
%>
</a>
</div>
</div>
</div>
</form>
<div class="row clearfix">
<div class="col-md-1 column">
</div>
<div class="col-md-11 column">
<table class="table">
<thead>
<tr>
<th>
学号
</th>
<th>
姓名
</th>
<th>
班级
</th>
<th>
年级
</th>
<th>
宿舍
</th>
</tr>
</thead>
<tbody>
<%
//获取连接对象
ArrayList stumess=(ArrayList) session.getAttribute("stumessage");
session.removeAttribute("stumessage");
if(stumess!=null){
//执行成功的地方
for(int i=0;i<stumess.size();i++){
StudentBean a=(StudentBean) stumess.get(i);
%>
<tr class="info">
<td>
<%=a.getSno() %>
</td>
<td>
<%=a.getSname() %>
</td>
<td>
<%=a.getScls() %>
</td>
<td>
<%=a.getSold() %>
</td>
<td>
<%=a.getSroom() %>
</td>
</tr>
<%
}
}
%>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
com.util包下:
DBUtil.java:编写连接数据库代码:
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.resource.cci.ResultSet;
import java.sql.PreparedStatement;
public class DBUtil {
public static String username="root";//账号
public static String password="123456";//密码
public static String database="student";
public static String url="jdbc:mysql://localhost:3306/"+database+"?useUnicode=true&characterEncoding=utf-8";
static{
try{
Class.forName("com.mysql.jdbc.Driver");//加载驱动
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
//连接数据库
public static Connection getConectionDb(){
Connection conn=null;
try{
conn=DriverManager.getConnection(url,username,password);//获取数据库连接
}catch(SQLException e){
e.printStackTrace();
}
return conn;
}
//关闭数据库
public static void CloseDB(ResultSet rs,PreparedStatement stm,Connection conn){
if(rs!=null){
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(stm!=null){
try{
stm.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(conn!=null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
com.dao包:
AdminDao.java:编写注册登录方法:
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.util.DBUtil;
public class AdminDao {
//注册方法
public static boolean regist(String account,String password){//传入账号和密码,用static就不用初始化
Connection conn=DBUtil.getConectionDb();
String sql="insert into ausers (admin,password) values(?,?)";
int a=0;
PreparedStatement stm=null;
boolean sta=false;
try {
stm = conn.prepareStatement(sql);
stm.setString(1, account);
stm.setString(2,password);
a=stm.executeUpdate();
if(a==0){
sta=false;
}else{
sta=true;
}
return sta;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
//登录方法
public static boolean Login(String account,String password){//传入账号和密码,用static就不用初始化
Connection conn=DBUtil.getConectionDb();
String sql="select * from ausers where admin=? and password=?";
ResultSet rs;
PreparedStatement stm=null;
boolean sta=false;
try {
stm = conn.prepareStatement(sql);
stm.setString(1, account);
stm.setString(2,password);
rs=stm.executeQuery();//查询
if(rs.next()){
sta=true;
}else{
sta=false;
}
return sta;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
StudentDao.java:学生信息的编写增删改查方法:
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.bean.StudentBean;
import com.util.DBUtil;
public class StudentDao {
//写入学生数据
public static boolean addStudent(String A,String B,String C,String D,String E){//用static就不用初始化
Connection conn=DBUtil.getConectionDb();
String sql="insert into stumes(sno,sname,scls,sold,sroom) values(?,?,?,?,?)";
int rs;
PreparedStatement stm=null;
try {
stm = conn.prepareStatement(sql);
stm.setString(1,A);
stm.setString(2,B);
stm.setString(3,C);
stm.setString(4,D);
stm.setString(5,E);
rs=stm.executeUpdate();//查询
if(rs==1){
return true;
}else{
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
//更改学生信息
public static boolean changeStudent(String A,String B,String C,String D,String E){//用static就不用初始化
Connection conn=DBUtil.getConectionDb();
String sql="update stumes set sname=?,scls=?,sold=?,sroom=? where sno=?";
int rs;
PreparedStatement stm=null;
try {
stm = conn.prepareStatement(sql);
stm.setString(1,B);
stm.setString(2,C);
stm.setString(3,D);
stm.setString(4,E);
stm.setString(5,A);
rs=stm.executeUpdate();//查询
if(rs==1){
return true;
}else{
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
//删除学生信息
public static boolean delStudent(String A){//用static就不用初始化
Connection conn=DBUtil.getConectionDb();
String sql="delete from stumes where sno=?";
int rs;
PreparedStatement stm=null;
try {
stm = conn.prepareStatement(sql);
stm.setString(1,A);
rs=stm.executeUpdate();//查询
if(rs==1){
return true;
}else{
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
//查询所有学生信息
public static ArrayList getLikeList(){
ArrayList tag_Array=new ArrayList();//创建数组集合
Connection conn=DBUtil.getConectionDb();
String sql="select * from stumes";
PreparedStatement stm=null;
ResultSet rs=null;
try{
stm=conn.prepareStatement(sql);
rs=stm.executeQuery();
while(rs.next()){
StudentBean tag=new StudentBean();
tag.setScls(rs.getString("scls"));
tag.setSno(rs.getString("sno"));
tag.setSname(rs.getString("sname"));
tag.setSold(rs.getString("sold"));
tag.setSroom(rs.getString("sroom"));
tag_Array.add(tag);
}
rs.close();
stm.close();
}catch(SQLException e){
e.printStackTrace();
}
return tag_Array;
}
//查询单个学生信息
public static ArrayList getLikeList(String id){
ArrayList tag_Array=new ArrayList();//创建数组集合
Connection conn=DBUtil.getConectionDb();
String sql="select * from stumes where sno=?";
PreparedStatement stm=null;
ResultSet rs=null;
try{
stm=conn.prepareStatement(sql);
stm.setString(1, id);
rs=stm.executeQuery();
while(rs.next()){
StudentBean tag=new StudentBean();
tag.setScls(rs.getString("scls"));
tag.setSno(rs.getString("sno"));
tag.setSname(rs.getString("sname"));
tag.setSold(rs.getString("sold"));
tag.setSroom(rs.getString("sroom"));
tag_Array.add(tag);
}
rs.close();
stm.close();
}catch(SQLException e){
e.printStackTrace();
}
return tag_Array;
}
}
com.servlet包:
LoginServlet:
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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 com.dao.AdminDao;
@WebServlet("/LoginSrevlet")
public class LoginServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginServlet() {
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 {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* 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.setContentType("text/html;charset=utf-8");//防止中文乱码
//获取登录信息
String username=request.getParameter("u");
String password=request.getParameter("p");
//调用AdminDao的登录方法
boolean a = AdminDao.Login(username, password);
//true表明注册成功
if(a==true){
//返回主页面
response.sendRedirect("/StudentManagerSystem/zhuPage.jsp");
}else{
request.getSession().setAttribute("A2", "账号或者密码错误请重新登录");
response.sendRedirect("/StudentManagerSystem/Login.jsp");
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
RegistServlet:
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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 com.dao.AdminDao;
@WebServlet("/RegistSrevlet")
public class RegistServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public RegistServlet() {
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 {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* 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.setContentType("text/html;charset=utf-8");//防止中文乱码
//获取注册信息
String username=request.getParameter("u");
String password=request.getParameter("p");
//调用AdminDao的注册方法
boolean a = AdminDao.regist(username, password);
//true表明注册成功
if(a==true){
//创建对象,存储状态
request.getSession().setAttribute("A1", "注册账号成功");
response.sendRedirect("/StudentManagerSystem/register.jsp");
}else{
request.getSession().setAttribute("A1", "注册账号失败");
response.sendRedirect("/StudentManagerSystem/register.jsp");
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
AddStudentServlet:
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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 com.dao.AdminDao;
import com.dao.StudentDao;
@WebServlet("/AddStudentServlet")
public class AddStudentServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public AddStudentServlet() {
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 {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* 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.setContentType("text/html;charset=utf-8");//防止中文乱码
//获取插入的信息
String id=request.getParameter("A");
String name=request.getParameter("B");
String cls=request.getParameter("C");
String age=request.getParameter("D");
String rooms=request.getParameter("E");
//
boolean a=StudentDao.addStudent(id, name, cls, age,rooms);
if(a==true){
request.getSession().setAttribute("B1", "学生添加成功");
response.sendRedirect("/StudentManagerSystem/zhuPage.jsp");
}else{
request.getSession().setAttribute("B1", "添加失败请检查学号");
response.sendRedirect("/StudentManagerSystem/zhuPage.jsp");
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
ChangeStudentServlet:
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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 com.dao.StudentDao;
@WebServlet("/ChangeStudentServlet")
public class ChangeStudentServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public ChangeStudentServlet() {
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 {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* 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.setContentType("text/html;charset=utf-8");//防止中文乱码
//获取插入的信息
String id=request.getParameter("A");
String name=request.getParameter("B");
String cls=request.getParameter("C");
String age=request.getParameter("D");
String rooms=request.getParameter("E");
//
boolean a=StudentDao.changeStudent(id, name, cls, age,rooms);
if(a==true){
request.getSession().setAttribute("B1", "更改学生信息添加成功");
response.sendRedirect("/StudentManagerSystem/zhuPage.jsp");
}else{
request.getSession().setAttribute("B1", "更改学生信息失败");
response.sendRedirect("/StudentManagerSystem/zhuPage.jsp");
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
DeleteStudentServlet:
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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 com.dao.StudentDao;
@WebServlet("/DelStudentServlet")
public class DelStudentServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public DelStudentServlet() {
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 {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* 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.setContentType("text/html;charset=utf-8");//防止中文乱码
//获取id的信息
String id=request.getParameter("A");
//
boolean a=StudentDao.delStudent(id);
if(a==true){
request.getSession().setAttribute("B2", "删除学生信息成功");
response.sendRedirect("/StudentManagerSystem/zhuPage.jsp");
}else{
request.getSession().setAttribute("B2", "删除学生信息失败");
response.sendRedirect("/StudentManagerSystem/zhuPage.jsp");
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
ShowStudentServlet:
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
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 com.dao.StudentDao;
@WebServlet("/ShowStudentServlet")
public class ShowStudentServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public ShowStudentServlet() {
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 {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* 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.setContentType("text/html;charset=utf-8");//防止中文乱码
//获取id的信息
String id=request.getParameter("A");
ArrayList a;
if(id.equals("")){
//查询所有
a=StudentDao.getLikeList();
request.getSession().setAttribute("B2", "查询学生信息成功");
}else{
a=StudentDao.getLikeList(id);
request.getSession().setAttribute("B2", "查询成功");
}
//
request.getSession().setAttribute("stumessage", a);
response.sendRedirect("/StudentManagerSystem/zhuPage.jsp");
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
com.bean包:
StudentBean.java:
package com.bean;
public class StudentBean {
private String sno;
private String sname;
private String scls;
private String sold;
private String sroom;
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getScls() {
return scls;
}
public void setScls(String scls) {
this.scls = scls;
}
public String getSold() {
return sold;
}
public void setSold(String sold) {
this.sold = sold;
}
public String getSroom() {
return sroom;
}
public void setSroom(String sroom) {
this.sroom = sroom;
}
}
数据库创建:
ausers用户登录表:
stumes学生信息表:
测试:
注册用户登录:
创建min520 密码123456
点击登入按钮,进行登录:
点击安全登录:
点击添加学生信息:
把宿舍改为201:
删除李晓的信息:
查找学号31190701的信息 :
点击查询学生信息: