Use Tomcat server, database, JSP and HTML to realize user registration

01? 2022-02-13 07:30:16 阅读数:581

use tomcat server database jsp

At the beginning, enter the home page interface as follows

  Click Register to enter the registration interface

Registered users , The data is saved to mysql In the database

 

  Click to save the registration data to the database , And jump into the registration success interface

 

  adopt userServet Inherit baseServlet Realize the registration function

package servlet;
import service.UserService;
import service.UserServiceimpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class BaseServlet extends HttpServlet {
private UserService userService=new UserServiceimpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String action = req.getParameter("action");
try {
Method declaredMethod = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
declaredMethod.invoke(this,req,resp);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
package servlet;
import pojo.User;
import service.UserService;
import service.UserServiceimpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class UserServlet extends BaseServlet{
protected UserService userService=new UserServiceimpl();
// register
protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
String phone = req.getParameter("phone");
User user = new User(null, username, password, phone);
if(userService.exitUsername(username)){
userService.register(user);
req.getRequestDispatcher("/user/register_successs.html").forward(req,resp);
}else {
req.getRequestDispatcher("/user/regist.html").forward(req,resp);
}
}
}

home page html Code

<%--
Created by IntelliJ IDEA.
User: 21781
Date: 2021/10/29
Time: 14:51
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="cn">
<head>
<meta charset="UTF-8">
<title>Title</title>
<base href="http://localhost:8080/OrderFood/">
<script type="text/javascript" src="static/lib/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function () {
$("#button1").click(function (){
window.location.href='user/login.html'
})
$("#button2").click(function () {
window.location.href='user/regist.html'
})
$("#button").click(function (){
window.location.href='user/manager.html'
})
})
</script>
<link type="text/css" rel="stylesheet" href="static/css/index.css">
<link type="text/css" rel="stylesheet" href="static/style(css)/bootstrap.min.css">
<link type="text/css" rel="stylesheet" href="static/style(js)/bootstrap.bundle.min.js">
</head>
<body style="background: url(static/piture/ background 1.jpg);background-size: cover">
<div id="div1" style="background-color: rgba(255,255,255,0.8);background-size: cover">
<h1 id="h1"> Welcome to online ordering </h1>
<button type="button" class="btn btn-link" id="button"> Background management </button>
<!-- Functional areas -->
<div id="div2">
<!-- Login registration button -->
<div class="d-grid gap-2 col-6 mx-auto" id="button1" >
<button type="button" class="btn btn-outline-secondary"> land </button>
</div>
<div class="d-grid gap-2 col-6 mx-auto" id="button2" >
<button type="button" class="btn btn-outline-secondary"> register </button>
</div>
</div>
<br/>
<!-- Home page content -->
<div id="div3">
<!-- Pictures add -->
<img src="static/piture/ Chinese food .jpg" class="piture" alt="" id="p1">
<img src="static/piture/ Fast food .jpg" class="piture" alt="" id="p2">
<img src="static/piture/ Milk tea .jpg" class="piture" alt="" id="p3">
<img src="static/piture/ drinks .jpg" class="piture" alt="" id="p4">
<!-- Text introduction -->
<div id="d1" class="d">
<a href="user/mainChinaFood.html" style="font-size: 1.5pc;font-weight: bold" > Chinese food </a>
<div> The Chinese cuisine here includes Cantonese cuisine 、 Sichuan cuisine 、 Shandong cuisine 、 Huaiyang cuisine 、 Zhejiang cuisine 、 Fujian Cuisine 、 Hunan cuisine 、 HUICAI , All kinds of CAI have everything , We have everything you want .</div>
</div>
<div id="d2" class="d">
<a href="user/mainFastFood.html" style="font-size: 1.5pc;font-weight: bold" > Fast food </a>
<div> Fast food here includes Chinese fast food 、 western-style fast food 、 Chinese and Western fast food 、 Other fast food , With fast 、 Convenient and other features , Order now !</div>
</div>
<div id="d3" class="d">
<a href="user/mainTeamilk.html" style="font-size: 1.5pc;font-weight: bold"> Milk tea </a>
<div> The milk tea here includes Hong Kong style milk tea 、 Table milk tea 、 English milk tea 、 Milk tea with silk stockings 、 Mandarin duck milk tea , It's so good that I can't stop , Order as soon as you like !</div>
</div>
<div id="d4" class="d">
<a href="user/mainDrinks.html" style="font-size: 1.5pc;font-weight: bold"> drinks </a>
<div> The drinks here include carbonated drinks 、 Milk drink 、 Fruit and vegetable juice 、 coffee 、 tea 、 Cocoa, etc , Natural purity , A healthy choice , Order quickly !</div>
</div>
</div>
</div>
</body>
</html>

copyright:author[01?],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130730140911.html