Author | Message |
---|---|
hhuang30
Posts: 40
|
Posted 14:02 Feb 05, 2014 |
Hi, I am playing Course Planner by using JSP. And I created 2 normal java classes: CourseBean class to hold list of CourseEntry objects. And I put reading file to list<CouseEntry> part in the constructor of CourseBean. At the same time I am using CoursePlanner.jsp to do display work. My question is, when I am trying to run CoursePlanner.jsp on the server, it always shows FileNotFoundException. BTW, the implement file(undergra_courses.txt) is in the WEB-INF directory, and I am using File file = new File( "/WEB-INF/undergrad_courses.txt" ); //this segment is in the constructor of CourseBean class. So how to deal with it? Thank you in advance. Hui
|
cysun
Posts: 2935
|
Posted 15:45 Feb 05, 2014 |
Because you don't have access to ServletContext in a bean, you can't use getRealPath() to convert a relative path to an absolute path, so you have to specify the absolute path for the file. On Windows it'd be something like new File("C:/CS320/courses.txt") (assuming the file is under a folder CS320 on the C drive), and on CS3 (which runs Linux), if you upload the file to the home directory of your account, it'd be something like new File("/home/cs320stu31/courses.txt"). Note that you have to change the path in the code before deploying it on CS3. |
hhuang30
Posts: 40
|
Posted 16:02 Feb 05, 2014 |
it works, thank you! |
hhuang30
Posts: 40
|
Posted 11:36 Feb 07, 2014 |
1. have been struggling with a parse question for a while, If array (e.g. String[]) is got from param expression, how to parse it to a list for the further use in JSP, can we use JSTL or EL? I tried fn library, but couldn't find solution 2. Another quick question, in JSP we can't declare a new variable, right? We can only use <c:set var="" value="" scope=""> to "assign" to a variable? Thank you so much! |
cysun
Posts: 2935
|
Posted 11:56 Feb 07, 2014 |
1. I'm not sure if I understand the question. I assume you mean using the EL implicit object paramValues to get all the values of a request parameter (like course prerequisites in the homework). You can use <c:forEach> to iterate through them, e.g.
2. You cannot use EL to create new variables, but you can use <c:set> to create one. |
hhuang30
Posts: 40
|
Posted 12:11 Feb 07, 2014 |
1. Right, that's my question, but I want to form a list with these String by using iteration. So I need a list type variable, right? Then that probably go to my second question 2 2. Is that possible to declare a new type of variable in JSP, like a list or an arraylist? I know, by using <c:set var ="" value="" scope="">,we can create a variable in JSP, e.g. to create a list. However, we need value="${List}" to "assign" this list type list to var here. And by using <c:set target ="" property="" value="">, we can set the value to a property in target. So is that possible to create "total new " variable in JSP? Sorry if my expression confused you, and thanks in advance~ |
cysun
Posts: 2935
|
Posted 12:24 Feb 07, 2014 |
Why do you want to create a List? In EL and JSTL, List<String> and String[] are handled in exactly the same way. Normally the advantage of List over array is that you can add new elements to a list, but you can't do that in JSP (with EL & JSTL) anyway so lists and arrays are the same. I think maybe you are trying to do something like set prereq into a Course bean. You can do that with <jsp:setProperty>, but the prereq property must be an array, not List. And also remember that EL & JSTL are not use for complex programming - there are lots thing they cannot do, and that's by design, because otherwise it becomes scripting elements again. After the MVC lecture next week it should become more clear. |
hhuang30
Posts: 40
|
Posted 12:27 Feb 07, 2014 |
Got u, thank you! |