How to render velocity template in servlet?

Chong Jing Hong June 22, 2021

I have a servlet class that handles csv file uploads. I want the plugin to render a velocity template after the upload is complete to say that the file upload has been completed. Here is my code so far:

public class BulkUserCreatorToolServlet extends HttpServlet{
private final TemplateRenderer renderer;    
private final WebResourceManager webResourceManager;
public BulkUserCreatorToolServlet(TemplateRenderer renderer, WebResourceManager webResourceManager){        
this.renderer = renderer;        
this.webResourceManager = webResourceManager;    
}

    @Override    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOExceptionServletException {
        // Create a factory for disk-based file items        
DiskFileItemFactory factory = new DiskFileItemFactory();
        // Configure a repository (to ensure a secure temp location is used)        
ServletContext servletContext = this.getServletConfig().getServletContext();        
File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");        
factory.setRepository(repository);

        // Create a new file upload handler        
ServletFileUpload upload = new ServletFileUpload(factory);
        try{            
//Parse the request to get file items            
List<FileItem> fileItems = upload.parseRequest(request);
            // Process the uploaded items            
Iterator<FileItem> iter = fileItems.iterator();         
while(iter.hasNext()){                
//handle file            
}
            

Map<StringObject> context = new HashMap<StringObject>();            
context.put(
"testname""testvalue");                        
response.setContentType(
"text/html;charset=UTF-8");            
response.getWriter().print(renderer.render(
"plugin-key","response-page.vm", context));        
}
        
catch(FileUploadException e){            
e.printStackTrace();
        
}
        
catch(Exception e){            
e.printStackTrace();
        
}
    
}

}

 Upon upload, Page Not Found always shows. What did I do wrong? Thank you

2 answers

0 votes
Michael Aglas February 13, 2023

have you defined the servlet in the atlassian-plugin.xml?

0 votes
Fabian Lim
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 27, 2021

Hi @Chong Jing Hong

You may want to post this on the Atlassian Development community for a faster response: https://community.developer.atlassian.com/

Regards

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events