0

I'm a rookie programmer developing my first dynamic web application on eclipse for tomcat. It communicates with a mySQL database and modifies some template files and saves them as new ones. The app needs to be portable. I know about absolute and relative paths, but am super confused about deciding where to put the extra resources and what to put in my sql database path so they can be accessed by the .war file. On my PC I'm able to connect to mySQL @ jdbc:mysql//localhost:3306/dbname. But I'm sure this url won't work on any other device..

How do I make everything portable? In what locations should I copy my templates to create a .war application which can access them using relative paths!

I found some help for the latter part here but I think it requires putting the files in the tomcat directory which in my opinion won't be bundled with the app.

Edit: Here's my code

Web form

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>       
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="test">
<input type="text" name="name"> </input>
</form>
<button type="submit">Create</button>
</body>
</html>

Servlet:

package rtest;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Testservlet
 */
@WebServlet(description = "testing", urlPatterns = { "/test" })
public class Testservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        File source = new File(getServletContext().getRealPath("templates/fiel1.docx"));
        File dest = new File(getServletContext().getRealPath("templates/FF.docx"));

        try{
            Files.copy(source.toPath(), dest.toPath());
        }catch(Exception e)
        {
            e.printStackTrace();

        }
    }

}

Edit: I'm getting a null pointer exception although I have the templates folder in under web content!

Community
  • 1
  • 1
Harry S
  • 23
  • 6
  • Yes, it will work. Why do you think it won't? – Branislav Lazic Aug 14 '14 at 18:39
  • @BranislavLazic About the db part, for it to work on a foreign device, the latter should have a db there by the name dbname and that machine's sql server should be running on the port 3306, which I don't think will be the case! – Harry S Aug 14 '14 at 18:44
  • The form is a static Jsp. Its not neccessary to solve the question. You should post the stack of the exception. Is your app unpacked when deployed at the server? If its not, getRealPath() will return null. – Stefan Aug 15 '14 at 06:25

1 Answers1

0

But I'm sure this url won't work on any other device

In that case that host will act as local host The url will work unless you are trying to connect some remote server with different port.

In what locations should I copy my templates

It should be placed under the web-content or web-app directory

Change

File source = new File(getServletContext().getRealPath("templates/fiel1.docx"));

to

File source = new File(getServletContext().getRealPath("/templates/fiel1.docx"));

Assuming templates is a directory under web-content and the war is exploded during deployment

SparkOn
  • 8,806
  • 4
  • 29
  • 34
  • 1) how will the db be available to that host?2) what (relative?) path would I assign to new File() function if I place my templates in web-content/web-app diretory? – Harry S Aug 14 '14 at 18:46
  • you can either export database as file using H2 db or you need to install database on the server. Try reading the file using servletContext or read it using resourceAsStream – SparkOn Aug 14 '14 at 18:48
  • Thank you for clearing the first part. About the second one, right now I'm using getServletContext().getRealPath("/templates/myfile"). Where does the getServletContext() point? Can I use it or is there another way? – Harry S Aug 14 '14 at 18:53
  • you can ofcourse use getServletContext().getRealPath("templates/myfile") provided templates is a directory under web-content – SparkOn Aug 14 '14 at 18:57
  • I tried using it but it gave a java.nio.NoSuchFileException!! – Harry S Aug 14 '14 at 19:01
  • How about File.dest()? Should I include the "/" there as well? I need to bundle the dest file in the app itself as I need it afterwards. – Harry S Aug 15 '14 at 17:58
  • :Also, what are the instructions to make the war explode? thanks – Harry S Aug 15 '14 at 18:00
  • refer [this](http://www.xinotes.net/notes/note/689/) `unpackWARs="true"` explodes the war – SparkOn Aug 15 '14 at 18:23
  • Is there anything else I need to take care of? Thank you very much – Harry S Aug 15 '14 at 18:35