• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

servlet initializatoin parameter to get relative path and file name

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Marshal
Posts: 67478
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags. More people will read formatted code than screenfuls of unformatted text.
 
Bear Bibeault
Marshal
Posts: 67478
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 67478
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use getRealPath the path is relative to the context root. Without it the path is relative to the file system root,
 
Greenhorn
Posts: 25
Oracle PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Is this the real life? Is this just fantasy? Is this a tiny ad?
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic