数据库中的预定义表单字段

时间:2016-11-03 20:06:39

标签: java jsp servlets

我正在创建一个在线银行。登录时,它为该用户创建了一个会话。用户可以选择单击不同的事务类型。因此,当用户单击打开帐户按钮时,它将转到openAccount.jsp页面。我还在openAccount.jsp中有一个用户名,姓和电子邮件的表单。当用户点击打开帐户按钮并重定向到openAccount.jsp时,我希望从数据库预先填写表单。我不知道怎么样?到目前为止,这是我的代码:

家庭page.jsp

<div class="col-md-4">
        <form name = "OpenAccount" action="open-account.jsp" id="openaccount">
            <p>
                <button type="submit" class="btn btn-primary btn-lg">Open Account</button>
            </p>
        </form>
    </div> 

开account.jsp

<!DOCTYPE html>
<html>
<head>
    <title>Open Account</title>
</head>
<body>
    <h3>Please fill in the details</h3> 

    <form name="predifinedFields">
        First Name: <input type="text" name="firstname" value= <%= request.getAttribute("firstname") %> > <br/><br/>
        Last Name: <input type="text" name="lastname" value= <%= request.getAttribute("lastname") %>> <br/><br/>
        Email: <input type="text" name="email" value= <%= request.getAttribute("email") %>>  <br/><br/>
    </form>

    <form name="openAccount" action="OpenAccount" method="POST">
        Select the type of account: 
        <select name="accounttype">
            <option>Checking</option>
            <option>Saving</option>
        </select> <br/><br/>

        Initial Deposit: $<input type="text" name="deposit">  <br/><br/>

        Please check the box if everything above is complete:
        Agree <input type="radio" name="agree" value="Agree">
        Disagree <input type="radio" name="agree" value="Disagree">

        <br/><br/>
        <input type="submit" value="submit" name="Submit">
    </form>
</body>
</html>

OpenAccount.java

public class OpenAccount extends HttpServlet 
{
    private static final long serialVersionUID = 1L;

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


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {   
        String username = "";

        HttpSession session = request.getSession(false);
        if(session != null)
        {
            username = (String) session.getAttribute("username");

            Users users = new Users();
            users = DBConnection.getUsers(username);

            request.setAttribute("firstname", users.getFirstName());
            request.setAttribute("lastname", users.getLastName());
            request.setAttribute("email", users.getEmail());
            //request.setAttribute("username", users.getUsername());
        }

        //  String firstName = (String) request.getParameter("firstname");
        //  String lastname = (String) request.getParameter("lastname");
        String email = (String) request.getParameter("email");
        String accountType = (String) request.getParameter("accounttype");
        String accept = (String) request.getParameter("agree");
        String deposit = (String) request.getParameter("deposit");

        if(accept.equals("Agree"))
        {
            if(accountType.equals("Checking"))
            {
                DBConnection.newCheckingAccount(email, deposit);
                RequestDispatcher dispatcher = request.getRequestDispatcher("home-page.jsp");
                dispatcher.forward(request, response);
            }
            else if(accountType.equals("Saving"))
            {
                DBConnection.newSavingAccount(email, deposit);
                RequestDispatcher dispatcher = request.getRequestDispatcher("home-page.jsp");
                dispatcher.forward(request, response);
            }
        }
        else
        {
            System.out.println("Please modify the changes");
            RequestDispatcher dispatcher = request.getRequestDispatcher("open-account.jsp");
            dispatcher.forward(request, response);
        }

    }
}

Database.java

public static Users getUsers(String username) 
    {
        Users user = new Users();
        try
        {
            DBConnection.connectToDB();
            String query = "SELECT * FROM userlogin where username=?";

            stmt = DBConnection.conn.prepareStatement(query);
            stmt.setString(1,username);
            ResultSet rs = stmt.executeQuery();

            while(rs.next())
            {
                user.setFirstName(rs.getString("firstname"));
                user.setLastName(rs.getString("lastname"));
                user.setEmail(rs.getString("email"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }

        return user;
    }

1 个答案:

答案 0 :(得分:0)

在你的情况下(基本servlet),你应该遵循这个逻辑:

- &gt;浏览器
- &gt; servlet,用于将所需的数据从数据库加载并准备到请求属性中,并最终转发到open-account.jsp
- &gt; open-account.jsp使用请求属性动态呈现从数据库中检索到的数据字段。

对于其他用例,请尝试保持这个简单的逻辑:
- &gt;浏览器
- &gt; servlet处理....
- &gt; jsp渲染....

home-page.jsp中的表单应该发布到servlet(控制器):

<div class="col-md-4">
        <form name = "OpenAccount" action="/OpenAccount" id="openaccount">
            <p>
                <button type="submit" class="btn btn-primary btn-lg">Open Account</button>
            </p>
        </form>
    </div> 

您应该将OpenAccount重命名为OpenAccountServlet(保持约定更好)并正确配置servletOpenAccountServlet的{​​{1}}(或带注释) 。 如果您使用web.xml文件进行servlet配置,则上一个web.xmljsp)中发布的网址应与action="/OpenAccount"

中的值匹配

前锋应该这样做:

<url-pattern>/OpenAccount</url-pattern>