[jboss-user] [Tomcat, HTTPD, Servlets & JSP] - Re: upload file

raffaele.letizia do-not-reply at jboss.com
Sat Feb 14 07:13:54 EST 2009


the code for addadmin.jsp is there:
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>

<portlet:defineObjects/>

  
    
        Domanda
          
    
    
        Risposta
          
    
    
          Seleziona un file da upload :
    
    
      
        
        
    
    
      
        
        

        
    

  
  
  





my portlet faqportlet is:

package com.test;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Expression;



import javax.portlet.PortletSession;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletSecurityException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;



public class FaqPortlet extends GenericPortlet
{
   private SessionFactory sessionFactory;



   public void init()
   {
      Configuration config = new Configuration();
      config.configure("hibernate.cfg.xml");
      this.sessionFactory = config.buildSessionFactory();
   }

   public void processAction(ActionRequest request, ActionResponse response) throws PortletException, PortletSecurityException, IOException
   {


      String op = request.getParameter("op");
      if((op != null) && (op.trim().length() > 0))
      {
         String key = request.getParameter("key");
         String domanda = request.getParameter("domanda");
         String risposta = request.getParameter("risposta");
         String filename = request.getParameter("FILENAME");
         

         if(op.equalsIgnoreCase("Aggiungi FAQ"))
         {
            addNewContact(request.getUserPrincipal().getName(),domanda,risposta,filename);
         }
         else if(op.equalsIgnoreCase("Aggiorna FAQ"))
         {
            saveContact(key,domanda,risposta,filename);
         }
         else if(op.equalsIgnoreCase("Cancella FAQ"))
         {
            deleteContact(key);
         }
      }
   }

   public void doView(RenderRequest request, RenderResponse response)
   {
      try
      {
         if((request.getUserPrincipal() != null) && (request.getUserPrincipal().getName() != null))
         {
            String userName = request.getUserPrincipal().getName();
            String jspName = null;
            /*verifica per l'admin che ha il controllo totale */
            System.out.println(userName);



              if((request.getParameter("op") != null) && (request.getParameter("op").equalsIgnoreCase("Ricerca")))
                {
                  if (userName.equals("admin"))
                      {
                       request.setAttribute("contacts", getUsersContacts(userName, request.getParameter("domanda")));
                       jspName ="/WEB-INF/jsp/listadmin.jsp";
                     }
                  else
                      {
                       request.setAttribute("contacts", getUsersContacts(userName, request.getParameter("domanda")));
                       jspName ="/WEB-INF/jsp/listuser.jsp";
                       }
              }
                else if((request.getParameter("op") != null) && (request.getParameter("op").equalsIgnoreCase("shownew")))
                         {
                          if (userName.equals("admin"))
                         {
                            jspName ="/WEB-INF/jsp/addadmin.jsp";
                         }
                          else
                          {
                            jspName ="/WEB-INF/jsp/adduser.jsp";
                         }}

                     else if((request.getParameter("op") != null) && (request.getParameter("op").equalsIgnoreCase("edit")))
                             {
                               if (userName.equals("admin"))
                                 {
                                 request.setAttribute("contact", findContact(userName, request.getParameter("key")));
                                 jspName ="/WEB-INF/jsp/editadmin.jsp";
                                 }

                           }
                           else if((request.getParameter("op") != null) && (request.getParameter("op").equalsIgnoreCase("delete")))
                                 {
                                  if (userName.equals("admin"))
                                   {
                                    request.setAttribute("contact", findContact(userName, request.getParameter("key")));
                                    jspName ="/WEB-INF/jsp/deleteadmin.jsp";
                                   }
                                  }
                                   else
                                  { if (userName.equals("admin"))
                                        {
                                         request.setAttribute("contacts", getUsersContacts(userName));
                                         jspName ="/WEB-INF/jsp/listadmin.jsp";
                                        }
                                    else
                                        {
                                         request.setAttribute("contacts", getUsersContacts(userName));
                                         jspName ="/WEB-INF/jsp/listuser.jsp";
                                        }
                                    }
   


            response.setContentType("text/html");
            PortletRequestDispatcher prd = getPortletContext().getRequestDispatcher(jspName);
            prd.include(request, response);
         }
         else
         {
            response.setContentType("text/html");
            PortletRequestDispatcher prd = getPortletContext().getRequestDispatcher("/WEB-INF/jsp/notloggedin.jsp");
            prd.include(request, response);
         }
      }
      catch(Exception e)
      {
         e.printStackTrace();
      }
   }

   private List getUsersContacts(String userName)
   {
      Session session = this.sessionFactory.openSession();
      Criteria criteria = session.createCriteria(Faq.class);
      //criteria.add(Expression.eq("userName", userName));
      List contacts = criteria.list();
      session.disconnect();
      return contacts;
   }

   private List getUsersContacts(String userName, String name)
   {
      Session session = this.sessionFactory.openSession();
      Criteria criteria = session.createCriteria(Faq.class);
      //criteria.add(Expression.eq("userName", userName));
      //criteria.add(Expression.like("domanda", name, MatchMode.ANYWHERE));
      List contacts = criteria.list();
      session.disconnect();
      return contacts;
   }

   private void addNewContact(String userName, String domanda1, String risposta1,String filename1)
   {
      Session session = this.sessionFactory.openSession();
      Transaction t = session.beginTransaction();
      try
      {
         Faq faq = new Faq();
         faq.setDomanda(domanda1);
         faq.setRisposta(risposta1);
         faq.setUserName(userName);
         faq.setFilename(filename1);
         session.save(faq);
         t.commit();
      }
      catch(Exception e)
      {
         t.rollback();
      }
      finally
      {
         if(session != null)
         {
            session.disconnect();
         }
      }
   }

   private Faq findContact(String userName, String id)
   {
      Session session = this.sessionFactory.openSession();
      Criteria criteria = session.createCriteria(Faq.class);
      //criteria.add(Expression.eq("userName", userName));
      criteria.add(Expression.eq("key", new Long(id)));
      List contacts = criteria.list();
      session.disconnect();
      if(contacts.size() == 1)
      {
         return (Faq) contacts.get(0);
      }
      return null;
   }

   private void saveContact(String key, String domanda2, String risposta2,String filename2)
   {
      Session session = this.sessionFactory.openSession();
      Transaction t = session.beginTransaction();
      try
      {
         Faq faq = (Faq) session.load(Faq.class, new Long(key));
         faq.setDomanda(domanda2);
         faq.setRisposta(risposta2);
         faq.setFilename(filename2);
         session.update(faq);
         t.commit();
      }
      catch(Exception e)
      {
         t.rollback();
      }
      finally
      {
         if(session != null)
         {
            session.disconnect();
         }
      }
   }


   private void deleteContact(String key)
   {
      Session session = this.sessionFactory.openSession();
      Transaction t = session.beginTransaction();
      try
      {
         Faq contact = (Faq) session.load(Faq.class, new Long(key));

         session.delete(contact);
         t.commit();
      }
      catch(Exception e)
      {
         t.rollback();
      }
      finally
      {
         if(session != null)
         {
            session.disconnect();
         }
      }
   }
}


where do you put the createactionURL in my code for result that is ok? can i send my code on your email?

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4210119#4210119

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4210119



More information about the jboss-user mailing list