简体   繁体   English

如何在Servlet中将对象从会话存储到列表

[英]How to store objects from session to List in Servlet

Good day people. 大家好。

I have really hard question to me which I spent whole day to think of today. 我真的很难回答我今天整整一整天都在想的问题。 The first of all whole code you can get on this url :https://github.com/Vadim32/RemoteWebContent 您可以在此网址上获得所有代码的第一个:https://github.com/Vadim32/RemoteWebContent

Ok, now is the problem: I need to make shopping cart of user orders. 好的,现在是问题所在:我需要制作用户订单的购物车。 Where user can order events like: developing software or computer maintenance. 用户可以在其中订购事件,例如:开发软件或计算机维护。 So for this purpose I have servlet: 为此,我有servlet:

public class EventNewUserServlet extends HttpServlet {


    //-------------------------Variables------------------------

        private static final long serialVersionUID = 1L;

        protected WebContentDAOIF webContentDAOIF;


    //-------------------------Servlet methods------------------  

     public void init() {
         /*Getting bean from spring ContextLoaderListener and inject it to webContentDAOIF variable 
           to use webContentDAOIF in this servlet*/
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            webContentDAOIF = context.getBean("webContentDAOImpl", WebContentDAOIF.class);
        }



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


            List<UserEvents> userEventsList = new ArrayList<UserEvents>(2); //This list store all user orders 



        }
}

So I have List<UserEvents> userEventsList in doPost method which will store events which user order, like: ComputerMaintenanceEvent or SoftwareDevelopmentEvent, they are all extended from UserEvents class 所以我在doPost方法中有List<UserEvents> userEventsList ,它将存储用户顺序的事件,例如:ComputerMaintenanceEvent或SoftwareDevelopmentEvent, 它们都从UserEvents类扩展

On this jsp page: 在此jsp页面上:

<html>

        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Order event page</title>

            <!-- Bootstrap css responsive-->
            <link href="resources/css/bootstrap.css" rel="stylesheet">
            <link href="resources/css/bootstrap-responsive.css" rel="stylesheet">

        </head>

        <body>

            <!-- JQuery and Bootstrap file-->
            <script src="resources/js/jquery-1.10.2.min.js"></script>
            <script src="resources/js/bootstrap.min.js"></script>
            <script src="resources/js/jquery.scrollTo.js"></script> 
            <script src="resources/js/jquery.nav.js"></script>

            <h1>New user</h1>
            <form action="new-user-event" method="post">

                    <input type="hidden" name="userSessionId" value="${userSessionId}">

                <table align="center">

                    <tr><td>Event type:</td><td>
                        <select name="event_type">
                            <option>Choose event type</option>
                            <option>Computer Maintenance Event</option>
                            <option>Software Development Event</option>
                        </select>
                    </td></tr>
                    <tr><td>Event date:</td> <td><input type="text" class="span2" name="user_event_date"></td></tr>
                    <tr><td>Event description:</td> <td><textarea rows="5" name="description"></textarea></td></tr>
                    <tr><td>Additional Info:</td> <td><input type="text" class="span2" name="additional_info"></td></tr>

                    <tr><td>First name:</td> <td><input type="text" class="span2" name="first_name"></td></tr>
                    <tr><td>Last name:</td> <td><input type="text" class="span2" name="last_name"></td></tr> 
                    <tr><td>E-male:</td> <td><input type="text" class="span2" name="e_male"> </td></tr>
                    <tr><td>Address:</td> <td> <input type="text" class="span2" name="address"></td></tr>
                    <tr><td>Phone number:</td> <td> <input type="text" class="span2" name="phone_number"></td></tr>

                    <tr><td><input type="submit" value="Regester user"></td></tr>

                </table>

            </form>

        </body>

    </html>

User will order the actual events(will make orders). 用户将订购实际事件(将进行订购)。

So the requirements are: 因此要求是:

  • I don't want to make another servlet to store object in list and another servlet to store list of events in database. 我不想使另一个servlet将对象存储在列表中,而另一个servlet将事件列表存储在数据库中。

  • I want to collect objects of events first in session or just add them to list with no servlet(maybe somehow with jsp or javascripts), when user have for example add button on web page and he can add as many events as he wants in the list and after by submit button from form on same jsp page will trigger the actual servlet to save all collected events from list to database? 我想在会话中首先收集事件的对象,或者只是将它们添加到不带servlet的列表中(也许以某种方式使用jsp或javascript),例如当用户在网页上添加按钮时,他可以根据需要在事件中添加任意数量的事件。列表和在同一jsp页面上的表单中的提交按钮之后,将触发实际的servlet将列表中所有收集的事件保存到数据库?

Is it possible to do so or not?? 是否可以这样做?

Thank you developers hope you will help me with some great tutorial or documentation to read. 谢谢开发人员,希望您能帮助我阅读一些很棒的教程或文档。

In my opinion,you want to store events temporarily if add button is clicked. 我认为,如果单击添加按钮,您想临时存储事件。 And finally only hit the submit button will update the previously temporarily stored events to database, right ? 最后,只有点击提交按钮,才会将先前临时存储的事件更新到数据库,对吗?

The answer is simple, just think you shop in Amazon, you first add some products to carts, and the products you selected will be stored to cookie which stays in client's computer. 答案很简单,只要想像您在亚马逊上购物,您首先将一些产品添加到购物车中,然后您选择的产品就会存储到客户计算机中的Cookie中。 And finally you checkout all the products you selected, here a servlet will read stored products from cookie and then persist them to the database. 最后,您签出所有选定的产品,在这里servlet将从cookie中读取存储的产品,然后将其保存到数据库中。 So I think you can follow this way. 所以我认为您可以按照这种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM