-1

我已将我的 java 项目附加到 java web 应用程序的源包中。我制作了一个名为“System”的 servlet,我想做的是为我的后端应用程序创建一个前端。

但是我遇到的问题是,在我的“processregister”表单中,我无法让它重新定向到提交操作上的第二个“processreadregister”操作。因此,当用户填写表格时,我可以获取信息,然后调用我的集合让我的后端进行注册,显然我可以看到它是否成功。

不习惯 servlet,所以我可能会完全错误并调用错误的操作。

代码示例:

package HPC;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import hpcproject.HPCSystem;
import hpcproject.JobRequest;
import hpcproject.User;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 *
 * @author Kieran
 */
@WebServlet(name = "System", urlPatterns = {"/System"})
public class System extends HttpServlet {

    HPCSystem System = new HPCSystem();

    public enum OPCode {
// specify enums to methods

        BOOKING, READBOOKING, NOTHING;

        public static System.OPCode resolve(String str) {
            try {
                return valueOf(str.toUpperCase());
            } catch (Exception e) {
                return NOTHING;
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


        switch (System.OPCode.resolve(request.getParameter("action"))) {
            case REGISTER:
                processRegister(request, response);
                break;
            case READREGISTER:
                processReadRegister(request, response);
                break;

        }
    }


    protected void processRegister(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        User testUser = new User();
        System.RegisterUserWithSystem(testUser);
        // point where it goes

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Register Process</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<FORM NAME ='register' ACTION ='readregister' METHOD='POST>'");
        // SORT OUT ISSUE HERE!!!
        out.println("<input type='hidden' name='action' value='readregister'>");
        out.println("<br>");
        out.println("Login ID:");
        out.println("<INPUT TYPE='TEXT' name='user'>");
        out.println("<br>");
        out.println("Password:");
        out.println("<INPUT TYPE='PASSWORD' name='password'>");
        out.println("<br>");
        out.println("Department:");
        out.println("<INPUT TYPE='TEXT' name='department'>");
        out.println("<br>");
        out.println("Email:");
        out.println("<INPUT TYPE='TEXT' name='email'>");
        out.println("<br>");
        out.println("First name:");
        out.println("<INPUT TYPE='TEXT' name='fname'>");
        out.println("<br>");
        out.println("Last name:");
        out.println("<INPUT TYPE='TEXT' name='lname'>");
        out.println("<br>");
        out.println("<INPUT TYPE='SUBMIT' VALUE='Submit' onclick=setType('readregister') >");
        out.println("</FORM>");
        out.println("</body>");
        out.println("</html>");
    }

    protected void processReadRegister(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        User testUser = new User();


            String id = request.getParameter("user");
            String password = request.getParameter("password");
            String department = request.getParameter("department");
            String email = request.getParameter("email");
            String myfirstname = request.getParameter("fname");
            String mylastname = request.getParameter("lname");

            testUser.setUserID(id);
            testUser.setUserPassword(password);
            testUser.setUseDepartment(department);
            testUser.setUserEmail(email);
            testUser.setUserFname(myfirstname);
            testUser.setUserLname(mylastname);

            if (System.RegisterUserWithSystem(testUser)) {
                out.println("User added successfully");
            } else {
                out.println("Failed to add user");
            }
             out.println("<html>");
        out.println("<head>");
        out.println("<title>Charts</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<p>View Charts</p>");
        out.println("</body>");
        out.println("</html>");


    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
4

1 回答 1

0

标签中的action属性“指定提交表单时将表单数据发送到何处”。您正在使用并尝试对它做一个没有意义的事情,因为您没有相应的映射。尝试更改您的 servlet 映射到的属性。<form>ACTION ='readregister'POSTreadregisteraction/System

你还有其他小错误:

    out.println("<FORM NAME ='register' ACTION ='readregister' METHOD='POST>'");

应该

    out.println("<FORM NAME ='register' ACTION ='readregister' METHOD='POST'>");

'注意 .之前的结束单引号>

您应该真正研究一下用于 Java servlet 的 JSP 和不同的 html 呈现技术。将 html 直接写入 java 是非常糟糕的做法。

于 2013-03-05T15:11:22.193 回答