简体   繁体   中英

Passing parameters from JSP page to a java method to persist to a database

I am trying to create a registration JSP page which when submit is clicked I would like it to pass the information entered to an addUser method in a java class

This is my JSP Page:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
     pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org /TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration</title>
</head>
<body>
<center>
            <h2>Signup Details</h2>
            <form action="RegisterUser.java" method="post">
            <br/>Username:<input type="text" name="username">
            <br/>Password:<input type="password" name="password">
            <br/>Email:<input type="text" name="email">

            <%= String name = request.getParameter("username");
                String pass = request.getParameter("password");
                String email = request.getParameter("email");
                User u = new User();
                u.setName(name);
                u.setPassword(pass);
                u.setEmail(email);%>

            <br/><input type="submit" value="Submit" action="RegisterUser.addUser(u)">
            </form>
        </center>

</body>
</html>

When Submit is clicked I want to add the details to a database using the following method

public class RegisterUser {
     private static final String PERSISTENCE_UNIT_NAME = "User";
     private static EntityManagerFactory factory;
     private User user;

     public void addUser(User user){
         factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
         EntityManager em = factory.createEntityManager();
         // Create new user
         em.getTransaction().begin();
         em.persist(user);
         em.getTransaction().commit();
         em.close();

You generally pass request data to Servlet which acts as controller and depending on request it delegates logical calls to services class

Check out the complete flow with example and nice explanation on servlet wiki page

You have to create a Servlet that processes your form and calls the addUser() method.

You can't invoke a Java method directly from JavaScript.

Here's a good tutorial on the subject.

http://www.tutorialspoint.com/servlets/servlets-form-data.htm

You need to work with a Servlet that will work as Controller in your Model . The View , the JSP in your case, should be able to communicate with your Model using a Servlet.

For a good separation between the Model, Controller and the View you can use Spring MVC. Here is a good example that you can follow as start point: http://www.mkyong.com/spring-mvc/spring-mvc-hello-world-example .

Did you try closing down eclipse and opening it again? If not try restarting eclipse. It works for me every time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM