Results 1 to 2 of 2
  1. #1
    rookiecoder is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Servlets/JSP Tomcat Eclipse

    I have an assignment that requires a simple date.txt file to be read, verifies that strings in the txt are actual dates and then display the information in the index.jsp

    The code was simple enough, but I have never worked with servlets, jsps, tomcat, or eclipse...regardless I've torn through tutorials, guides, and youtube videos to get this far, but I'm stuck. I was able to get the dateList to display from the servlet using a PrintWriter, but to follow the assignment and MVC it should be read from the index.jsp file. Currently the error message I am receiving is:

    Java Code:
    org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 17
    
    14: 
    15: ReadDateFile file = (ReadDateFile) request.getAttribute("file"); 
    16: 
    17: LinkedList<String> dateList = file.getDateList();
    18: 
    19: for (String date : dateList) {
    20: 	out.println(date);
    The reader:
    Java Code:
    package dateverify;
    
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.LinkedList;
    import java.util.Scanner;
    
    public class ReadDateFile{ 
    	
    	private LinkedList<String> dateList = new LinkedList<String>();
    
    	public ReadDateFile()throws FileNotFoundException, IOException{
    		
    		String strLine = "";
    		
    	    //Get information from text document
    	    FileInputStream fstream = new FileInputStream("C:/Users/Shadow/Desktop/Eclipse/DateVerifyServlet/dates-2.txt");
    	   
    	    // Get the objects of DataInputStream
    	    DataInputStream in = new DataInputStream(fstream);
    	    BufferedReader br = new BufferedReader(new InputStreamReader(in)); {
    	    
    		    //Read File Line By Line
    		    while ((strLine = br.readLine()) != null)   {
    		
    		        //Input each line into new Scanner to be analyzed by delimiter
    		        Scanner date = new Scanner(strLine);
    		        // Set delimiters to ":", tells Scanner to match as delimiters.
    		        date.useDelimiter(",");
    		
    		        // Read strLine one field at a time.
    		        while (date.hasNext()) {
    		        	
    		        	//Assigns DateFormat to 'yyyy-mm-dd';
    		        	DateFormat dftest = new SimpleDateFormat("yyyy-MM-dd");
    		        	//sets leniency to parsing off so dates can be verified accurately 
    		        	dftest.setLenient(false);
    		        	//Assigns read line from text document to testable string
    		            String dateString = date.next();
    		            
    		            //Ignores lines that begin "#" treating them as comments
    		            if (dateString.startsWith("#")) {
    		            	break;
    		            }
    		            //Removes space after comma
    		            if (dateString.startsWith(" ")) {
    		            	dateString = dateString.substring(1);
    		            }
    		
    		            try {
    		                //Test if dateString can be parsed as date.
    		            	Date dateTest = dftest.parse(dateString);
    		            	Calendar d = Calendar.getInstance();
    		                d.setTime(dateTest);
    		            	dateList.add(dateString +  " ("
    		            			+ getDayOfTheWeek(d.get(Calendar.DAY_OF_WEEK)) + ")");
    		            	
    		            }catch (ParseException e) {
    		                dateList.add(dateString + "<--- Invalid Date");
    		                break;
    		            }
    		        }
    		        //Close scanner stream
    		        date.close();
    		    }        
    	    //Close the input stream
    	    in.close();
    	    }
    	}
    	
    	private String getDayOfTheWeek(int dayOfWeekNumber) {
    		String dayOfTheWeek = "";
    		switch (dayOfWeekNumber) {
    		case 1:
    			dayOfTheWeek = "Sunday";
    			break;
    		case 2:
    			dayOfTheWeek = "Monday";
    			break;
    		case 3:
    			dayOfTheWeek = "Tuesday";
    			break;
    		case 4:
    			dayOfTheWeek = "Wednesday";
    			break;
    		case 5:
    			dayOfTheWeek = "Thursday";
    			break;
    		case 6:
    			dayOfTheWeek = "Friday";
    			break;
    		default:
    			dayOfTheWeek = "Saturday";
    			break;
    		}
    		return dayOfTheWeek;
    	}
    	
    	public LinkedList<String> getDateList() {
    		return dateList;
    	}
    }
    The Servlet
    Java Code:
    package dateverify;
    
    import java.io.IOException;
    
    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 DateVerify
     */
    @WebServlet("/DateVerifyPath")
    public class DateVerify extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    
        /**
         * Default constructor. 
         */
        public DateVerify() {
            // TODO Auto-generated constructor stub
        }
    
    	/**
    	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		
    		ReadDateFile file = new ReadDateFile();
    		request.setAttribute("file", file);
            request.getRequestDispatcher("/index.jsp").forward(request, response);
    
    		
    	}
    
    }
    and the index.jsp
    Java Code:
    <%@page import="dateverify.ReadDateFile, java.util.LinkedList"%>
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    
    <!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=ISO-8859-1">
    <title>DateVerify</title>
    </head>
    <body>
    
    <%
    
    ReadDateFile file = (ReadDateFile) request.getAttribute("file"); 
    
    LinkedList<String> dateList = file.getDateList();
    
    for (String date : dateList) {
    	out.println(date);
    }
    
    %>
    
    </body>
    </html>
    I feel like there is more wrong with my code than I know, but I really I'm not familiar enough to know what...

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: Servlets/JSP Tomcat Eclipse

    To follow MVC then this data should be read in the servlet and turned into a model that is forwarded to the JSP page.
    In this case it would simply be the LinkedList of Strings, which the JSP will then get from the request (addAttribute and getAttribute).
    You should not have code like that in your JSP.

    So that should be your start point.

    The reason it's not compiling is that your ReadFile constructor throws exceptions, which your JSP is not handling.
    Removing the ReadFile from the JSP entirely will actually solve your problem.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Tomcat and servlets beginner question
    By iparry in forum New To Java
    Replies: 2
    Last Post: 03-24-2012, 02:49 AM
  2. Replies: 0
    Last Post: 10-20-2011, 11:10 AM
  3. applet and mysql in jsp using tomcat servlets
    By gedas in forum Java Applets
    Replies: 4
    Last Post: 04-05-2011, 10:41 PM
  4. Ubuntu 10.10 Tomcat 6 & Servlets newbie
    By midnight1111 in forum Java Applets
    Replies: 1
    Last Post: 12-07-2010, 09:59 AM
  5. running servlets in tomcat
    By mipravin in forum Java Servlet
    Replies: 4
    Last Post: 03-18-2009, 12:17 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •