0

I have a problem: When I call index.jsp, it's not calling servlet.

(1) index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:forward page="HelloServlet"/>
</body>
</html>

(2) servlet/HelloServlet.java:

package servlet;

import java.io.IOException;

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("/HelloServlet")
public class HelloServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;


    public HelloServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.getRequestDispatcher("welcome.jsp").forward(request, response);
    }
}

(3) welcome.jsp:

<body>
    Hello world
</body>

(4) web.xml

<?xml version="1.0" encoding="ISO-8859-1"?> 
<web-app 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" 
version="2.4"> 

 <welcome-file-list>       
        <welcome-file>index.jsp</welcome-file>           
 </welcome-file-list>

 <servlet> 
     <servlet-name>hello</servlet-name> 
     <servlet-class>servlet.HelloServlet</servlet-class> 
 </servlet> 

 <servlet-mapping> 
     <servlet-name> hello </servlet-name> 
     <url-pattern> /welcome</url-pattern> 
 </servlet-mapping> 

</web-app> 

Thank you for seeing and helping me

Naman
  • 2,205
  • 2
  • 19
  • 32

2 Answers2

0

Use the servlet's URL pattern instead of the class name in your jsp:forward tag:

<jsp:forward page="/welcome"/>
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
0

There is no issues in coding. Check your project name. The path must be http:localhost:8080/Servlet/HelloServlet

Here Servlet is project name. Check this once.

The URL must be /HelloServlet. Not /servlet/HelloServlet

mallikarjun
  • 88
  • 1
  • 7