Summary of JSP website development 5 and jsp website development 5
Before proceeding to this summary, let's first talk about a small problem that exists in the previous article. The previous article summarized that there was a problem when creating a table in the database. name, year, and form seem to be keywords, it cannot be used as a table attribute. Therefore, when creating a table, you must modify the attribute name of the table in the SQL statement section of the previous article.
Next I will start this article on the interactive connection between JSP and MYSQL. In order to facilitate the summary, I will aim to create a student information management system. This article will summarize the implementation of the login function.
1. Create an administrator table:
2. Create an administrator class:
public class Manager { private int id; private String name ; private String password; private int key; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getKey() { return key; } public void setKey(int key) { this.key = key; } }
3. How to determine the user name and password:
Public class ManagerMaImp {// logon verification public boolean getByName (String name, String password) {System. out. println (name + "" + password); boolean flag = false; Connection conn = null; Statement st = null; ResultSet rs = null; conn = DBO. getConnection (); String SQL = "select * from manager where user_name = '" + name + "' and pwd = '" + password + "' and num =" + 1; try {st = conn. createStatement (); rs = st.exe cuteQuery (SQL); if (rs. next () {flag = true ;}} catch (SQLException e) {e. printStackTrace ();} return flag ;}}
4. design the login JSP page:
<% @ 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">
:
5. select used for identity judgment:
public class login extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String name = request.getParameter("name"); String password = request.getParameter("pwd"); ManagerMaImp mmi = new ManagerMaImp(); boolean flag = mmi.getByName(name, password); if(flag){ StudentMaImp smi = new StudentMaImp(); List<Student> list = new ArrayList<Student>(); list = smi.getAll(); request.setAttribute("list", list); request.getRequestDispatcher("All.jsp").forward(request, response); }else{ response.sendRedirect("Login.jsp"); } }}
6. Main Interface (All. jsp ):
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ page import =" com. mysql. jsp. student. student "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
:
Summary: This article mentions select. JSP interacts with the background database through select. in the previous article, we summarized the addition, deletion, modification, and query methods, in the next several articles, we will make specific use for everyone. In the main interface JSP code, you must have seen a lot: <%>, we can add JAVA code %> in <%.
In the next article, the system will summarize the specific operations for adding, deleting, and modifying users.