0

I want to display an autor first name and last name using Java beans in my Java EE web application but for some reasons i ignore, nothing show up in my index.jsp web page when i write <p>Bonjour ${ auteur.prenom } ${ auteur.nom }</p> (Only Bonjour and then nothing). I made sure everything in web.xml was fine but i really can't find the reason of my little problem. I thought maybe something is wrong with the path in this line this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);

thank you.

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: vorolf
  Date: 2020-09-10
  Time: 4:49 p.m.
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
Hello, World!

<a href="myServlet">Appuyez ici pour avoir l’heure</a>

<br>

<p>Bonjour ${ auteur.prenom } ${ auteur.nom }</p>
<p>${ auteur.actif ? 'Vous êtes très actif !' : 'Vous êtes inactif !' }</p>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_4_0.xsd"
         version="4.0">

    <display-name>my app</display-name>

    <description>
        description test
    </description>

    <servlet>
        <servlet-name>myServlet</servlet-name>
        <servlet-class>myServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>Test</servlet-name>
        <servlet-class>Test</servlet-class>
    </servlet>

<!--mapping-->

    <servlet-mapping>
        <servlet-name>Test</servlet-name>
        <url-pattern>/Test</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>myServlet</servlet-name>
        <url-pattern>/myServlet</url-pattern>
    </servlet-mapping>


</web-app>

Test.java servlet

import beans.Auteur;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



@WebServlet("/Test")
public class Test extends HttpServlet {
    private static final long serialVersionUID = 1L;


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Auteur auteur = new Auteur();
        auteur.setPrenom("Mathieu");
        auteur.setNom("Nebra");
        auteur.setActif(true);

        request.setAttribute("auteur", auteur);

        this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
    }


}

my bean

package beans;


import java.io.Serializable;

public class Auteur implements Serializable {
    private String nom;
    private String prenom;
    private boolean actif;

    public Auteur(){

    }

    public String getNom() {
        return nom;
    }
    public void setNom(String nom) {
        this.nom = nom;
    }
    public String getPrenom() {
        return prenom;
    }
    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }
    public boolean isActif() {
        return actif;
    }
    public void setActif(boolean actif) {
        this.actif = actif;
    }
}

My project structure

enter image description here

The URL of my website http://localhost:8080/demo/ and the generated output of the web page is

<html>
<head>
    <title>Title</title>
</head>
<body>
Hello, World!

<a href="myServlet">Appuyez ici pour avoir l’heure</a>

<br>

<p>Bonjour  </p>
<p>Vous êtes inactif !</p>
</body>
</html>

Lynn
  • 121
  • 8
  • 25
  • 1) what's URL as you see in browser's address bar? 2) what's the generated HTML output as you see via "view page source" in webbrowser? Your answer is in one of those places. – BalusC Sep 22 '20 at 22:51
  • @BalusC I updated my post and responded to 1) and 2). I still can't determine the problem, it's the first time I use Java EE – Lynn Sep 22 '20 at 22:57
  • 1) that's not the URL of the desired servlet. 2) EL is working perfectly fine (the `${` and `}` are correctly absent in the generated HTML output). So, your problem is just that you used the wrong URL to invoke the desired servlet. Probably this is helpful to get started the right way: https://stackoverflow.com/q/2370960/ – BalusC Sep 22 '20 at 22:58
  • @BalusC oh okay I understand, but what should be the path of my servlet if i want to display Bonjour... in my index.jsp and not in Test.jsp? Should it be only "/"? – Lynn Sep 22 '20 at 23:08
  • Not in JSP. In browser's address bar. See link in previous comment for detailed instructions. Advice: put JSP in WEB-INF folder to prevent users from being able to open it without invoking the servlet. – BalusC Sep 22 '20 at 23:08
  • @BalusC No i think I understood what you did, your servlet had the url "/hello" and then you opened http://localhost:8080/contextpath/hello. What i want to do is to stay in http://localhost:8080/demo/ and not go to http://localhost:8080/demo/Test – Lynn Sep 22 '20 at 23:12
  • That's answered by https://stackoverflow.com/q/33248473 – BalusC Sep 22 '20 at 23:15
  • @BalusC works finee thx – Lynn Sep 22 '20 at 23:17
  • @BalusC if you could write a little answer so I can accept it ^^ – Lynn Sep 23 '20 at 17:08
  • Your question is a duplicate of https://stackoverflow.com/q/33248473 – BalusC Sep 23 '20 at 22:03

0 Answers0