Example usage for org.hibernate Session save

List of usage examples for org.hibernate Session save

Introduction

In this page you can find the example usage for org.hibernate Session save.

Prototype

Serializable save(Object object);

Source Link

Document

Persist the given transient instance, first assigning a generated identifier.

Usage

From source file:criaProduto.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from   w  w  w  .j  a  v a 2 s.  c  o m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {

        Produto prod = new Produto();
        prod.setNome("Produto_" + new Random().nextLong());
        prod.setFabricante("Pirelli");
        prod.setPreco(400);

        Transaction tx = null;
        try {
            Session sessao = HibernateUtil.getSessionFactory().openSession();

            tx = sessao.beginTransaction();

            sessao.save(prod);
            sessao.flush();

            tx.commit();
            sessao.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            tx.rollback();
        }

        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet criaProduto</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet criaProduto at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

From source file:prop_add_serv.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*w  w  w . j ava2  s  .  co  m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, FileUploadException {
    response.setContentType("text/html;charset=UTF-8");

    HttpSession hs = request.getSession();
    PrintWriter out = response.getWriter();
    try {

        if (hs.getAttribute("user") != null) {
            Login ln = (Login) hs.getAttribute("user");
            System.out.println(ln.getUId());
            String pradd1 = "";
            String pradd2 = "";
            String prage = "";
            String prarea = "";
            String prbhk = "";
            String prdescrip = "";
            String prprice = "";
            String prcity = "";
            String prstate = "";
            String prname = "";
            String prtype = "";

            String prfarea = "";
            String prphoto1 = "";
            String prphoto2 = "";
            String prphoto3 = "";
            String prphoto4 = "";

            //             
            // creates FileItem instances which keep their content in a temporary file on disk
            FileItemFactory factory = new DiskFileItemFactory();
            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);

            //get the list of all fields from request
            List<FileItem> fields = upload.parseRequest(request);
            // iterates the object of list
            Iterator<FileItem> it = fields.iterator();
            //getting objects one by one
            while (it.hasNext()) {
                //assigning coming object if list to object of FileItem
                FileItem fileItem = it.next();
                //check whether field is form field or not
                boolean isFormField = fileItem.isFormField();

                if (isFormField) {
                    //get the filed name 
                    String fieldName = fileItem.getFieldName();

                    if (fieldName.equals("pname")) {
                        //getting value of field
                        prname = fileItem.getString();
                        System.out.println(prname);
                    } else if (fieldName.equals("price")) {
                        //getting value of field
                        prprice = fileItem.getString();
                        System.out.println(prprice);
                    } else if (fieldName.equals("city")) {
                        prcity = fileItem.getString();
                        System.out.println(prcity);
                    } else if (fieldName.equals("state")) {
                        prstate = fileItem.getString();
                        System.out.println(prstate);
                    } else if (fieldName.equals("area")) {
                        prarea = fileItem.getString();
                        System.out.println(prarea);
                    } else if (fieldName.equals("pbhk")) {
                        prbhk = fileItem.getString();
                        System.out.println(prbhk);
                    } else if (fieldName.equals("pdescription")) {
                        prdescrip = fileItem.getString();
                        System.out.println(prdescrip);

                    } else if (fieldName.equals("ptype")) {
                        prtype = fileItem.getString();
                        System.out.println(prtype);

                    } else if (fieldName.equals("paddress1")) {
                        pradd1 = fileItem.getString();
                        System.out.println(pradd1);
                    } else if (fieldName.equals("paddress2")) {
                        pradd2 = fileItem.getString();
                        System.out.println(pradd2);
                    } else if (fieldName.equals("page")) {
                        prage = fileItem.getString();
                        System.out.println(prage);
                    } else if (fieldName.equals("pfarea")) {
                        prfarea = fileItem.getString();
                        System.out.println(prfarea);
                    }
                } else {

                    String fieldName = fileItem.getFieldName();

                    if (fieldName.equals("photo1")) {

                        //getting name of file
                        prphoto1 = new File(fileItem.getName()).getName();

                        //get the extension of file by diving name into substring
                        // String extension=prphoto.substring(prphoto.indexOf(".")+1,prphoto.length());;
                        //rename file...concate name and extension
                        //prphoto=prname+"."+extension;
                        //System.out.println(prphoto);
                        try {

                            String fp = "/home/rushin/NetBeansProjects/The_Asset_Consultancy/web/images/property/";
                            // FOR UBUNTU add GETRESOURCE  and GETPATH
                            // String filePath = this.getServletContext().getResource("/images").getPath() + "\\";
                            fileItem.write(new File(fp + prphoto1));
                        } catch (Exception ex) {
                            out.println(ex.toString());
                        }
                    }

                    //PHOTO 2

                    else if (fieldName.equals("photo2")) {
                        prphoto2 = new File(fileItem.getName()).getName();

                        try {

                            String fp = "/home/rushin/NetBeansProjects/The_Asset_Consultancy/web/images/property/";
                            fileItem.write(new File(fp + prphoto2));
                        } catch (Exception ex) {
                            out.println(ex.toString());
                        }

                    }
                    //PHOTO 3
                    else if (fieldName.equals("photo3")) {

                        prphoto3 = new File(fileItem.getName()).getName();
                        try {

                            String fp = "/home/rushin/NetBeansProjects/The_Asset_Consultancy/web/images/property/";
                            fileItem.write(new File(fp + prphoto3));
                        } catch (Exception ex) {
                            out.println(ex.toString());
                        }
                    }

                    //PHOTO 4
                    else if (fieldName.equals("photo4")) {
                        prphoto4 = new File(fileItem.getName()).getName();
                        try {

                            String fp = "/home/rushin/NetBeansProjects/The_Asset_Consultancy/web/images/property/";
                            fileItem.write(new File(fp + prphoto4));
                        } catch (Exception ex) {
                            out.println(ex.toString());
                        }
                    }

                }

            }

            SessionFactory sf = NewHibernateUtil.getSessionFactory();
            Session ss = sf.openSession();
            Transaction tr = ss.beginTransaction();

            String op = "";
            Criteria cr = ss.createCriteria(StateMaster.class);
            cr.add(Restrictions.eq("sId", Integer.parseInt(prstate)));
            ArrayList<StateMaster> ar = (ArrayList<StateMaster>) cr.list();
            if (ar.isEmpty()) {

            } else {
                StateMaster sm = ar.get(0);
                op = sm.getSName();

            }

            String city = "";
            Criteria cr2 = ss.createCriteria(CityMaster.class);
            cr2.add(Restrictions.eq("cityId", Integer.parseInt(prcity)));
            ArrayList<CityMaster> ar2 = (ArrayList<CityMaster>) cr2.list();
            System.out.println("----------" + ar2.size());
            if (ar2.isEmpty()) {

            } else {
                city = ar2.get(0).getCityName();
                System.out.println("-------" + city);
            }

            String area = "";
            Criteria cr3 = ss.createCriteria(AreaMaster.class);
            cr3.add(Restrictions.eq("areaId", Integer.parseInt(prarea)));
            ArrayList<AreaMaster> ar3 = (ArrayList<AreaMaster>) cr3.list();
            System.out.println("----------" + ar3.size());
            if (ar3.isEmpty()) {

            } else {
                area = ar3.get(0).getAreaName();
                System.out.println("-------" + area);
            }

            PropDetail pd = new PropDetail();

            pd.setUId(ln);

            pd.setPAge(Integer.parseInt(prage));

            pd.setPBhk(prbhk);
            pd.setPDescription(prdescrip.trim());
            pd.setPAdd1(pradd1);
            pd.setPAdd2(pradd2);
            pd.setPPrice(Integer.parseInt(prprice));

            pd.setPCity(city);
            pd.setPState(op);
            pd.setPArea(area);

            pd.setPName(prname);
            pd.setPType(prtype);
            pd.setPImg1(prphoto1);
            System.out.println(prphoto1);
            System.out.println(prphoto2);
            pd.setPImg2(prphoto2);

            pd.setPImg3(prphoto3);
            pd.setPImg4(prphoto4);

            pd.setPFloor(Integer.parseInt(prfarea));

            ss.save(pd);

            tr.commit();

            RequestDispatcher rd = request.getRequestDispatcher("property_search_1.jsp");
            rd.forward(request, response);
        }
    } catch (HibernateException e) {
        out.println(e.getMessage());
    }
}

From source file:agent_fb_serv.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from w w  w  . j a  v  a  2 s.co  m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    HttpSession hs = request.getSession();
    try {

        if (hs.getAttribute("user") != null) {
            AgentDetail ad = (AgentDetail) hs.getAttribute("agentobj");
            Login ln = (Login) hs.getAttribute("user");

            String content = request.getParameter("content");

            SessionFactory sff = NewHibernateUtil.getSessionFactory();
            Session ss = sff.openSession();
            Transaction tr = ss.beginTransaction();

            AgentFeedback af = new AgentFeedback();
            af.setAId(ad);
            af.setUId(ln);
            af.setComment(content);
            af.setTime(new Date(System.currentTimeMillis()));

            ss.save(af);
            tr.commit();

            RequestDispatcher rd = request.getRequestDispatcher("getstate?id=4");
            rd.forward(request, response);

        }
    } catch (HibernateException he) {
        out.println(he.getMessage());
    }
}

From source file:TeachingPlanMandatoryTest.java

License:Open Source License

@SuppressWarnings({ "unchecked" })
public void testBasicUsage() {

    Session session = sessionFactory.openSession();
    session.beginTransaction();//ww w.  j  a va2  s.  c o  m
    session.save(new TeachingPlanMandatory(2, 1, 0, 1, 3, 0, 0, 0, 1, 2, 6, 4, 2, 2, 0, 0, 0, 2, 3, 2, 0));
    session.save(new TeachingPlanMandatory(4, 2, 3, 1, 3, 0, 0, 0, 1, 2, 6, 4, 2, 2, 0, 0, 0, 2, 3, 2, 0));
    session.getTransaction().commit();
    session.close();

    session = sessionFactory.openSession();
    session.beginTransaction();
    List result = session.createQuery("from TeachingPlanMandatory").list();
    for (TeachingPlanMandatory m : (List<TeachingPlanMandatory>) result) {
        System.out.println("Test (" + m.getId() + ") : " + m.getTikyba());
    }
    session.getTransaction().commit();
    session.close();
}

From source file:calculatefactorrisk.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from  w w w . j av a  2 s  . co m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();
        HttpSession hs = request.getSession();

        if (hs.getAttribute("patient") != null) {

            Patient p1 = (Patient) hs.getAttribute("patient");
            int metervalue = 0;
            double rmetervalue = 0;
            int facindex = 0;
            int points = 0;
            String[] chartdata = new String[4];
            chartdata[0] = "5";
            chartdata[1] = "12";
            chartdata[2] = "25";
            chartdata[3] = "60";
            ArrayList<PatientfactorAnswer> finalans = new ArrayList<PatientfactorAnswer>();

            if (request.getParameter("facindex") != null && request.getParameter("ans") != null) {

                if (hs.getAttribute("metervalue") != null) {
                    metervalue = (Integer) hs.getAttribute("metervalue");
                }
                if (hs.getAttribute("finalans") != null) {
                    finalans = (ArrayList<PatientfactorAnswer>) hs.getAttribute("finalans");
                }
                if (request.getParameter("ans").equals("15")) {
                    hs.setAttribute("gender", "MALE");
                    hs.setAttribute("agefactor", null);
                }
                if (request.getParameter("ans").equals("16")) {
                    hs.setAttribute("gender", "FEMALE");
                    hs.setAttribute("agefactor", null);
                }

                facindex = Integer.parseInt(request.getParameter("facindex"));
                if (request.getParameter("next") != null) {

                    int ansid = Integer.parseInt(request.getParameter("ans"));
                    FactorOption ansfo = (FactorOption) ss.get(FactorOption.class, ansid);
                    if (ansfo.getFactorId().equals(new FactorDetails(2))) {
                        hs.setAttribute("agefactor", ansfo);
                    }

                    PatientfactorAnswer currentans = new PatientfactorAnswer();
                    currentans.setFactorId(ansfo.getFactorId());
                    currentans.setFactorOptionId(ansfo);
                    currentans.setPId(p1);

                    if (finalans.size() > 0) {
                        ArrayList<PatientfactorAnswer> finalans1 = finalans;
                        for (int i1 = 0; i1 < finalans1.size(); i1++) {
                            PatientfactorAnswer c1 = finalans.get(i1);
                            if (c1.getFactorId().equals(currentans.getFactorId())) {
                                finalans.remove(i1);
                            }
                        }
                    }
                    finalans.add(facindex, currentans);

                    //                        System.out.println(finalans.get(0).getFactorOptionId());
                    //                        System.out.println(finalans.get(1).getFactorOptionId());
                    //                        System.out.println(finalans.get(2).getFactorOptionId());
                    System.out.println("size : " + finalans.size());
                    facindex = facindex + 1;
                } else if (request.getParameter("prev") != null) {
                    if (facindex > 0) {
                        facindex = facindex - 1;
                    }
                }

                if (finalans.size() > 0) {
                    String gender = "MALE";
                    if (hs.getAttribute("gender") != null) {
                        gender = (String) hs.getAttribute("gender");
                    }
                    if (gender.equals("MALE") && hs.getAttribute("agefactor") != null) {

                        FactorOption agefac = (FactorOption) hs.getAttribute("agefactor");

                        for (PatientfactorAnswer c1 : finalans) {
                            points = points + c1.getFactorOptionId().getMen();
                        }
                        if (points < 0) {
                            points = 0;
                        }
                        if (points > 14) {
                            points = 14;
                        }
                        //                Count risk on point
                        Criteria cr2 = ss.createCriteria(AbsulateRiskMale.class);
                        cr2.add(Restrictions.eq("foptionid", agefac));
                        cr2.add(Restrictions.eq("point", points));
                        ArrayList<AbsulateRiskMale> riskdata = (ArrayList<AbsulateRiskMale>) cr2.list();
                        if (riskdata.size() > 0) {
                            AbsulateRiskMale risk = riskdata.get(0);
                            if (risk.getGreen() != null) {
                                metervalue = risk.getGreen();
                            } else if (risk.getViolet() != null) {
                                metervalue = risk.getViolet();
                            } else if (risk.getYellow() != null) {
                                metervalue = risk.getYellow();
                            } else if (risk.getRed() != null) {
                                metervalue = risk.getRed();
                            }
                            if (risk.getRGreen() != null) {
                                rmetervalue = risk.getRGreen();
                            } else if (risk.getRViolet() != null) {
                                rmetervalue = risk.getRViolet();
                            } else if (risk.getRYellow() != null) {
                                rmetervalue = risk.getRYellow();
                            } else if (risk.getRRed() != null) {
                                rmetervalue = risk.getRRed();
                            }
                            //                                System.out.println("Risk data : " + risk);
                        }
                        System.out.println("Meter value : " + metervalue);
                        System.out.println("RMeter value : " + rmetervalue);
                        //                        Set Chart Data    
                        Criteria cr01 = ss.createCriteria(AbsulateRiskMale.class);
                        cr01.add(Restrictions.eq("foptionid", agefac));
                        cr01.setProjection(Projections.max("green"));
                        if (cr01.uniqueResult() != null) {
                            chartdata[0] = cr01.uniqueResult().toString();
                        }
                        Criteria cr02 = ss.createCriteria(AbsulateRiskMale.class);
                        cr02.add(Restrictions.eq("foptionid", agefac));
                        cr02.setProjection(Projections.max("violet"));
                        if (cr02.uniqueResult() != null) {
                            chartdata[1] = cr02.uniqueResult().toString();
                        }
                        Criteria cr03 = ss.createCriteria(AbsulateRiskMale.class);
                        cr03.add(Restrictions.eq("foptionid", agefac));
                        cr03.setProjection(Projections.max("yellow"));
                        if (cr03.uniqueResult() != null) {
                            chartdata[2] = cr03.uniqueResult().toString();
                        }
                        chartdata[3] = "54";
                    }
                    if (gender.equals("FEMALE") && hs.getAttribute("agefactor") != null) {

                        FactorOption agefac = (FactorOption) hs.getAttribute("agefactor");

                        for (PatientfactorAnswer c1 : finalans) {
                            points = points + c1.getFactorOptionId().getWomen();
                        }
                        if (points < 0) {
                            points = 0;
                        }
                        if (points > 17) {
                            points = 17;
                        }
                        //                Count risk on point
                        Criteria cr2 = ss.createCriteria(AbsoluteRiskFemale.class);
                        cr2.add(Restrictions.eq("foptionid", agefac));
                        cr2.add(Restrictions.eq("point", points));
                        ArrayList<AbsoluteRiskFemale> riskdata = (ArrayList<AbsoluteRiskFemale>) cr2.list();
                        if (riskdata.size() > 0) {
                            AbsoluteRiskFemale risk = riskdata.get(0);
                            if (risk.getGreen() != null) {
                                metervalue = risk.getGreen();
                            } else if (risk.getViolet() != null) {
                                metervalue = risk.getViolet();
                            } else if (risk.getYellow() != null) {
                                metervalue = risk.getYellow();
                            } else if (risk.getRed() != null) {
                                metervalue = risk.getRed();
                            }
                            if (risk.getRGreen() != null) {
                                rmetervalue = risk.getRGreen();
                            } else if (risk.getRViolet() != null) {
                                rmetervalue = risk.getRViolet();
                            } else if (risk.getRYellow() != null) {
                                rmetervalue = risk.getRYellow();
                            } else if (risk.getRRed() != null) {
                                rmetervalue = risk.getRRed();
                            }
                            //                                System.out.println("Risk data : " + risk);
                        }
                        //                            System.out.println("Meter value : " + metervalue);
                        //                        Set Chart Data    
                        Criteria cr01 = ss.createCriteria(AbsoluteRiskFemale.class);
                        cr01.add(Restrictions.eq("foptionid", agefac));
                        cr01.setProjection(Projections.max("green"));
                        if (cr01.uniqueResult() != null) {
                            chartdata[0] = cr01.uniqueResult().toString();
                        }
                        Criteria cr02 = ss.createCriteria(AbsoluteRiskFemale.class);
                        cr02.add(Restrictions.eq("foptionid", agefac));
                        cr02.setProjection(Projections.max("violet"));
                        if (cr02.uniqueResult() != null) {
                            chartdata[1] = cr02.uniqueResult().toString();
                        }
                        Criteria cr03 = ss.createCriteria(AbsoluteRiskFemale.class);
                        cr03.add(Restrictions.eq("foptionid", agefac));
                        cr03.setProjection(Projections.max("yellow"));
                        if (cr03.uniqueResult() != null) {
                            chartdata[2] = cr03.uniqueResult().toString();
                        }
                        chartdata[3] = "30";
                    }
                }

            }
            System.out.println("sssss : " + hs.getAttribute("agefactor"));
            Criteria cr = ss.createCriteria(FactorDetails.class);
            ArrayList<FactorDetails> fdlist = (ArrayList<FactorDetails>) cr.list();
            if (fdlist.size() > facindex) {
                FactorDetails factor = fdlist.get(facindex);

                Criteria cr1 = ss.createCriteria(FactorOption.class);
                cr1.add(Restrictions.eq("factorId", factor));
                ArrayList<FactorOption> folist = (ArrayList<FactorOption>) cr1.list();
                if (folist.size() > 0) {
                    request.setAttribute("folist", folist);
                }

                //                    metervalue = metervalue + 3;
                hs.setAttribute("metervalue", metervalue);
                hs.setAttribute("rmetervalue", rmetervalue);
                hs.setAttribute("points", points);
                hs.setAttribute("finalans", finalans);
                hs.setAttribute("chartdata", chartdata);
                request.setAttribute("factor", factor);
                request.setAttribute("facindex", facindex);

            } else {
                //                    Save In database final ans
                ArrayList<PatientfactorAnswer> save = (ArrayList<PatientfactorAnswer>) hs
                        .getAttribute("finalans");
                Calculationmeteredata cm = new Calculationmeteredata();

                cm.setFinalpoints((Integer) hs.getAttribute("points"));
                cm.setMetervalue((Integer) hs.getAttribute("metervalue"));
                cm.setRmetervalue((Double) hs.getAttribute("rmetervalue"));
                cm.setDate(new Date().toString());
                cm.setPId(p1);

                ss.save(cm);
                if (cm.getCalculationmeteredataId() != null) {
                    for (PatientfactorAnswer pans : save) {
                        pans.setCalculationmeteredataId(cm);
                        ss.save(pans);
                    }
                }
                tr.commit();
                RequestDispatcher rd = request.getRequestDispatcher(
                        "systemgeneratedreport?calcid=" + cm.getCalculationmeteredataId());
                rd.forward(request, response);
            }

            tr.commit();
            RequestDispatcher rd = request.getRequestDispatcher("heartattack_calculater.jsp");
            rd.forward(request, response);
        } else {
            tr.commit();
            RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
            rd.forward(request, response);
        }

    }
}

From source file:take_doc_app.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from w ww.  ja va2  s  .co m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();
        HttpSession hs = request.getSession();
        if (hs.getAttribute("patient") != null) {
            if (request.getParameter("docid") != null) {
                Patient p = (Patient) hs.getAttribute("patient");
                int docid = Integer.parseInt(request.getParameter("docid"));

                Doctor d1 = new Doctor(docid);
                DoctorAppointment dappo = new DoctorAppointment();
                dappo.setPId(p);
                dappo.setDId(d1);
                dappo.setStatus("PENDING");
                ss.save(dappo);
                request.setAttribute("msg", "Your Appointment has been booked..!");
            }
            tr.commit();
            RequestDispatcher rd = request.getRequestDispatcher("consult_cardiac.jsp");
            rd.forward(request, response);

        } else {
            tr.commit();
            RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
            rd.forward(request, response);
        }
    }
}

From source file:addnewtest.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//  w ww.ja  v a  2  s .c om
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */

        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();
        HttpSession hs = request.getSession();
        if (hs.getAttribute("lab") != null) {
            Lab lb = (Lab) hs.getAttribute("lab");

            if (request.getParameter("price") != null && request.getParameter("tid") != null) {
                int tid = Integer.parseInt(request.getParameter("tid"));
                Double price = Double.parseDouble(request.getParameter("price"));
                Labtest lt = new Labtest();
                lt.setLId(lb);
                lt.setTId(new Test(tid));
                lt.setTestFees(price);
                ss.save(lt);
                request.setAttribute("msg", "Test added to your test list.!");
            }

            Criteria cr = ss.createCriteria(Test.class);
            ArrayList<Test> alltest = (ArrayList<Test>) cr.list();
            ArrayList<Test> alltest1 = alltest;
            if (alltest.size() > 0) {
                Criteria cr1 = ss.createCriteria(Labtest.class);
                cr1.add(Restrictions.eq("lId", lb));
                ArrayList<Labtest> ltest = (ArrayList<Labtest>) cr1.list();
                if (ltest.size() > 0) {
                    for (int i = 0; i < ltest.size(); i++) {
                        for (int j = 0; j < alltest.size(); j++) {
                            if (ltest.get(i).getTId().equals(alltest.get(j))) {
                                alltest1.remove(j);
                            }
                        }
                    }
                }
                request.setAttribute("alltest", alltest1);
            }
            tr.commit();
            RequestDispatcher rd = request.getRequestDispatcher("lab_addnewtest.jsp");
            rd.forward(request, response);

        } else {
            tr.commit();
            RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
            rd.forward(request, response);
        }

    }
}

From source file:userRegistration.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./* w w  w . j a va2s. co  m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {

        Session session = NewHibernateUtil.getSessionFactory().openSession();
        Transaction tr = session.beginTransaction();

        Login lt = new Login();
        lt.setUname(request.getParameter("uname"));
        lt.setPwd(request.getParameter("pwd"));
        session.save(lt);

        UserReg ur = new UserReg();
        ur.setEmail(request.getParameter("email"));
        ur.setMobile(request.getParameter("mobile"));
        ur.setUid(lt);
        session.save(ur);

        tr.commit();
        out.println("Success");
    }

    catch (Exception ex) {
        out.println(ex.getMessage());
    }
}

From source file:SalvarPincel.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from w w  w  .  j a  v a 2s  .  co m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {

        Pincel p1 = new Pincel();
        p1.setCor("azul");
        p1.setFabricante("Pilot");
        p1.setNum_serie(10254);

        Session sessao = HibernateUtil.getSessionFactory().openSession();

        Transaction tx = sessao.beginTransaction();
        try {
            sessao.save(p1);
            sessao.flush();
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        }
        sessao.close();

        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet SalvarPincel</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet SalvarPincel at " + request.getContextPath() + "</h1>");
        out.println("<h1>kkkkk " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

From source file:creategroup_serv.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    try {//from  ww  w .  ja  va2  s  .co  m

        SessionFactory sf = NewHibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();

        HttpSession hs = request.getSession();
        Login lg2 = (Login) hs.getAttribute("user");
        ArrayList<Login> gpList = (ArrayList<Login>) hs.getAttribute("gpList");
        gpList.add(lg2);
        GroupDetail gd = new GroupDetail();

        gd.setGName(request.getParameter("gname"));
        gd.setUId(lg2);
        gd.setUName(lg2.getUName());

        ss.save(gd);

        for (Login l : gpList) {
            GmemberDetail gmd = new GmemberDetail();
            gmd.setGId(gd);
            gmd.setUId(l);
            ss.save(gmd);
        }
        tr.commit();

        hs.removeAttribute("gpList");
        out.println("Group Created");
        RequestDispatcher rd = request.getRequestDispatcher("getgroups?id=0");
        rd.forward(request, response);

    } catch (HibernateException he) {
        out.print(he.getMessage());
    }
}