盖伦:阿巴阿巴阿巴阿巴
前言
水一下,水一下。来不及了,好多还没完善,我也先不说明了。看代码吧。
注意!
方法不是最佳的。
缺少各种的filter
工作分配,业务逻辑,联系不是最佳
操作不全
可以连接数据库
登录判断
注册
显示表单内容
并进行删除
一、链接数据库以及操作
获得接口
- connection
package com.example.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class connection {
static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
static final String DB_URL="jdbc:mysql://127.0.0.1:3306/information?user=root&password=123456&useUnicode=true&characterEncoding=UTF-8&useSSL=true";
//3306是端口,information是已有的数据库(database),root是用户名,123456是相应的密码
static Connection conn=null;
//连接数据库
public static Connection getConnection() {
try {
Class.forName(JDBC_DRIVER);
conn=DriverManager.getConnection(DB_URL);
System.out.println("连接成功");
}catch(Exception e) {
e.printStackTrace();
}
return conn;
}
//关闭数据库连接
public static void Close() {
try {
if(conn!=null) {
conn.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
操作
- operation
package com.example.test;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class operation {
static Connection connection1;
static List<User> studentList= new ArrayList<>() ;
static {
connection1 = connection.getConnection();
System.out.println("连接成功");
}
static Statement statement;
static String ac;
static String table;
static {
try {
statement = connection1.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
static void user(String account)//判断是学生还是老师
{
if(account.charAt(0)=='t')
{
ac="id";
table="teacher";
}
else
{
ac="sno";
table="student";
}
}
public static List<User> selectAll() throws SQLException {//在服务器显示查询结果
List<User> list = new ArrayList<User>();
String sql = "select * from "+table;
String name;
String sno;
String password;
System.out.println("查询结果如下:");
System.out.printf("%-10s","name" );
System.out.printf("%-10s",ac);
System.out.printf("%-10s\n","password");
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
name = resultSet.getString("name");
sno = resultSet.getString(ac);
password = resultSet.getString("password");
User stu = new User();
stu.setName(name);
stu.setSno(sno);
stu.setPassword(password);
list.add(stu);
System.out.printf("%-10s",name);
System.out.printf("%-10s",sno);
System.out.printf("%-10s\n",password);
}
resultSet.close();
return list;
}
public static List<User> getlist() throws SQLException {//这没有意义就是想这么写
return selectAll();
}
public static boolean exist(String account,String password) throws SQLException {//
boolean t = false;
String sql = "select * from "+table;
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
if(resultSet.getString(ac).equals(account)&&resultSet.getString("password").equals(password))
t = true;
}
resultSet.close();
return t;
}
public static boolean exist(String account) throws SQLException {
boolean t = false;
String sql = "select "+ac+" from "+table+" where "+ac+" ="+"'"+account+"'";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
return true;
}
resultSet.close();
return t;
}
public static boolean delete(String account ) throws SQLException {
String sql = "delete from "+table+" where "+ac+" = "+"'"+account+"'";
int n = statement.executeUpdate(sql);
if(n!=0)
System.out.println("用户"+account+"删除成功");
else
System.out.println("用户"+account+"删除失败");
return n!=0;
}
public static boolean insert (String name ,String account,String password ) throws SQLException {
boolean t = false;
int n = 0;
String sql = "insert into "+table+" values ('"+name+"','"+account+"','"+password+"')";
if(!exist(account)) {
n = statement.executeUpdate(sql);
t = true ;
}
if(n!=0)
System.out.println(name+","+account+","+password+"插入成功");
else
System.out.println(name+","+account+","+password+"插入失败");
return t;
}
public static void main(String[] args) throws SQLException {
user("tkjk");
List<User> list = getlist();
delete("sjad");
insert("dsak","sja9000","sadlk");
statement.close();
connection1.close();
}
}
二、servlet,filter,bean
- loginFilter
package com.example.test;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.sql.SQLException;
@WebFilter(filterName = "Filter")
public class loginFilter implements Filter {
public void init(FilterConfig config) throws ServletException {
}
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
HttpServletRequest request1=(HttpServletRequest)request;
String account = request1.getParameter("account");
String password= request1.getParameter("password");
operation.user("s");
boolean t=true;
if(account==null||account.length()==0){
request.setAttribute("accountNull", "Please input a account");
t=false;
}
else
request.setAttribute("account", account);
if(password==null||password.length()==0) {
request.setAttribute("passwordNull", "Please input a password");
t=false;
}
if(!t) {
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
else {
try {
if(operation.exist(account,password))
{
chain.doFilter(request,response);
}
else{
request.setAttribute("errorMsg", "Wrong user name or password");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
- deleteServlet
package com.example.test;
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 java.io.IOException;
import java.sql.SQLException;
@WebServlet(name = "deleteServlet", value = "/deleteServlet")
public class deleteServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
operation.user("s");
String sno = request.getParameter("id");
try {
operation.delete(sno);
} catch (SQLException e) {
e.printStackTrace();
}
request.getRequestDispatcher("/student.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
- registerservlet
package com.example.test;
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 java.io.IOException;
import java.sql.SQLException;
@WebServlet(name = "register", value = "/register")
public class registerservlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String account = request.getParameter("account");
String password = request.getParameter("password");
String name = request.getParameter("name");
operation.user("s");
try {
if(operation.exist(account))
{
request.setAttribute("error","已存在!请重新输入");
request.getRequestDispatcher("/register.jsp").forward(request, response);
}
else
{
if(operation.insert(name,account,password))
request.getRequestDispatcher("/registersucceed.jsp").forward(request, response);
}
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("error","请输入完整信息");
request.getRequestDispatcher("/register.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
- user
package com.example.test;
public class User {
String name;
String sno;
String password;
public User() {
super();
// TODO Auto-generated constructor stub
}
public void setName(String name) {
this.name = name;
}
public void setSno(String sno) {
this.sno = sno;
}
public void setPassword(String password) {
this.password = password;
}
public String getName(){
return this.name;
}
public String getSno() {
return this.sno;
}
public String getPassword()
{
return this.password;
}
}
三、页面
登陆页面
- index.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
<script>
function zhuce(){
document.login.action="register.jsp";
document.login.submit();
}
function qingchu()
{
document.getElementById("account").value="";
document.getElementById("password").value="";
}
</script>
<title>Title</title>
</head>
<body>
<form action="index.jsp" name="login">
<table border="1">
<tr>
<td>account:</td>
<td height="30" color="red"><input type="text" id="account" name="account" value=${account} ><font color="red">${errorMsg} ${accountNull}</font></td>
</tr>
<tr>
<td>password:</td>
<td height="30" color="red"><input type="password" id="password" name="password"><font color="red"> ${passwordNull}</font></td>
</tr>
<tr>
<td height="30" style="text-align: center" colspan="2">
<input type="submit" value="login">
<input type="button" name="reset" value="reset" onclick=qingchu()>
<input type="button" name="register" value="register" onclick=zhuce()>
</td>
</tr>
</table>
</form>
</body>
</html>
注册页面
- register.jsp
<%--
Created by IntelliJ IDEA.
User: 段佳鑫
Date: 2022/5/11
Time: 0:31
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>账号注册</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/registerServlet">
<label>
<input type="text" name="name" placeholder="请输入姓名">
</label><br>
<label>
<input type="text" name="account" placeholder="请输入学号">
</label><br>
<label>
<input type="password" name="password" placeholder="请输入密码">
</label><br>
<label>
<input type="submit" value="注册">
</label><br>
</form>
<p style="color: red">${error}</p>
</body>
</html>
注册成功页面
- registersucceed.jsp
<%--
Created by IntelliJ IDEA.
User: 段佳鑫
Date: 2022/5/14
Time: 17:54
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>注册成功!!!</h1>
<%
String str="index.jsp?account="+"fads"+""+"&"+"password="+"fadsf"+"";
%>
<a href="<%=str%>">进入用户页面</a><br>
<a href="login.jsp">返回登录界面</a>
</body>
</html>
用户页面
- index.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<h1><%= "Hello World!" %>
</h1>
<br/>
<a href="hello-servlet">Hello Servlet</a><br>
<a href="student.jsp">表单</a>
</body>
</html>
表单页面
- student
<%@ page import="java.util.List" import="com.example.myweb.*" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="com.example.test.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%--
Created by IntelliJ IDEA.
User: 段佳鑫
Date: 2022/5/15
Time: 9:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<script>
function shanchu(){
}
</script>
<%
List<User> list = null;
try {
list = operation.getlist();
} catch (SQLException e) {
e.printStackTrace();
}
assert list != null;
%>
<%
request.setAttribute("LIST", list);
%>
<p style="color: #b74646;font-size: 30px"><%=list.size()%></p><br>
<c:out value="hello world"/>
<form action="" name="delete">
<table border="1">
<tr>
<td>姓名</td>
<td>学号</td>
<td>密码</td>
<td>操作</td>
</tr>
<%for(int i=0;%><%i<list.size();i++){%>
<tr>
<td><%= list.get(i).getName()%></td>
<td><%= list.get(i).getSno()%></td>
<td><%= list.get(i).getPassword()%></td>
<% String str="delete11111?id="+list.get(i).getSno();%>
<td><a href=<%=str%>>删除</a> </td>
</tr>
<%}%>
</table>
</form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<filter>
<filter-name>login</filter-name>
<filter-class>com.example.test.loginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>login</filter-name>
<url-pattern>/index.jsp</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>registerServelt</servlet-name>
<servlet-class>com.example.test.registerservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>registerServelt</servlet-name>
<url-pattern>/registerServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>delete</servlet-name>
<servlet-class>com.example.test.deleteServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>delete</servlet-name>
<url-pattern>/delete11111</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
数据库中的student表
截图
总结
德玛西亚!!!!
没有进行检查