Java small project - simple web login registration interface
Made it for an assignment.
Java+servlet+Mysql
The prototype author of this project is "WANGZIC"
Here is the download link of its original project
There are some problems in the original code, and some changes have been made in this project. It is worth mentioning that there are many loopholes, which are only for beginners to learn and entertain!
Environmental preparation
eclipse2019, Tomcat9, Mysql (phpstudy for direct use), java1.8
reference configuration
phpstudy installation and configuration
Friends who understand can install mysql directly. Here, for the convenience of Xiaobai, I choose phpstudy
eclipse to download
and install.
Eclipse development web configuration
This only needs to do the second step.
Note that
there is a small problem here, which needs to be preprocessed
If there is something in the second arrow, right-click and click to remove, if there is no one, go to the second step directly, double-
click with the left button, click on the place pointed by the first arrow Use Tomcat installation... Then ctrl+s report to
start the official deployment project below, come on! It is very close to use!
deployment project
database creation
Before creating, click the start button to start!
1. Click MySql Management Tools, and then click MySql-Front
This interface pops up, click to open
The following may be a bit complicated. If you don’t understand, please follow the instructions and pay attention to the marked things
2. First click the SQL editor, enter create database users
this code, and then click the blue square button marked on the toolbar. After the execution is successful, there will be an extra users in the left directory. If there is no one, click the refresh button on the directory.
Then, in the SQL editor interface, continue to enter the code, just like the figure, the code is as follows, you can paste it
use users
CREATE TABLE `user` (
`USERID` int(11) NOT NULL AUTO_INCREMENT,
`USERNAME` varchar(255) NOT NULL,
`PASSWORD` varchar(255) DEFAULT NULL,
`NICKNAME` varchar(255) DEFAULT NULL,
`GENDER` varchar(255) DEFAULT NULL,
`PHONE` varchar(255) DEFAULT NULL,
`EMAIL` varchar(255) DEFAULT NULL,
`ADDRESS` varchar(255) DEFAULT NULL,
PRIMARY KEY (`USERID`,`USERNAME`)
) ENGINE=InnoDB AUTO_INCREMENT=100000 DEFAULT CHARSET=utf8;
Finally, press and hold the left mouse button to select this piece of code, and then click the button on the toolbar. At this time, user will appear under the users on the left. If it does not appear,
click Refresh
to this point. The database is established. Some information of registered users will be Deploy in eclipse here
1. Import the project to Eclipse
After downloading the project, unzip it, then open eclipse,
> right click on the blank space of the project directory on the right side of eclipse, click import
> select General > Existing projects into Workspace
>点击Next>点击Browse..选择刚下载的项目
>勾选下面Options中的Copy projects into workspace 然后点击Finish(项目导入后会有小红叉,先不用管)
>右键导入的项目选择Build path>configure build path
>选择Libraries
>Select JRE System Library and click Remove on the right
>Then click Add Library…>Select JRE System Library >Click Next >Click Finish
>Select Apache Tomcat and click Remove on the right
>Then click Add Library…>Select Server Runtime >Click Next > Select your own tomcat > click Finish
This is my Chinese version, and everyone’s English version is also correct.
2. Change the JDBC connection information in the UserDao.java file.
Change the database driver name to the driver name of the database you use. This project only provides the Mysql driver package.
(If you connect to other databases, please download the database driver jar package yourself)
Change the user and password of the database connection to the user and password of your own database
If it is configured in accordance with this article, there is basically no need to change
3. Create a new user table (database operation)
table creation statement:
CREATE TABLE `user` (
`USERID` int(11) NOT NULL AUTO_INCREMENT,
`USERNAME` varchar(255) NOT NULL,
`PASSWORD` varchar(255) DEFAULT NULL,
`NICKNAME` varchar(255) DEFAULT NULL,
`GENDER` varchar(255) DEFAULT NULL,
`PHONE` varchar(255) DEFAULT NULL,
`EMAIL` varchar(255) DEFAULT NULL,
`ADDRESS` varchar(255) DEFAULT NULL,
PRIMARY KEY (`USERID`,`USERNAME`)
) ENGINE=InnoDB AUTO_INCREMENT=100000 DEFAULT CHARSET=utf8;
At this point, the project is ready to try to run!
Notice
Here I noticed that there is still a small red cross on the project, and the solution is introduced below
Go to the project directory setting, modify this file, as shown in the figure, change 7.0 to your own version of Tomcat, and also change the following "1.7" to your own java version!
some knowledge supplement
Servlet
Reference Development Tutorial
Introduction
A servlet is a small program running on a server to process server requests, B/S model (browser-server model). The servlet is a component that processes requests, runs in an application server that supports Java, and plays a role in manipulating data.
Development Process
-
Write a java class that inherits the HttpServlet class
-
Override the doGet method and doPost method of the HttpServlet class
-
Configure the web.xml file, or use annotations to configure the servlet
- web.xml (usually in the webapp directory)
<webapp> <!-- 配置一个servlet --> <!-- servlet的配置 --> <servlet> <!-- servlet的内部名称,自定义。尽量有意义 --> <servlet-name>MyServlet</servlet-name> <!-- servlet的类全名: 包名+简单类名 --> <servlet-class>cn.roobtyan.servlet.FirstServlet</servlet-class> </servlet> <!-- servlet的映射配置 --> <servlet-mapping> <!-- servlet的内部名称,一定要和上面的内部名称保持一致!! --> <servlet-name>MyServlet</servlet-name> <!-- servlet的映射路径(访问servlet的名称) --> <url-pattern>/first</url-pattern> </servlet-mapping> </webapp>
-
Annotation (servlet3.0 and above)
The method is to use the @WebServlet annotation to mark the corresponding Servlet class. There are two attributes in @WebServlet that can be used to represent the access path of the Servlet, namely value and urlPatterns. Both value and urlPatterns are in the form of arrays, which means that we can map a Servlet to multiple access paths, but value and urlPatterns cannot be used at the same time. As an example (this is part of the code below)
package com.wzc.login.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class LogoutServlet * @author WANGZIC */ @WebServlet("/LogoutServlet") public class LogoutServlet extends HttpServlet { private static final long serialVersionUID = 1L; public LogoutServlet() { super(); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getSession().removeAttribute("user"); response.sendRedirect(request.getContextPath()+"login.jsp"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
JDBC
Introduction
Java Database Connective, Java database connection, is a set of standards dedicated to connecting and operating databases. In fact, a large number of interfaces are provided in the entire JDBC
use
When performing JDBC operations, you can follow the steps below:
1. Load the database driver. When loading, the driver needs to be configured in the classpath
2. Connect to the database through the Connection interface and the DriverManager class
3. Operate the database through three interfaces: Statement, PreparedStatement, and ResultSet
4. Close the database. In actual development, the database resources are very limited and must be closed after the operation is completed.
Refer to the link above for specific use!