从第二页中的表单收集输入,而不会丢失第一页中表单的输入

时间:2015-12-01 17:26:57

标签: jsp servlets jstl

我试图让用户输入某个项目代码,然后将其带到一个页面,在那里他们可以根据输入的代码从数据库中查看代码和项目信息。在第二页上,我希望用户能够使用另一个提交按钮输入另一个项目代码(在表格下),该按钮将使用他们添加的另一个项目来更新表格,该项目从数据库中获取。如何在不丢失桌面上的先前数据的情况下执行此操作?

到目前为止

代码:

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Scanned Items</title>
    </head>
    <body> 
    <h1 align="center"><u><font color="blue">List of Scanned Items</font></u></h1>

    <table align="center">
        <tr>
            <th> Item Code </th>
            <th> Name </th>
            <th> Price </th>
            <th> Taxable </th>
            <th> Tax </th>
        </tr>
                <tr>
                    <td> ${productcatalogue.code}</td>
                    <td> ${productcatalogue.name}</td>
                    <td> $${productcatalogue.price}</td>
                    <td> ${productcatalogue.taxable}</td>
                    <td> </td>
                </tr>
    </table>
</body>
</html>

Servlet代码:

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

    DBConnection dbConn = (DBConnection)request.getServletContext()
        .getAttribute("dbConn");

    ProductCatalogueDAO productcatalogueDAO = new ProductCatalogueDAO();
    String itemcode = request.getParameter("itemcode");
    ProductCatalogue productcatalogue = productcatalogueDAO.getItems(dbConn.getConnection(), itemcode);
    request.setAttribute("productcatalogue", productcatalogue);

    RequestDispatcher view = request.getRequestDispatcher
    (response.encodeURL("scannedItems.jsp"));
    view.forward(request, response);  
}

1 个答案:

答案 0 :(得分:1)

  

我可以给你概述来处理它,我只是没有使用数据库   存储它,但剩下的东西我做了。

     

按照这个顺序,您将获得解决方案,

     

只需创建一个jsp文件,在我的情况下, a1.jsp

------------------------ a1.jsp代码---------------- -----------------

<%@page import="java.util.ArrayList"%>
<%@page import="com.stackoverflow.ItemBean"%>
<%@ 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>Insert title here</title>
</head>
<body>

    <table border="2">
        <tr>
            <th>Item Code</th>
            <th>Name</th>
            <th>Price</th>
            <th>Taxable</th>
            <th>Tax</th>
            <th>Action</th>
        </tr>

        <%
        ArrayList<ItemBean> list = new ArrayList<ItemBean>();
           if(session.getAttribute("allitems") != null){
               list =  (ArrayList<ItemBean>)session.getAttribute("allitems");   
           }

            for(ItemBean ib : list){
                %>
                <tr>
                    <td><%= ib.getItemCode() %></td>
                    <td><%= ib.getItemname() %></td>
                    <td><%= ib.getPrice() %></td>
                    <td><%= ib.getTaxable() %></td>
                    <td><%= ib.getTax() %></td>
                    <td><i>Saved</i></td>
                </tr>   

                <%}%>

        <form action="ItemSubmit.do" method="post">
        <tr>
            <td><input type="text" name="itemCode" value="" /></td>
            <td><input type="text" name="itemname" value="" /></td>
            <td><input type="text" name="price" value="" /></td>
            <td><select name="taxable">
                    <option value="yes">Yes</option>
                    <option value="no">No</option>
            </select></td>
            <td><input type="text" name="tax" value="" /></td>
            <td><input type="submit" value="Add" /></td>
        </tr>
        </form>
    </table>


</body>
</html>

--------------- ItemSubmit.java servlet代码-----------------

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


        HttpSession session = request.getSession();
        ArrayList<ItemBean> listBean = new ArrayList<ItemBean>();

        String itemCode = request.getParameter("itemCode");
        String itemname = request.getParameter("itemname");
        double price = 0;
        String taxable = request.getParameter("taxable");
        double tax = 0;
        String remark = "saved";

        try {
            price = Double.parseDouble(request.getParameter("price")) ;
            tax =   Double.parseDouble(request.getParameter("tax"));
        } catch (Exception e) {
            remark = e.getMessage();
        }

        if(session.getAttribute("allitems") != null){
            listBean = (ArrayList<ItemBean>)session.getAttribute("allitems");
        }

        ItemBean itemBean = new ItemBean(itemCode, itemname, price, taxable, tax,remark);
        //save itemBean to DB-if needed then...
        listBean.add(itemBean);
        session.setAttribute("allitems",listBean);

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

    }

-------------------- ItemBean.java普通java类---------------

package com.stackoverflow;

public class ItemBean {
        String itemCode;
        String itemname;
        double price;
        String taxable;
        double tax;
        String remark;

        public ItemBean(String itemCode, String itemname, double price, String taxable,double tax , String remark ) {
            this.itemCode = itemCode;
            this.itemname = itemname;
            this.price = price;
            this.taxable = taxable;
            this.tax  = tax;
            this.remark = remark;
        }


        public String getItemCode() {
            return itemCode;
        }
        public void setItemCode(String itemCode) {
            this.itemCode = itemCode;
        }
        public String getItemname() {
            return itemname;
        }
        public void setItemname(String itemname) {
            this.itemname = itemname;
        }
        public double getPrice() {
            return price;
        }
        public void setPrice(double price) {
            this.price = price;
        }
        public String getTaxable() {
            return taxable;
        }
        public void setTaxable(String taxable) {
            this.taxable = taxable;
        }
        public double getTax() {
            return tax;
        }
        public void setTax(double tax) {
            this.tax = tax;
        }

        public String getRemark() {
            return remark;
        }

        public void setRemark(String remark) {
            this.remark = remark;
        }

}

--------------- OUT-PUT --------------------

enter image description here