java:在转发后保留表单中的值

时间:2013-06-14 06:25:37

标签: java

我有一个jsp页面GetEmpDetails.jsp和一个servlet类ShowEmpDetails.java

我怀疑是什么时候请求转到servlet和servlet类 验证(at present i did validation only for firstname and last name)。如果让我们说firstname字段留空,那么我在jsp中显示除该特定字段之外的消息(我使用forward转发回相同的{{1}形式)。但是,我还希望用户输入的详细信息保留在该表格中。 能否指导我如何在servlet以及jsp页面中编写代码。

我的Servlet:

GetEmp

我的Jsp:

package com.fulcrum.EmpForm;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sun.xml.internal.ws.developer.MemberSubmissionAddressing.Validation;

public class ShowEmpDetails extends HttpServlet {

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

        String fname = request.getParameter("fname");
        request.setAttribute("fname", fname);

        String lname = request.getParameter("lname");
        String gender = request.getParameter("gender");
        String age = request.getParameter("age");
        String email = request.getParameter("email");
        String password = request.getParameter("pwd");
        int date = Integer.parseInt(request.getParameter("date"));
        int month = Integer.parseInt(request.getParameter("month"));
        int year = Integer.parseInt(request.getParameter("year"));

        if (fname == "") {

            String message1 = "OOps!!! required field fname";
            request.setAttribute("message1", message1);

            // get back to login.jsp page using forward

        }
        if (lname == "") {
            String message2 = "OOps!!! required field lname";
            request.setAttribute("message2", message2);

        }
        request.getRequestDispatcher("/GetEmpDetails.jsp").forward(request,
                response);

        return;

    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

    }

}

1 个答案:

答案 0 :(得分:0)

你为名字和姓氏的messsage设置了setAttrribute,以便可以通过方法request.getAttribut("","")获取该属性,并且你想显示输入的值然后你应该使用你在servlet中使用的那个那是request.getParameter("fname")