In der Trainingsschule habe ich beschlossen, eine App mit einem Team von acht Personen zu erstellen, aber die Anmeldefunktion war als funktionale Voraussetzung unerlässlich. Ich möchte die Seiten, die angezeigt werden können, nach der registrierten Rolle aufteilen, wusste aber nicht, wie das geht. Es sind 3 aufeinanderfolgende Feiertage und lass es uns schaffen!
Ich habe es in ungefähr 4 Stunden geschafft. Ich denke, es hätte vor drei Monaten drei- bis fünfmal länger gedauert. Ich werde erwachsen.
Java version 13.0.1 mysql 8.0
DAO
AccountDAO.java
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import model.AccountBeans;
public class AccountDAO {
//Informationen, die für die Datenbankverbindung verwendet werden
final String jdbcId = "root";
final String jdbcPass = "password";
final String jdbcUrl = "jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=JST";
//Suchen Sie ein Login-Konto
public AccountBeans findAccount(AccountBeans ab) {
//Vorbereitung des Rückgabewertes
AccountBeans returnAb = new AccountBeans();
//Verbindung zur Datenbank herstellen
try (Connection con = DriverManager.getConnection(jdbcUrl, jdbcId, jdbcPass)) {
String sql = "SELECT loginId, pass, name, roleId FROM account WHERE loginId = ? AND pass = ?";
PreparedStatement ps= con.prepareStatement(sql);
ps.setString(1, ab.getLoginId());
ps.setString(2, ab.getPass());
ResultSet rs = ps.executeQuery();
if (rs.next()) {
//Legen Sie die gefundenen Kontoinformationen als Rückgabewert fest
returnAb.setLoginId(rs.getString("loginId"));
returnAb.setPass(rs.getString("pass"));
returnAb.setName(rs.getString("name"));
returnAb.setRole(rs.getInt("roleId"));
} else {
//Gibt null zurück, wenn kein Konto vorhanden ist
return null;
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return returnAb;
}
}
AccountRegisterDAO.java
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import model.AccountBeans;
public class AccountRegisterDAO {
//Informationen, die für die Datenbankverbindung verwendet werden
final String jdbcId = "root";
final String jdbcPass = "password";
final String jdbcUrl = "jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=JST";
public AccountRegisterDAO(AccountBeans ab) {
try (Connection con = DriverManager.getConnection(jdbcUrl, jdbcId, jdbcPass)) {
String sql = "INSERT INTO account (loginId, pass, name, roleId) VALUES (?, ?, ?, ?)";
PreparedStatement ps= con.prepareStatement(sql);
ps.setString(1, ab.getLoginId());
ps.setString(2, ab.getPass());
ps.setString(3, ab.getName());
ps.setInt(4, ab.getRole());
int r = ps.executeUpdate();
if(r != 0) {
System.out.println("Erfolgreiche Neuanmeldung!");
} else {
System.out.println("Neuer Registrierungsfehler(Nein Д`)Krank ...");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
servlet
AccountCheck.java
package servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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 model.AccountBeans;
/**
* Servlet implementation class AccountCheck
*/
@WebServlet("/AccountCheck")
public class AccountCheck extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AccountCheck() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//Abrufinformationen aus der Sitzung abrufen
HttpSession session = request.getSession();
AccountBeans ab = (AccountBeans) session.getAttribute("account");
//Sortieren Sie das Weiterleitungsziel nach Rolle
if(ab.getRole() == 1) {
RequestDispatcher rd = request.getRequestDispatcher("admin.jsp");
rd.forward(request, response);
} else if(ab.getRole() == 2) {
RequestDispatcher rd = request.getRequestDispatcher("user.jsp");
rd.forward(request, response);
} else {
RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
rd.forward(request, response);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
AccountRegister.java
package servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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 dao.AccountRegisterDAO;
import model.AccountBeans;
/**
* Servlet implementation class AccountRegister
*/
@WebServlet("/AccountRegister")
public class AccountRegister extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AccountRegister() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String loginId = request.getParameter("loginId");
String pass = request.getParameter("pass");
int role = Integer.parseInt(request.getParameter("role"));
// register.Setzen Sie den von jsp empfangenen Wert auf Beans
AccountBeans ab = new AccountBeans();
ab.setName(name);
ab.setLoginId(loginId);
ab.setPass(pass);
ab.setRole(role);
//Konto in DB registrieren
AccountRegisterDAO ard = new AccountRegisterDAO(ab);
//Speichern Sie die Kontoinformationen in der Sitzung
HttpSession session = request.getSession();
session.setAttribute("account", ab);
RequestDispatcher rd = request.getRequestDispatcher("registerSuccess.jsp");
rd.forward(request, response);
}
}
AccountSearch.java
package servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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 dao.AccountDAO;
import model.AccountBeans;
/**
* Servlet implementation class AccountDAO2
*/
@WebServlet("/AccountSearch")
public class AccountSearch extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AccountSearch() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String loginId = request.getParameter("loginId");
String pass = request.getParameter("pass");
// login.Legen Sie die Login-ID und den von jsp empfangenen Pass in den Beans fest
AccountBeans ab = new AccountBeans();
ab.setLoginId(loginId);
ab.setPass(pass);
//Suche nach Konto
//Holen Sie sich die gesuchten Kontoinformationen
AccountDAO ad = new AccountDAO();
AccountBeans returnAb = ad.findAccount(ab);
if(returnAb != null) {
//Registrieren Sie Kontoinformationen und die Rolle in der Sitzung
HttpSession session = request.getSession();
session.setAttribute("account", returnAb);
RequestDispatcher rd = request.getRequestDispatcher("loginSuccess.jsp");
rd.forward(request, response);
} else {
RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
rd.forward(request, response);
}
}
}
Logout.java
package servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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;
/**
* Servlet implementation class Logout
*/
@WebServlet("/Logout")
public class Logout extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Logout() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session = request.getSession();
session.invalidate();
RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
rd.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
filter
Filter.java
package filter;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
/**
* Servlet Filter implementation class Filter
*/
@WebFilter("/*")
public class Filter implements javax.servlet.Filter {
/**
* Default constructor.
*/
public Filter() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
request.setCharacterEncoding("UTF-8");
// pass the request along the filter chain
chain.doFilter(request, response);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
JavaBeans
AccountBeans.java
package model;
import java.io.Serializable;
public class AccountBeans implements Serializable {
private static final long serialVersionUID = 1L;
private String loginId;
private String pass;
private String name;
private int role;
public String getLoginId() {
return loginId;
}
public void setLoginId(String loginId) {
this.loginId = loginId;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRole() {
return role;
}
public void setRole(int role) {
this.role = role;
}
}
JSP
admin.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Administrator Seite</title>
</head>
<body>
Dies ist eine Seite für Administratoren.<br>
<c:choose>
<c:when test="${account.role == 1 }">
Bestätigung der Registrierungsdaten.
<p>Anmelde-ID:<c:out value="${account.loginId }"></c:out></p>
<p>Passwort:<c:out value="${account.pass }"></c:out></p>
<p>Name:<c:out value="${account.name }"></c:out></p>
<p>Rolle: Administrator</p>
<p><a href="/login/Logout"><button type="button" >Ausloggen</button></a></p>
<a href="user.jsp"><button type="button" >Zur Benutzerseite</button></a>
</c:when>
<c:when test="${account.role == 2 }">
<a href="user.jsp">Zur Benutzerseite</a>
</c:when>
<c:otherwise>
<a href="login.jsp">Gehen Sie zur Anmeldeseite</a>
</c:otherwise>
</c:choose>
</body>
</html>
error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
Ich konnte mich nicht anmelden.<br>
<a href="login.jsp"><button>Gehen Sie zur Anmeldeseite</button></a>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Anmeldebildschirm</title>
</head>
<body>
<form action="/login/AccountSearch" method="post">
BENUTZERIDENTIFIKATION:<input type="text" name="loginId" required><br>
Passwort:<input type="password" name="pass" required><br>
<input type="submit" value="Einloggen"><br>
</form>
<p>
Wenn Sie Ihr Konto noch nicht registriert haben, klicken Sie hier ↓<br>
<a href="register.jsp"><button>Anmelden</button></a>
</p>
</body>
</html>
loginSuccess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>erfolgreicher Login</title>
</head>
<body>
Login war erfolgreich!
<p><a href="/login/AccountCheck"><button type="button" name="aaa" >Zur Benutzer- oder Administratorseite</button></a></p>
</body>
</html>
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Anmelden</title>
</head>
<body>
<form action="/login/AccountRegister" method="post">
<p>Bitte geben Sie alle ein</p>
<p>
<input type="radio" name="role" value="1">Registrieren Sie sich als Administrator
<input type="radio" name="role" value="2" checked>Registrieren Sie sich als Benutzer
</p>
Name:<input type="text" name="name" required><br>
BENUTZERIDENTIFIKATION:<input type="text" name="loginId" required><br>
Passwort:<input type="password" name="pass" required><br>
<input type="submit" value="Anmeldung"><br>
</form>
</body>
</html>
registerSuccess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Abschluss der Registrierung</title>
</head>
<body>
Neue Registrierung abgeschlossen!
<p><a href="/login/AccountCheck"><button type="button" name="aaa" >Zur Benutzer- oder Administratorseite</button></a></p>
</body>
</html>
user.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Nutzerinformation</title>
</head>
<body>
Dies ist die Benutzerseite.<br>
<c:choose>
<c:when test="${account.role == 2 }">
Bestätigung der Registrierungsdaten.
<p>Anmelde-ID:<c:out value="${account.loginId }"></c:out></p>
<p>Passwort:<c:out value="${account.pass }"></c:out></p>
<p>Name:<c:out value="${account.name }"></c:out></p>
<p>Rolle: Benutzer</p>
<p><a href="/login/Logout"><button type="button" >Ausloggen</button></a></p>
<a href="admin.jsp"><button type="button" >Gehen Sie zur Admin-Seite</button></a>
</c:when>
<c:when test="${account.role == 1 }">
<a href="admin.jsp">Gehen Sie zur Admin-Seite</a>
</c:when>
<c:otherwise>
<a href="login.jsp">Gehen Sie zur Anmeldeseite</a>
</c:otherwise>
</c:choose>
</body>
</html>
Recommended Posts