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!