I was trying to implement a login filter in my web app with jsf 2, following this guide:
https://stackoverflow.com/tags/servlet-filters/info
after I compiled my filter and added the .class in "web-inf/classes" (as the guide says) the filter worked, but i put the wrong url to redirect to the login page so i deleted the filter.class from the folder (web-inf/classes) and tried to compile the project again , but it failed, and since then im getting "package javax.servlet does not exist"
it is weird because before it was working and i have javax.servlet in my pom.xml.. i tried cleaning the project, but nothing.
this is my filter class:
package Bean; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Created with IntelliJ IDEA. * User: rodrigo * Date: 28-04-13 * Time: 06:54 AM * To change this template use File | Settings | File Templates. */ @WebFilter("/Contenido/*") public class filtro implements Filter { @Override public void init(FilterConfig config) throws ServletException { // If you have any <init-param> in web.xml, then you could get them // here by config.getInitParameter("name") and assign it as field. } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { HttpServletRequest req = (HttpServletRequest) request; LoginBean user = (LoginBean) req.getSession().getAttribute("user"); if (user != null && user.isLoggedIn()) { // User is logged in, so just continue request. chain.doFilter(request, response); } else { // User is not logged in, so redirect to index. HttpServletResponse res = (HttpServletResponse) response; res.sendRedirect(req.getContextPath() + "/Contenido/Login.xhtml"); } } @Override public void destroy() { // If you have assigned any expensive resources as field of // this Filter class, then you could clean/close them here. } }
this is the error:
\Users\rodrigo\IdeaProjects\Frutemu\src\main\java\Bean\filtro.java:[5,20] error: package javax.servlet does not exist [ERROR] \Users\rodrigo\IdeaProjects\Frutemu\src\main\java\Bean\filtro.java:[6,20] error: package javax.servlet does not exist [ERROR] \Users\rodrigo\IdeaProjects\Frutemu\src\main\java\Bean\filtro.java:[7,20] error: package javax.servlet does not exist [ERROR] \Users\rodrigo\IdeaProjects\Frutemu\src\main\java\Bean\filtro.java:[8,20] error: package javax.servlet does not exist [ERROR] \Users\rodrigo\IdeaProjects\Frutemu\src\main\java\Bean\filtro.java:[9,20] error: package javax.servlet does not exist [ERROR] \Users\rodrigo\IdeaProjects\Frutemu\src\main\java\Bean\filtro.java:[10,20] error: package javax.servlet does not exist [ERROR] \Users\rodrigo\IdeaProjects\Frutemu\src\main\java\Bean\filtro.java:[11,31] error: package javax.servlet.annotation does not exist
my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Frutemu</groupId> <artifactId>Frutemu</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>Frutemu Maven Webapp</name> <url>http://maven.apache.org</url> <repositories> <repository> <id>prime-repo</id> <name>Prime Repo</name> <url>http://repository.primefaces.org</url> </repository> </repositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.ejb</groupId> <artifactId>ejb-api</artifactId> <version>3.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.primefaces</groupId> <artifactId>primefaces</artifactId> <version>3.5</version> </dependency> <dependency> <groupId>org.primefaces.themes</groupId> <artifactId>all-themes</artifactId> <version>1.0.9</version> </dependency> <dependency> <groupId>javax.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.0</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-impl</artifactId> <version>2.0.2-b10</version> <scope>compile</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- MySQL database driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>el-impl</artifactId> <version>2.2.1-b04</version> <scope>provided</scope> </dependency> <!-- OpenJPA framework --> <dependency> <groupId>org.apache.openjpa</groupId> <artifactId>openjpa-all</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>sqljdbc4</artifactId> <version>4.0</version> </dependency> <dependency> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>4.1.2</version> </dependency> </dependencies> <build> <finalName>Frutemu</finalName> <plugins> <!-- Open Jpa --> <plugin> <groupId>org.apache.openjpa</groupId> <artifactId>openjpa-maven-plugin</artifactId> <version>2.2.0</version> <configuration> <includes>**/model/*.class</includes> <addDefaultConstructor>true</addDefaultConstructor> <enforcePropertyRestrictions>true</enforcePropertyRestrictions> </configuration> <executions> <execution> <id>enhancer</id> <phase>process-classes</phase> <goals> <goal>enhance</goal> </goals> </execution> </executions> </plugin> <!-- Plugin para levantar una instancia de Tomcat 7 liviana, única para este proyecto --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.0</version> <configuration> <url>http://127.0.0.1:8080/manager/text</url> <server>TomcatServer</server> <path>/Frutemu</path> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jasperreports-maven-plugin</artifactId> <executions> <execution> <goals> <goal>compile-reports</goal> </goals> </execution> </executions> <dependencies> <!--note this must be repeated here to pick up correct xml validation --> <dependency> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>4.1.2</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
If you're using the command console to compile the servlet, then you should include Tomcat's /lib/servlet-api. jar in the compile classpath. If you're using an IDE, then you should integrate Tomcat in the IDE and reference it as target runtime in the project.
It contains, among others, the files /usr/share/java/servlet-api-2.5. jar and /usr/share/java/jsp-api-2.1. jar , which are the servlet and JSP libraries you need.
The javax prefix is used by the Java programming language for a package of standard Java extensions. These include extensions such as javax. servlet, which deals with running servlets, and javax. jcr, which deals with the Java content library. Package, Programming terms.
This package provides the number of interfaces and classes to support Generic servlet which is protocol independent. These interfaces and classes describe and define the contracts between a servlet class and the runtime environment provided by a servlet container.
The javax.servlet dependency is missing in your pom.xml. Add the following to the dependencies-Node:
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency>
I only put this code in my pom.xml and I executed the command maven install.
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With