0

I'm trying to work only with native java without using frameworks such as spring, struts etc.

I'm building a web application using servlets and jsps.

My project layout in Project explorer (in Eclipse): MyApp (java project), MyAppWEB (dynamic web project), MyAppWEBEAR (ear project),

Servlet is inside the MyApp project. Jsp is inside MyAppWEB project.

Both projects added to EAR. MyApp was added to MyAppWEB's manifest as jar in deployment assembly.

And now is the problem : When i use @WebServlet("/hi") above the servlet class name i get error 404.

But, if i define the servlet inside web.xml then everything works fine (without the annotation).

Also if export MyApp project as jar and put it inside WEB-INF/lib folder without using web.xml then it works fine too.

So the question is, is it possible to use this annotation inside java project (with out exporting manually to jar and put inside WEB-INF/lib) ?

web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>MyAppWEB</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
<!--   <servlet> -->
<!--     <description></description> -->
<!--     <display-name>HelloWorldServlet</display-name> -->
<!--     <servlet-name>HelloWorldServlet</servlet-name> -->
<!--     <servlet-class>com.MyCom.MyApp.Servlets.HelloWorldServlet</servlet-class> -->
<!--   </servlet> -->
<!--   <servlet-mapping> -->
<!--     <servlet-name>HelloWorldServlet</servlet-name> -->
<!--     <url-pattern>/Hello</url-pattern> -->
<!--   </servlet-mapping> -->
</web-app>

HelloWorldServlet:

package com.MyCom.MyApp.Servlets;

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("/hi")
public class HelloWorldServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public HelloWorldServlet() {
        super();
    }

    /**
     * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
     */
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsp/HelloWorld.jsp").forward(request, response);
    }

}

HelloWorld.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>MyApp</title>
</head>
<body>
    <h1>Hello World !!</h1>
</body>
</html>

1 Answers1

0

OK, got this figured out. It appears one also need to add the MyApp.jar to Deployment Assembly of the MyAppWEB project, and not only in Manifest Entries. This in turn will put the the MyApp.jar at WEB-INF/lib automatically at deployment on server. Now annotations works just fine.