File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes servlet initializatoin parameter to get relative path and file name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet initializatoin parameter to get relative path and file name" Watch "servlet initializatoin parameter to get relative path and file name" New topic
Author

servlet initializatoin parameter to get relative path and file name

Bob Dunn
Greenhorn

Joined: Jun 23, 2011
Posts: 12
I'm trying to change the hardcoded file name in the servlet to a parameter contained in the web.xml file. I'm getting a 500 error web page. The first line of the error is:

java.io.FileNotFoundException: \WEB-INF\EmailList.txt (The system cannot find the path specified)

which is interesting because that is the file. I've verified it is there and it works before i changed the getRealPath to ServletConfig().getInitParameter()

Listed below is the code for the servlet, any help is appreciated.

package email;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import business.User;
import data.UserIO;

public class AddToEmailListServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// get parameters from the request
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String emailAddress = request.getParameter("emailAddress");

// use regular Java classes
User user = new User(firstName, lastName, emailAddress);

String message = "";
String url = "";
if (firstName.length() == 0 ||
lastName.length() == 0 ||
emailAddress.length() == 0)
{
message = "Please fill out all three text boxes.";
url = "/join_email_list.jsp";
}
else
{
message = "";

// ServletContext context = this.getServletContext();
// String path = context.getInitParameter("relativePathToFile");

// String path = context.getRealPath("/WEB-INF/EmailList.txt");
// ServletConfig config = this.getServletConfig();

String relativePath = getServletConfig().getInitParameter("relativePathToFile");

UserIO.addRecord(user,relativePath);

url = "/display_email_entry.jsp";
}


// store the User object in the request object
request.setAttribute("user", user);
request.setAttribute("message",message);
// forward request and response objects to JSP page
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
}

web.xml file

<servlet>
<servlet-name>AddToEmailListServlet</servlet-name>
<servlet-class>email.AddToEmailListServlet</servlet-class>
<init-param>
<param-name>relativePathToFile</param-name>
<param-value>/WEB-INF/EmailList.txt</param-value>
</init-param>
</servlet>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

Please UseCodeTags. More people will read formatted code than screenfuls of unformatted text.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

I suspect the problem is that \WEB-INF\EmailList.txt is being looked for at the root of the file system, not your app context.
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 512
Hi,
Can you please show us how you are accessing the file?
I think this lies within the function "UserIO.addRecord(user,relativePath); "

Regards,
Amit

Bob Dunn
Greenhorn

Joined: Jun 23, 2011
Posts: 12
I created the directory /WEB-INF/ at the root and copied the file there and it worked. Not sure why that changed since I think the change was to use a variable for the file name rathe than it being hard coded but at least it works.

I'm not sure how to show the code how the file is being accessed in the UserIO since it's being imported.

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

When you use getRealPath the path is relative to the context root. Without it the path is relative to the file system root,
Manjula Weerasinghe
Greenhorn

Joined: Mar 15, 2010
Posts: 25

Hi Bob,
You have said that this code worked fine when you used

.
After executing

,
value of the relativePath = "/WEB-INF/EmailList.txt".

Use above two facts and try again.

Thanks & Regards,
Manjula



OCPJP 6, OCE JEE 6 JSP and Servlet Developer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet initializatoin parameter to get relative path and file name