Java servlet表单操作问题

时间:2013-03-05 14:56:30

标签: java servlets

我已将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);
    }
}

1 个答案:

答案 0 :(得分:0)

action代码“specifies where to send the form-data when a form is submitted”中的<form>属性。您正在使用ACTION ='readregister'并尝试对POST执行readregister这是没有意义的,因为您没有相应的映射。尝试将action属性更改为/System,这是servlet的映射。

你还有其他一些小错误:

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

应该是

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

'之前注意结束单引号>

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