You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by yoursoft <yo...@freemail.hu> on 2007/03/22 09:12:21 UTC

How to migrate my VelocityServlet code to VelocityViewServlet

Dear List,

I found that in new Velocity the VelocityServelt depricated, and will be 
removed.

I would like to migrate my existing sources to new structure (assign all 
*.vm to VelocityViewServelt (VVS) (or extend VVS as VelocityServelt?) ), 
but I have some mistakes.
My current servlets code structure are like followings:

package wf.fizel;

import java.sql.Connection;
import java.sql.ResultSet;
import javax.naming.InitialContext;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.apache.velocity.Template;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;
import org.apache.velocity.servlet.VelocityServlet;
import my.system.SessionParser;
import my.system.Tools;
import my.system.exceptionhandling.MyException;
import my.system.utils.*;


public class Igeny extends VelocityServlet {
   
    private DataSource poolWorkFlow = null; // DB connection pool
     
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
       
        InitialContext initCtx = null;
        try {
            initCtx = new InitialContext();
            pri_poolWorkFlow = (DataSource) 
initCtx.lookup("java:comp/env/jdbc/workflow");
        } catch (Exception e) {
            throw new ServletException("no datasource found " +e);
        }
    }
   
    public Template handleRequest( HttpServletRequest req,  
HttpServletResponse resp,  Context context){
       
        Template template = null;
        StringBuffer sb = new StringBuffer();
        final String lang = req.getParameter("lang");
        final String companyId = req.getParameter("companyId");
        String id2 = "";
        SessionParser sessionp = null;
        Connection db = null;
        ResultSet rs = null;
        int max=0;
       
        try {          
            sessionp = new SessionParser( req.getSession(false) );
            db = poolWorkFlow.getConnection();
           
            rs = Utils.getMan( sessionp.getStringParameter("userid"), db);
           String options = Options.setOptionValue(rs, "", false);

            try {
                rs.first();
                context.put("ORIGMAN", rs.getString(1));
                rs.close();
            } catch (Exception e) {}
           
            sb.append("select * from table where userid::text) ='");
            sb.append(sessionp.getStringParameter("userid"));
            sb.append("'");
            rs = Db.getSql(sb.toString(), db);
            if (rs.first()) {
                id2 = rs.getString("id");
                max = rs.getInt("min_paid")*3;
            } else {
                throw new Exception("wrong parameter!");
            }
.....          
         
            context.put("MAX", Szoveg.getIntStr(max,"###,###,###", '.'));
            context.put("MAX_NUM", String.valueOf(max));
            context.put("ID", id2);
          
            context.put("LANG", lang );
            context.put("COMPANY",  companyId);
            context.put("SESSION", (req.getSession(false)).getId());
         
            template = Velocity.getTemplate(
                    MyStrings.getTemplateName(sb.toString(), lang, 
companyId));
        } catch( Exception e ) {
            MyException.forward(e, 1, req, resp);
        } finally {
            try {db.close(); } catch (Exception e2) {}
        }
       
        return template;
    }
}

How you can rewrite this code structure into VVS? Can you help to me 
with an example? :-)
I don't would like to decrease server performance.

Regards,
    Ferenc

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: How to migrate my VelocityServlet code to VelocityViewServlet

Posted by yoursoft <yo...@freemail.hu>.
Dear Nathan,

Thanks for help :-)

Regards,
    Ferenc

Nathan Bubna wrotte:
> On 3/22/07, yoursoft <yo...@freemail.hu> wrote:
>> Dear List,
>>
>> I found that in new Velocity the VelocityServelt depricated, and will be
>> removed.
>>
>> I would like to migrate my existing sources to new structure (assign all
>> *.vm to VelocityViewServelt (VVS) (or extend VVS as VelocityServelt?) ),
>> but I have some mistakes.
>
> you'll want to extend the VVS.   it should only require three changes:
>
> import org.apache.velocity.tools.view.servlet.VelocityViewServlet;
>
> extends VelocityViewServlet //(instead of VelocityServlet)
>
> getVelocityEngine().getTemplate(...); //instead of 
> Velocity.getTemplate(...)
>
>> My current servlets code structure are like followings:
>>
>> package wf.fizel;
>>
>> import java.sql.Connection;
>> import java.sql.ResultSet;
>> import javax.naming.InitialContext;
>> import javax.servlet.ServletConfig;
>> import javax.servlet.ServletException;
>> import javax.servlet.http.HttpServletRequest;
>> import javax.servlet.http.HttpServletResponse;
>> import javax.sql.DataSource;
>> import org.apache.velocity.Template;
>> import org.apache.velocity.app.Velocity;
>> import org.apache.velocity.context.Context;
>> import org.apache.velocity.tools.view.servlet.VelocityViewServlet;
>> import my.system.SessionParser;
>> import my.system.Tools;
>> import my.system.exceptionhandling.MyException;
>> import my.system.utils.*;
>>
>>
>> public class Igeny extends VelocityViewServlet {
>>
>>     private DataSource poolWorkFlow = null; // DB connection pool
>>
>>     public void init(ServletConfig config) throws ServletException {
>>         super.init(config);
>>
>>         InitialContext initCtx = null;
>>         try {
>>             initCtx = new InitialContext();
>>             pri_poolWorkFlow = (DataSource)
>> initCtx.lookup("java:comp/env/jdbc/workflow");
>>         } catch (Exception e) {
>>             throw new ServletException("no datasource found " +e);
>>         }
>>     }
>>
>>     public Template handleRequest( HttpServletRequest req,
>> HttpServletResponse resp,  Context context){
>>
>>         Template template = null;
>>         StringBuffer sb = new StringBuffer();
>>         final String lang = req.getParameter("lang");
>>         final String companyId = req.getParameter("companyId");
>>         String id2 = "";
>>         SessionParser sessionp = null;
>>         Connection db = null;
>>         ResultSet rs = null;
>>         int max=0;
>>
>>         try {
>>             sessionp = new SessionParser( req.getSession(false) );
>>             db = poolWorkFlow.getConnection();
>>
>>             rs = Utils.getMan( sessionp.getStringParameter("userid"), 
>> db);
>>            String options = Options.setOptionValue(rs, "", false);
>>
>>             try {
>>                 rs.first();
>>                 context.put("ORIGMAN", rs.getString(1));
>>                 rs.close();
>>             } catch (Exception e) {}
>>
>>             sb.append("select * from table where userid::text) ='");
>>             sb.append(sessionp.getStringParameter("userid"));
>>             sb.append("'");
>>             rs = Db.getSql(sb.toString(), db);
>>             if (rs.first()) {
>>                 id2 = rs.getString("id");
>>                 max = rs.getInt("min_paid")*3;
>>             } else {
>>                 throw new Exception("wrong parameter!");
>>             }
>> .....
>>
>>             context.put("MAX", Szoveg.getIntStr(max,"###,###,###", 
>> '.'));
>>             context.put("MAX_NUM", String.valueOf(max));
>>             context.put("ID", id2);
>>
>>             context.put("LANG", lang );
>>             context.put("COMPANY",  companyId);
>>             context.put("SESSION", (req.getSession(false)).getId());
>>
>>             template = getVelocityEngine().getTemplate(
>>                     MyStrings.getTemplateName(sb.toString(), lang,
>> companyId));
>>         } catch( Exception e ) {
>>             MyException.forward(e, 1, req, resp);
>>         } finally {
>>             try {db.close(); } catch (Exception e2) {}
>>         }
>>
>>         return template;
>>     }
>> }
>>
>> How you can rewrite this code structure into VVS? Can you help to me
>> with an example? :-)
>> I don't would like to decrease server performance.
>>
>> Regards,
>>     Ferenc
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: How to migrate my VelocityServlet code to VelocityViewServlet

Posted by Nathan Bubna <nb...@gmail.com>.
On 3/22/07, yoursoft <yo...@freemail.hu> wrote:
> Dear List,
>
> I found that in new Velocity the VelocityServelt depricated, and will be
> removed.
>
> I would like to migrate my existing sources to new structure (assign all
> *.vm to VelocityViewServelt (VVS) (or extend VVS as VelocityServelt?) ),
> but I have some mistakes.

you'll want to extend the VVS.   it should only require three changes:

import org.apache.velocity.tools.view.servlet.VelocityViewServlet;

extends VelocityViewServlet //(instead of VelocityServlet)

getVelocityEngine().getTemplate(...); //instead of Velocity.getTemplate(...)

> My current servlets code structure are like followings:
>
> package wf.fizel;
>
> import java.sql.Connection;
> import java.sql.ResultSet;
> import javax.naming.InitialContext;
> import javax.servlet.ServletConfig;
> import javax.servlet.ServletException;
> import javax.servlet.http.HttpServletRequest;
> import javax.servlet.http.HttpServletResponse;
> import javax.sql.DataSource;
> import org.apache.velocity.Template;
> import org.apache.velocity.app.Velocity;
> import org.apache.velocity.context.Context;
> import org.apache.velocity.tools.view.servlet.VelocityViewServlet;
> import my.system.SessionParser;
> import my.system.Tools;
> import my.system.exceptionhandling.MyException;
> import my.system.utils.*;
>
>
> public class Igeny extends VelocityViewServlet {
>
>     private DataSource poolWorkFlow = null; // DB connection pool
>
>     public void init(ServletConfig config) throws ServletException {
>         super.init(config);
>
>         InitialContext initCtx = null;
>         try {
>             initCtx = new InitialContext();
>             pri_poolWorkFlow = (DataSource)
> initCtx.lookup("java:comp/env/jdbc/workflow");
>         } catch (Exception e) {
>             throw new ServletException("no datasource found " +e);
>         }
>     }
>
>     public Template handleRequest( HttpServletRequest req,
> HttpServletResponse resp,  Context context){
>
>         Template template = null;
>         StringBuffer sb = new StringBuffer();
>         final String lang = req.getParameter("lang");
>         final String companyId = req.getParameter("companyId");
>         String id2 = "";
>         SessionParser sessionp = null;
>         Connection db = null;
>         ResultSet rs = null;
>         int max=0;
>
>         try {
>             sessionp = new SessionParser( req.getSession(false) );
>             db = poolWorkFlow.getConnection();
>
>             rs = Utils.getMan( sessionp.getStringParameter("userid"), db);
>            String options = Options.setOptionValue(rs, "", false);
>
>             try {
>                 rs.first();
>                 context.put("ORIGMAN", rs.getString(1));
>                 rs.close();
>             } catch (Exception e) {}
>
>             sb.append("select * from table where userid::text) ='");
>             sb.append(sessionp.getStringParameter("userid"));
>             sb.append("'");
>             rs = Db.getSql(sb.toString(), db);
>             if (rs.first()) {
>                 id2 = rs.getString("id");
>                 max = rs.getInt("min_paid")*3;
>             } else {
>                 throw new Exception("wrong parameter!");
>             }
> .....
>
>             context.put("MAX", Szoveg.getIntStr(max,"###,###,###", '.'));
>             context.put("MAX_NUM", String.valueOf(max));
>             context.put("ID", id2);
>
>             context.put("LANG", lang );
>             context.put("COMPANY",  companyId);
>             context.put("SESSION", (req.getSession(false)).getId());
>
>             template = getVelocityEngine().getTemplate(
>                     MyStrings.getTemplateName(sb.toString(), lang,
> companyId));
>         } catch( Exception e ) {
>             MyException.forward(e, 1, req, resp);
>         } finally {
>             try {db.close(); } catch (Exception e2) {}
>         }
>
>         return template;
>     }
> }
>
> How you can rewrite this code structure into VVS? Can you help to me
> with an example? :-)
> I don't would like to decrease server performance.
>
> Regards,
>     Ferenc
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org