0

So, I am just learning JSP and Java-EE. I made this test program but the EL is not showing any output.

This is my JSP Code:

<%@page contentType="text/html" pageEncoding="UTF-8" import="com.test.TestServlet" isELIgnored="false"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Test JSP</h1>
        ${person.name}'s dog ${person.dog.name}'s toys are: ${person.dog.toys[0].name}, ${person.dog.toys[1].name} and ${person.dog.toys[2].name}
    </body>
</html>

Servlet code:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Person p = new Person();
        p.setName("Leelu");
        Dog d = new Dog();
        d.setName("Clyde");
        Toy t1 = new Toy();
        t1.setName("Stick");
        Toy t2 = new Toy();
        t2.setName("neighbour's cat");
        Toy t3 = new Toy();
        t3.setName("Barbie doll head");

        d.setToys(new Toy[]{t1,t2,t3});
        p.setDog(d);
        request.setAttribute("person", p);


        RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/testJSP.jsp");
        view.forward(request, response);
}

web.xml Code:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>TestServlet</servlet-name>
        <servlet-class>com.test.TestServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/TestServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

I am using Netbeans 8.2 IDE with Glassfish 4.1.1. What am I doing wrong here ?

EDIT: Added web.xml code.

  • What's the URL displayed in your browser address bar? – JB Nizet Jan 07 '17 at 17:25
  • And how does the tag of web.xml look like? – BalusC Jan 07 '17 at 17:28
  • @JBNizet http://localhost:8080/WebApplication5/testJSP.jsp – Aman Singh Jan 07 '17 at 17:29
  • 1
    So you opened the JSP without invoking the servlet and you expected the servlet's data to be displayed in JSP somehow? That's not how stuff works. Move `doPost()` code into `doGet()` and put JSP in `/WEB-INF` folder so you can never invoke it without invoking the servlet. The abovelinked duplicate shows several correct kickoff examples. – BalusC Jan 07 '17 at 17:29
  • @BalusC I moved the code into `doGet()` and the JSP was already in `/WEB-INF` directory, but I am still not getting any output – Aman Singh Jan 07 '17 at 17:43
  • 1
    There is no way your JSP is under WEB-INF. If it was, you wouldn't be able to see it from your browser. `getRequestDispatcher("testJSP.jsp");` should be changed to `getRequestDispatcher("/WEB-INF/testJSP.jsp");` (assuming the JSP is indeed directly under WEB-INF. – JB Nizet Jan 07 '17 at 17:49
  • Just put all of this aside and follow examples in the duplicate to get started correctly. – BalusC Jan 07 '17 at 17:55
  • @JBNizet Sorry, my bad. The JSP was not in `WEB-INF` directory. After moving it and changing the url in Servlet as mentioned by @BalusC this is the url I'm getting: http://localhost:8080/WebApplication5/testJSP and it says `The requested resource is not available.` – Aman Singh Jan 07 '17 at 18:26
  • The mapping for your servlet is /TestServlet, not /testJSP. – JB Nizet Jan 07 '17 at 18:33
  • @JBNizet It worked. Thanks man, you really saved me today – Aman Singh Jan 07 '17 at 18:38
  • @BalusC Thanks to you too man :D – Aman Singh Jan 07 '17 at 18:38

0 Answers0