Page 1 of 2 12 LastLast
Results 1 to 20 of 26

Thread: new at JavaEE

  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default new at JavaEE

    I am starting up a small servlet example called ebookshop taken from the
    beginning jsp jsf and tomact manual. All classes are compiled correctly and
    all code is directed into the right folders etc.

    I am getting "eboolshop the requested resource () is not available" error!

    I believe the problems is at the web.xml (which I do not fully understand).

    In order to get a better understanding of this stuff I need the code to run correctly,
    so I can add stuff like println etc to see what it is doing (I do understand all of the Java code
    and most of the HTML stuff). See pls the next code block!
    -The part I do not understand is superior to <display-name>.
    -what is xsi:schemaLocation=~CCC
    -I believe that line forms the problem, right?

    see: http://books.google.es/books?id=LosB...okshop&f=false

    Java Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation=~CCC
    "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"version="2.4">
    
    <display-name>Electronic Bookshop</display-name>
    
    <description>
    e-bookshop example for Beginning JSP, JSF and Tomcat: from Novice to Professional
    </description>
    
    <servlet>
    <servlet-name>EBookshopServlet</servlet-name>
    <servlet-class>ebookshop.ShoppingServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
    <servlet-name>EBookshopServlet</servlet-name>
    <url-pattern>/eshop</url-pattern>
    </servlet-mapping>
    
    </web-app>
    Some additional info:
    1) inside webapps (tomcat 6) I created the folder bookshop;
    2) inside you'll find the two jsp file: index.jsp and Checkout.jsp
    3) and the two folders: META-INF and WEB-INF
    4) inside WEB-INF you'll classes/ebookshop
    5) in classes/ebookshop you'll find the two compiled classes:
    Book.class and ShoppingServlet.class
    6) at META-INF you'll find the web.xml
    Last edited by willemjav; 04-02-2014 at 04:09 PM.

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,675
    Rep Power
    5

    Default Re: new at JavaEE

    Don't assume, post everything.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: new at JavaEE

    "Don't assume, post everything."

    two jsp and two classes? ok here are the jsp:
    index.jsp
    Java Code:
    <%@page language="java" contentType="text/html"%>  
    <%@page trimDirectiveWhitespaces="true"%>  
    <%@page errorPage="stack_trace.jsp"%> 
    <%@page session="true" import="java.util.Vector, ebookshop.Book"%>  
    <html>  
    <head>  
    <title>E-bookshop</title>  
    <style type="text/css">  
        body {background-color:gray; font-size=10pt;}  
        H1 {font-size:20pt;}  
        table {background-color:white;}  
    </style>  
    </head>  
    <body>  
        <H1>Your online Bookshop</H1>  
        <hr/><p/>  
        <% // Scriptlet 1: check whether the booklist is ready  
            Vector<ebookshop.Book> booklist =  
            (Vector<ebookshop.Book>)session.getValue("ebookshop.list");  
            if (booklist == null) {  
                response.sendRedirect("/eshop");  
            }  
            else {  
                %>  
                <form name="addForm" action="eshop" method="POST">  
                    <input type="hidden" name="do_this" value="add">  
                    Book:  
                    <select name=book>  
                    <% // Scriptlet 2: copy the booklist to the selection control 
                        for (int i = 0; i < booklist.size(); i++) {  
                            out.println("<option>" + booklist.elementAt(i) + "</option>");  
                        }  
                    %>  
                    </select>  
                    Quantity: <input type="text" name="qty" size="3" value="1">  
                    <input type="submit" value="Add to Cart">  
                </form>  
                <p/>  
                <% // Scriptlet 3: check whether the shopping cart is empty
                Vector shoplist =  
                (Vector<ebookshop.Book>)session.getAttribute("ebookshop.cart");  
                if (shoplist != null && shoplist.size() > 0) {  
                    %>  
                    <table border="1" cellpadding="2">  
                        <tr>  
                            <td>TITLE</td>  
                            <td>PRICE</td>  
                            <td>QUANTITY</td>  
                            <td></td>  
                        </tr>  
                        <% // Scriptlet 4: display the books in the shopping cart
                            for (int i = 0; i < shoplist.size(); i++) {  
                                Book aBook = (Book)shoplist.elementAt(i);  
                        %>  
                                <tr>  
                                    <form name="removeForm" action="eshop" method="POST">  
                                        <input type="hidden" name="position" value="<%=i%>">  
                                        <input type="hidden" name="do_this" value="remove">  
                                        <td><%=aBook.getTitle()%></td>  
                                        <td align="right">$<%=aBook.getPrice()%></td>  
                                        <td align="right"><%=aBook.getQuantity()%></td>  
                                        <td><input type="submit" value="Remove from Cart"></td>  
                                    </form>  
                                </tr>  
                        <%  
                            } // for (int i..  
                        %>  
                    </table>  
                    <p/>  
                    <form name="checkoutForm" action="eshop" method="POST">  
                        <input type="hidden" name="do_this" value="checkout">  
                        <input type="submit" value="Checkout">  
                    </form>  
                    <%  
                } // if (shoplist..  
            } // if (booklist..else..  
        %>  
    </body>  
    </html>


    Checkout.jsp
    Java Code:
    <%@page language="java" contentType="text/html"%>  
    <%@page session="true" import="java.util.Vector, ebookshop.Book" %>  
    <html>  
    <head>  
    <title>E-Bookshop Checkout</title>  
    <style type="text/css">  
        body {background-color:gray; font-size=10pt;}  
        H1 {font-size:20pt;}  
        table {background-color:white;}  
    </style>  
    </head>  
    <body>  
        <H1>Your online Bookshop - Checkout</H1>  
        <hr/><p/>  
        <table border="1" cellpadding="2">  
            <tr>  
                <td>TITLE</td>  
                <td align="right">PRICE</td>  
                <td align="right">QUANTITY</td>  
            </tr>  
            <%  
                Vector<Book> shoplist =  
                (Vector<Book>)session.getAttribute("ebookshop.cart");  
                for (Book anOrder : shoplist) {  
            %>  
            <tr>  
                <td><%=anOrder.getTitle()%></td>  
                <td align="right">$<%=anOrder.getPrice()%></td>  
                <td align="right"><%=anOrder.getQuantity()%></td>  
            </tr>  
            <%  
                }  
                session.invalidate();  
            %>  
            <tr>  
                <td>TOTALS</td>  
                <td align="right">$<%=(String)request.getAttribute("dollars")%></td>  
                <td align="right"><%=(String)request.getAttribute("books")%></td>  
            </tr>  
        </table>  
        <p/>  
        <a href="/ebookshop/eshop">Buy more!</a>  
    </body>  
    </html>
    Last edited by willemjav; 04-02-2014 at 04:16 PM.

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: new at JavaEE

    The two classes:

    Java Code:
    package ebookshop;  
    
    import java.util.Vector;  
    import java.io.IOException;  
    import javax.servlet.ServletException;  
    import javax.servlet.ServletConfig;  
    import javax.servlet.ServletContext;  
    import javax.servlet.RequestDispatcher;  
    import javax.servlet.http.HttpServlet;  
    import javax.servlet.http.HttpServletRequest;  
    import javax.servlet.http.HttpSession;  
    import javax.servlet.http.HttpServletResponse;  
    import ebookshop.Book;  
    
    public class ShoppingServlet extends HttpServlet {  
        public void init(ServletConfig conf) throws ServletException {  
            super.init(conf);  
        }  
        public void doGet (HttpServletRequest req, HttpServletResponse res)  
            throws ServletException, IOException {  
                doPost(req, res);  
            }  
        public void doPost (HttpServletRequest req, HttpServletResponse res)  
            throws ServletException, IOException {  
                HttpSession session = req.getSession(true);  
                @SuppressWarnings("unchecked")  
                Vector<Book> shoplist = (Vector<Book>)session.getAttribute("ebookshop.cart");  
                String do_this = req.getParameter("do_this");  
                // If it is the first time, initialize the list of books, which in  
                // real life would be stored in a database on disk  
                if (do_this == null) {  
                    Vector<String> blist = new Vector<String>();  
                    blist.addElement("Learn HTML5 and JavaScript for iOS. Scott Preston $39.99");  
                    blist.addElement("Java 7 for Absolute Beginners. Jay Bryant $39.99");  
                    blist.addElement("Beginning Android 4. Livingston $39.99");  
                    blist.addElement("Pro Spatial with SQL Server 2012. Alastair Aitchison $59.99");  
                    blist.addElement("Beginning Database Design. Clare Churcher $34.99");  
                    session.setAttribute("ebookshop.list", blist);  
                    ServletContext sc = getServletContext();  
                    RequestDispatcher rd = sc.getRequestDispatcher("/");  
                    rd.forward(req, res);  
                }  
                else {  
                    // If it is not the first request, it can only be a checkout request  
                    // or a request to manipulate the list of books being ordered    
                    if (do_this.equals("checkout")) {  
                        float dollars = 0;  
                        int books = 0;  
                        for (Book aBook : shoplist) {  
                            float price = aBook.getPrice();  
                            int qty = aBook.getQuantity();  
                            dollars += price * qty;  
                            books += qty;  
                        }  
                        req.setAttribute("dollars", new Float(dollars).toString());  
                        req.setAttribute("books", new Integer(books).toString());  
                        ServletContext sc = getServletContext();  
                        RequestDispatcher rd = sc.getRequestDispatcher("/Checkout.jsp");  
                        rd.forward(req, res);  
                    } // if (..checkout..  
                    // Not a checkout request - Manipulate the list of books  
                    else {  
                        if (do_this.equals("remove")) {  
                            String pos = req.getParameter("position");  
                            shoplist.removeElementAt((new Integer(pos)).intValue());  
                        }  
                        else if (do_this.equals("add")) {
                            boolean found = false;  
                            Book aBook = getBook(req);  
                            if (shoplist == null) { // the shopping cart is empty  
                                shoplist = new Vector<Book>();  
                                shoplist.addElement(aBook);  
                            }  
                            else { // update the #copies if the book is already there  
                                for (int i = 0; i < shoplist.size() && !found; i++) {  
                                    Book b = (Book)shoplist.elementAt(i);  
                                    if (b.getTitle().equals(aBook.getTitle())) {  
                                        b.setQuantity(b.getQuantity() + aBook.getQuantity());  
                                        shoplist.setElementAt(b, i);  
                                        found = true;  
                                    }  
                                } // for (i..  
                                if (!found) { // if it is a new book => Add it to the shoplist  
                                    shoplist.addElement(aBook);  
                                }  
                            } // if (shoplist == null) .. else ..  
                        } // if (..add..  
                        // Save the updated list of books and return to the home page
                        session.setAttribute("ebookshop.cart", shoplist);  
                        ServletContext sc = getServletContext();  
                        RequestDispatcher rd = sc.getRequestDispatcher("/");  
                        rd.forward(req, res);  
                    } // if (..checkout..else  
                } // if (do_this..  
            } // doPost  
        private Book getBook(HttpServletRequest req) {  
            String myBook = req.getParameter("book");  
            int n = myBook.indexOf('$');  
            String title = myBook.substring(0, n);  
            String price = myBook.substring(n+1);  
            String qty = req.getParameter("qty");  
            return new Book(title, Float.parseFloat(price), Integer.parseInt(qty));  
        } // getBook  
    }



    Java Code:
    package ebookshop;  
    public class Book {  
        String title;  
        float price;  
        int quantity;  
        public Book(String t, float p, int q) {  
            title = t;  
            price = p;  
            quantity = q;  
        }  
        public String getTitle() { return title; }  
        public void setTitle(String t) { title = t; }  
        public float getPrice() { return price; }  
        public void setPrice(float p) { price = p; }  
        public int getQuantity() { return quantity; }  
        public void setQuantity(int q) { quantity = q; }  
    }

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,675
    Rep Power
    5

    Default Re: new at JavaEE

    Java Code:
    RequestDispatcher rd = sc.getRequestDispatcher("/");
    You are sending the user to the root of your webserver, yet your application is deployed under '/ebookshop'. Hence the error that there is no resource under "/": that is correct, you didn't deploy anything there.

    Now you might go two ways at this point:

    a) (wrong) change the forward to '/ebookshop'
    b) (correct) wonder to yourself: perhaps I should think of a way so that I don't have to hardcode that my application is deployed under '/ebookshop'

    A very quick fix to fulfill b) is to actually deploy your web application as the root web application.
    Last edited by gimbal2; 04-02-2014 at 04:23 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: new at JavaEE

    Wow you fund that very fast!
    So what I do?
    the ebookshops sits indeed at the webapps direction of the server!

    I followed the direction at page 21: http://www.academia.edu/5086197/this...S_JAVA_ROADMAP

  7. #7
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: new at JavaEE

    change "/" into "/ebookshop"?

    have to compile and come back to you.....

  8. #8
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: new at JavaEE

    changed that line (40) into "RequestDispatcher rd = sc.getRequestDispatcher("/ebookshop"); "
    and still getting the same error...

    what is: xsi:schemaLocation=~CCC (post 1 line 5)
    is that correct?
    Last edited by willemjav; 04-02-2014 at 04:45 PM.

  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,675
    Rep Power
    5

    Default Re: new at JavaEE

    Yes but I've now given you a hint what to look for, so you can now do some further investigating yourself. I spot this line of code too for example:

    Java Code:
     response.sendRedirect("/eshop");
    O RLY? /eshop exists on your server? I don't think so, its a big mess and you need to sweep through it with a broom to find all the flaws.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: new at JavaEE

    "A very quick fix to fulfill b) is to actually deploy your web application as the root web application."

    I'll try that one first (but it is weird that I am following the direction of the book.... and got in trouble,,,,)

  11. #11
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,675
    Rep Power
    5

    Default Re: new at JavaEE

    Following but not understanding usually leads to usage of the word "weird". I warn you: when you do that (use words such as "weird", "strange", "odd" or "is it a bug in XXX?") I become very evil, I would advise you to not mention such words when I'm around ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: new at JavaEE

    So I took WEB-INF, META.INF, index.jsp and Checkout.jsp out of the direction ebookshop and copied it
    to:
    1) webapps
    2) (next) webapps/ROOT
    and still the same error!

  13. #13
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: new at JavaEE

    Ok, ok I won't even use "bananas" (one of my favorites)!
    It is not "fair" the code is not mine it is from the book and it should work, right!
    So we have a classical egg and chicken situation:
    1) in order to begin to understand this stuff I need it running;
    2) and in order to get it running I need to understand it?
    what you say now?

  14. #14
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,800
    Rep Power
    19

    Default Re: new at JavaEE

    Have you set up a web app before?
    If so then do that (even a simple HelloWorld servlet).
    Once that's working, then and only then bolt these extra bits on.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: new at JavaEE

    Yes tolls I got through some jsp hello word stuff (about a 10/ 15 small applications) etc.
    I ran them all from the dir "tests" at the ROOT, here a small example:
    and they all worked just fine!

    Java Code:
    req_<%@page language="java" contentType="text/html"%> 
    <%@page import="java.util.*, java.io.*"%>
    <% 
    int len = request.getContentLength(); 
    byte[] buf = null; 
    int	n = 0; 
    if (len > 0) {
    	buf = new byte[len]; 
    	n = request.getInputStream().read(buf); 
    }
    %>
    <html><head><title>Test request.getInputStream</title></head><body>
    <form action="" method="post" enctype="multipart/form-data"> 
    <input type="hidden" name="oneTwoThree" value="123"/> 
    <input type="file" name="fil"/> 
    <input type="submit"/>
    </form>
    <table border="1">
    <tr><td>getContentType()</td><td><%=request.getContentType()%></td></tr> 
    <tr><td>getContentLength()</td><td><%=len%></td></tr>
    <%
    	out.print("<tr><td>getInputStream(): " + n + "</td><td><pre>");
    	for (int k = 0; k < n; k++) out.print((char)buf[k]); 
    	out.println("</pre></td></tr>");
    %>
    </table>
    </body></html>

  16. #16
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: new at JavaEE

    If you guys give me a quick fix here, I can start studying this stuff!
    But when there are errors I do not know where to begin.
    Maybe the leap from those jps app into a small but full functioning servlet is too big!
    You see I was reading along in that book and everything developed slow and gradually,
    all very well and carefully explained and than suddenly this!
    And on top of it gimball2 gets mad (again).....
    You know I am not the only one (haha):
    GlassFish Users - 404 Resource not found error
    and yet another one:
    http://stackoverflow.com/questions/9...-not-available

    What is he talking about:
    "Thanks JB Nizet. I found the problem. There was some extra characters in my web.xml file. you can see that. "xsi:schemaLocation=~CCC". "~CCC" was making the problem. – " at the last link bottom page!
    Last edited by willemjav; 04-02-2014 at 06:07 PM.

  17. #17
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: new at JavaEE

    You see I still have confidence in the human kind, so when a guy writes a book and explains this stuff really well
    and suddenly pops up with his ebookshop thing, I believe him even when I also see that eshop does not really
    make much since! But still he has thought all this through and there must be some small tiny mistake hare, right?
    It can't be that he (the author of the book) suddenly looses it all.....

  18. #18
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,062
    Rep Power
    8

    Default Re: new at JavaEE

    Ok I'll wait patiently until the oracle speaks next.....
    and for the time being I'll try to encript the little things I got

  19. #19
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,800
    Rep Power
    19

    Default Re: new at JavaEE

    Quote Originally Posted by willemjav View Post
    Yes tolls I got through some jsp hello word stuff
    No, I'm talking about a simple servlet.
    No JSPs.

    If this is one of those books that starts teaching using a JSP then (personally) I would take it outside and put it out of my misery. I could go off into my rant about how starting with JSPs is arse about face, but I'm sure there's enough of those here already.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  20. #20
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,675
    Rep Power
    5

    Default Re: new at JavaEE

    Quote Originally Posted by Tolls View Post
    No, I'm talking about a simple servlet.
    No JSPs.

    If this is one of those books that starts teaching using a JSP then (personally) I would take it outside and put it out of my misery.
    And I'd set the corpse on fire and dance around it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Page 1 of 2 12 LastLast

Similar Threads

  1. Best IDE for JavaEE development.
    By chyrl in forum Advanced Java
    Replies: 7
    Last Post: 01-26-2013, 02:17 PM
  2. Public folder in JavaEE webapp
    By Vash1986 in forum Java Servlet
    Replies: 0
    Last Post: 11-15-2011, 10:44 PM
  3. Replies: 2
    Last Post: 03-10-2011, 09:25 AM
  4. Netbeans 6.9.1 JavaEE Framework
    By chyrl in forum Web Frameworks
    Replies: 0
    Last Post: 11-07-2010, 06:51 PM

Posting Permissions

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