0

I'm trying to pass the attribute from a servlet to JSP. I want to show Hello World in the JSP. It always returns null. It never gets the attribute x which says how many times hello world should be shown. I don't know what I'm doing wrong. This is my code in the servlet.

   public class HelloIbaServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int x = 1;
        if (req.getParameter("x") != null) {
            try {
                x = Integer.parseInt(req.getParameter("x"));
            } catch (NumberFormatException ex) {
                x = 1;
            }
        }
        req.setAttribute("x", x);
        req.getRequestDispatcher("/index.jsp").forward(req, resp);
    }

}

This is my JSP.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="com.mycompany.servlets.HelloIbaServlet"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>

    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        </head>
        <body>
            <c:forEach begin="1" end="${x}">
                <h1>Hello world!</h1>
            </c:forEach>
        </body>
    </html>

My dependencies are

<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>  
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
        </dependency>

My web.xml

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <servlet>
        <servlet-name>HelloIbaServlet</servlet-name>
        <servlet-class>com.mycompany.servlets.HelloIbaServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloIbaServlet</servlet-name>
        <url-pattern>/sayhello</url-pattern>
    </servlet-mapping>
</web-app>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user3637775
  • 499
  • 4
  • 20
  • Your question is ambiguous and you're not describing the problem as if you were the web developer. Rightclick page in browser and do *View Page Source* to check the JSP-generated HTML output. What exactly do you see there? Do you see `` there or not? Also try to print only `${x}` instead of ``. What do you then see in the generated HTML output? By the way, the JSP API is like Servlet API usually already provided by the servletcontainer itself. Providing it anyway via the webapp will only result in duplicate APIs conflicting in runtime classpath. Don't do that. – BalusC Jun 22 '16 at 07:12
  • I can see a blank page. I am new to Java EE. What should I change? I don't really understand your response. – user3637775 Jun 22 '16 at 07:20
  • You should look at the problem as a web developer, not as a web enduser. "I see a blank page" is the enduser perspective, not the web developer perspective. As said, rightclick page in browser and choose *View Page Source* option to check the JSP-generated HTML output (or press Ctrl+U in Google Chrome). What exactly do you see there? – BalusC Jun 22 '16 at 07:28
  • Just basic structure of HTML. No mention of forEach – user3637775 Jun 22 '16 at 07:33
  • 1
    Okay, JSP and JSTL work fine. Now we can move the focus to the servlet. Look at your browser's address bar. What is the URL in the browser's address bar? Is it the URL of the servlet or the JSP file itself? If it is the URL of the JSP file itself, how exactly did you expect the servlet's `doGet()` to be invoked? Is it actually invoked after all? (i.e. a debug breakpoint will hit it and a `System.out.println("blah")` will print to server log). – BalusC Jun 22 '16 at 07:35
  • The URL is http://localhost:8080/my-app?x=5. I've specified servlet-mapping in the web.xml like /. Should it work? – user3637775 Jun 22 '16 at 07:50
  • [The `/` is not the correct mapping](http://stackoverflow.com/q/33248473). By the way, you have still not answered what happens when you print only EL `${x}` instead of JSTL ``. If you're seeing `${x}` in generated HTML output, then it means your web.xml is wrong. If you're seeing an empty string in HTML output, then it means servlet wasn't invoked. If you're seeing `1` (or whatever passed-in number), then it means a conflict between libraries in runtime classpath causing JSTL to fail finding it. – BalusC Jun 22 '16 at 07:51
  • I see an empty string in HTML output. – user3637775 Jun 22 '16 at 07:57
  • I've added web.xml above and changed the url pattern. – user3637775 Jun 22 '16 at 08:00
  • Okay, EL works fine. Well, clearly servlet isn't invoked. Your web.xml also looks like it from 00's. Make sure your learning resources are no older than 1~3 years. Which is most helpful in order to solve your problem? http://stackoverflow.com/q/33248473 or http://stackoverflow.com/q/11731377 or http://stackoverflow.com/q/3590961? Don't forget to move the JSP file into `/WEB-INF` folder to prevent it from being accessed directly without invoking the servlet. – BalusC Jun 22 '16 at 08:03
  • When I've changed url pattern to/sayhello/*. It worked but without parameter. Otherwise it failed - The server encountered an internal error that prevented it from fulfilling this request. – user3637775 Jun 22 '16 at 08:08
  • Good, your problem is solved. As to your new problem, in case of exceptions the answer us usually straight in the root cause (in case you can't interpret it, just copypaste the type+message in Google). – BalusC Jun 22 '16 at 08:11
  • When I've moved the index.jsp into /WEB-INF. It isn't working at all :D – user3637775 Jun 22 '16 at 08:13
  • Of course you have to alter the JSP file path in `getRequestDispatcher()` accordingly. See also the duplicate to get started the right way. – BalusC Jun 22 '16 at 08:14
  • Sorry I'm lost. In the web.xml I specify /sayhello/*. Then in the servlet req.getRequestDispatcher("WEB-INF/index.jsp").forward(req, resp); I will run http://localhost:8080/my-app/sayhello right? – user3637775 Jun 22 '16 at 08:19
  • The `getRequestDispatcher()` path must always start with `/`. Otherwise it will be relative to the current request URL. Your JSP file surely isn't in the `/sayhello/WEB-INF/index.jsp` place. – BalusC Jun 22 '16 at 08:27
  • If I understand right I have two options how to map the servlet. First option is to define in the web.xml and second one through the webserver annocation? – user3637775 Jun 22 '16 at 08:40
  • That's correct. See also http://stackoverflow.com/q/6535676 (which is also linked in one of my previous links). It appears that you're using bad learning resources. I recommend you to throw away your current learning resource and get started at our Servlets wiki page: http://stackoverflow.com/tags/servlets/info – BalusC Jun 22 '16 at 08:41
  • What do you think its better to use annocation or web.xml? I'm really stuck. It's some stupid mistake. – user3637775 Jun 22 '16 at 09:01
  • 1
    It's best to keep moving on and use latest technologies where possible. – BalusC Jun 22 '16 at 09:02
  • I can't stil solve it :( – user3637775 Jun 22 '16 at 09:29

0 Answers0