0
votes

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
Where is the servlet along with web.xml? - VHS
i am new in stackoverflow. i am trying to display the java code servlet but when i try to save my post it says "it looks like your post is mostly code. please add some more details". i am trying to post that code. - Jimmy Maghrebien
Phew !! i succeeded posting that code :) - Jimmy Maghrebien
Where is your web.xml ? See my answer and if you have <servlet> and <servlet-mapping> tags remove them from there. - Alexei Petrov

1 Answers

1
votes

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 {    
    }    
}