0

My servlet is not workning. Cold someone help me ? you can see thre files her : web.xml, java code, and jsp page. thanks for some help.

web.xml file :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="UTF-8"%>    
<!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>2) Titre : ecrasee par servlet2</title>
</head>
<body> 
<p>2) Par le mappage de cette page jsp et de la servlet MaServlet, il y a ecrasement de ce contenu par a servlet</p>
<p>Pour le vérifier mapper puis démapper par le fichier web.xml sité dans le dossier WebContent</p>
</body>     
</html>

java servlet code :

package test.servletpac;        
import javax.servlet.http.HttpServlet;    
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; 

@WebServlet(name="MaSecondeServlet")
public class SecondeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;       

    public SecondeServlet() { 
        super();         
    }    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*ServletContext sc = this.getServletContext() ;
        RequestDispatcher rd = sc.getRequestDispatcher("/secondejspservlet.jsp") ;
        rd.forward(request, response);  */
        try 
        {               this.getServletContext().getRequestDispatcher("/secondejspservlet.jsp").forward(request, response);
        } 

        catch(ServletException ex) 
        {
            System.out.println("Servlet <SecondeServlet> Erreur \"ServletException\" suivante : " + ex.getMessage());
            System.out.println("Servlet <SecondeServlet> Erreur \"ServletException\" suivante : " + ex.hashCode());
        }
        catch(IOException ioex) 
        {
            System.out.println("Servlet <SecondeServlet> Erreur \"IOException\" suivante : " + ioex.getMessage());
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    
    }    
}

jsp file (simple html): <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="UTF-8"%>

<!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>2) Titre : ecrasee par servlet2</title>
</head>
<body> 
<p>2) Par le mappage de cette page jsp et de la servlet MaServlet, il y a ecrasement de ce contenu par a servlet</p>
<p>Pour le vérifier mapper puis démapper par le fichier web.xml sité dans le dossier WebContent</p>
</body>     
</html>

1 Answers1

1

Your anotation is wrong. It should be

@WebServlet(name = "SecondeServlet", urlPatterns = {"/MaSecondeServlet"})

This is your result. App is named Test

enter image description here Try this code.

package test.servletpac;  

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

import java.io.IOException; 

@WebServlet(name = "SecondeServlet", urlPatterns = {"/MaSecondeServlet"})
public class SecondeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;       


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                    getServletContext().getRequestDispatcher("/secondejspservlet.jsp").forward(request, response);

    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    
    }    
}
Alexei Petrov
  • 331
  • 5
  • 14
  • I did the correction you proposed but the page is blannk. there is no error anymore but i think i shoud see the html content of the jsp file. don't you think so ? – Jimmy Maghrebien Mar 01 '17 at 16:27
  • What is your app name? Where is your JSP file? As you can see from picture it works. Your url is probably wrong. – Alexei Petrov Mar 01 '17 at 16:29
  • i am new in servlet programming. how can i know my app name ? changing the name attribute as you proposed, permitted to cancel the java error but the page is still blank. May be you talk about the project name ? – Jimmy Maghrebien Mar 01 '17 at 16:33
  • url should be http://localhost:8080/YourProjectNameHere/MaSecondeServlet – Alexei Petrov Mar 01 '17 at 16:40
  • ok thank you Alexei. As you can see i am still a dummy in Java :) i am going to try that. – Jimmy Maghrebien Mar 01 '17 at 16:41
  • The url i should use is : http://localhost:8080/MaBeanJSP/MaSecondeServlet Because the project name is MaBeanJSP But i still have a blank page. Maybe my web.xml file is not well configured ? – Jimmy Maghrebien Mar 01 '17 at 16:44
  • You don't show your web.xml. Just delete it. For that servlet you don't need web.xml since you use annotations. Try again and if you don't see anything i can't help you further. – Alexei Petrov Mar 01 '17 at 16:50
  • 1
    Thank you Alexei.i changed a line in the web.xml file : /SecondeServlet.jsp became : /MaSecondeServlet And it worked. Thanks a lot – Jimmy Maghrebien Mar 01 '17 at 16:59