E
E
Evgeny Petryaev2019-10-21 15:22:03
JavaScript
Evgeny Petryaev, 2019-10-21 15:22:03

Why does the user write to the server?

There is a simple registration form

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Create User</title>
<link rel="stylesheet" href="create.css">
<script type="text/javascript" src="create.js"></script>
</head>
<body>
<h1>Create Account</h1>
<div id="CenterBlock">
<form method="post"  onsubmit="return ValidMail()">
<p id="message" >Enter your information</p>
<label class="labeler">Username</label><br>
<input class="inputer" name="nickname"/><br><br>
<label class="labeler">Password</label><br>
<input class="inputer" name="password" type="password" /><br><br>
<label class="labeler">Email</label><br>
<input class="inputer" id="email" name="email" /><br><br>
<input class="button" type="submit" value="Save"/>
</form>
</div>
</body>
</html>

and a js script that prevents writing an incorrect mailbox:
function ValidMail() {
    var re = /^[\w-\.][email protected][\w-]+\.[a-z]{2,4}$/i;
    var myMail = document.getElementById('email').value;
    var valid = re.test(myMail);
    if (valid) output = 'E-mail address writed correct!';
    else output = 'E-mail address writed incorrect!';
    document.getElementById('message').innerHTML = output;
    return valid;
}

Here is the code used on the backend
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;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author User
 */
@WebServlet("/create")
public class CreateServlet extends HttpServlet{
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
        getServletContext().getRequestDispatcher("/create.jsp").forward(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
        try {
            String nickname = request.getParameter("nickname");
            String  email = request.getParameter("email");
            String  password = request.getParameter("password");
            Users users = new Users(nickname,email, password);
            UsersDB.insert(users);
            getServletContext().getRequestDispatcher("/create.jsp").forward(request, response);
            //response.sendRedirect(/*request.getContextPath()+*/"/index.html");
        }
        catch(Exception ex) {
            getServletContext().getRequestDispatcher("/create.jsp").forward(request, response); 
        }
    }
}

The user is added after he entered the data and then refreshed the page through the browser (although the fields are clear), who knows what to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2019-10-21
@Robur

first, get rid of "like"
or make sure that it forbids sending and the request doesn't go away (obviously it doesn't). Look in the network log in devtools first.
or make sure that the prohibition does not work (probably does not work) and figure out why

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question