问题
Im trying to login from a jsp page, using a loginServlet. It redirects to the servlet, but it doesn't make the authentication and throws an error. Here is the code:
JSP file:
<div class="container">
<div class="row">
<div class="box">
<div class="col-md-6 col-md-offset-4">
<h2 class="intro-text">Welcome! Login here:</h2>
<form class="form-horizontal" action="LoginServlet" method="POST">
<div class="form-group">
<label class="control-label col-sm-2" for="username">Username:</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="username" placeholder="Enter username" name="un">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Password:</label>
<div class="col-sm-4">
<input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-4">
<div class="checkbox">
<label><input type="checkbox" name="remember"> Remember me</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-4">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
LoginServlet:
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
try
{
User user = new User();
user.setUsername(request.getParameter("un"));
user.setPassword(request.getParameter("pwd"));
PrintWriter writer = response.getWriter();
// build HTML code
String htmlRespone = "<html>";
htmlRespone += "<h2>Your username is: " + user.getUsername() + "<br/>";
htmlRespone += "Your password is: " + user.getPassword() + "</h2>";
htmlRespone += "</html>";
writer.println(htmlRespone);
if (authUser(user))
{
System.out.println("exists!");
HttpSession session = request.getSession(true);
session.setAttribute("currentSessionUser",user);
response.sendRedirect("home.jsp"); //logged-in page
}
else {
System.out.println("not!");
response.sendRedirect("invalidLogin.jsp"); //error page
}
}
catch (Throwable theException)
{
System.out.println(theException);
}
}
public boolean authUser(User user) {
// TODO Auto-generated method stub
User userDB = UserDAO.getUserByUsername(user.getUsername());
System.out.println(userDB.getUsername()+" "+userDB.getPassword());
System.out.println(user.getUsername()+" "+user.getPassword());
if (userDB != null && userDB.getUsername().equals(user.getUsername()) && userDB.getPassword().equals(user.getPassword())) {
return true;
} else {
return false;
}
}
}
getUserByUsername method from UserDAO:
public static User getUserByUsername(String username) {
Session session = factory.openSession();
Transaction tx = null;
List<User> users = null;
tx = session.beginTransaction();
Query query = session.createQuery("FROM User WHERE username = :username ");
query.setParameter("username", username);
List<User> result = query.list();
tx.commit();
return result != null && !result.isEmpty() ? result.get(0) : null;
}
POM file:
<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>org.paul.licenta</groupId>
<artifactId>teachApp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>teachApp</name>
<build>
<finalName>teachApp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.0.0.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.0.GA</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0-m08</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.10.Final</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
<!-- for JPA, use hibernate-entitymanager instead of hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.10.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>5.3.0.Final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
-->
</dependencies>
<properties>
<jersey.version>2.26-b03</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
And here are the errors that I get:
SEVERE: StandardWrapper.Throwable
java.lang.NoClassDefFoundError: javax/ws/rs/client/RxInvokerProvider
at org.glassfish.jersey.internal.inject.Providers.getJaxRsProviderInterfaces(Providers.java:114)
at org.glassfish.jersey.internal.inject.Providers.<clinit>(Providers.java:87)
at org.glassfish.jersey.model.internal.ComponentBag.modelFor(ComponentBag.java:435)
at org.glassfish.jersey.model.internal.ComponentBag.lambda$registerModel$5(ComponentBag.java:394)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.model.internal.ComponentBag.registerModel(ComponentBag.java:386)
at org.glassfish.jersey.model.internal.ComponentBag.register(ComponentBag.java:297)
at org.glassfish.jersey.model.internal.CommonConfig.register(CommonConfig.java:459)
at org.glassfish.jersey.server.ResourceConfig.register(ResourceConfig.java:447)
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:332)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:178)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:370)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1269)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1182)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1072)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5368)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5660)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javax.ws.rs.client.RxInvokerProvider
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1892)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1735)
... 27 more
Aug 06, 2017 11:27:45 AM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet [Jersey Web Application] in web application [/teachApp] threw load() exception
java.lang.ClassNotFoundException: javax.ws.rs.client.RxInvokerProvider
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1892)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1735)
at org.glassfish.jersey.internal.inject.Providers.getJaxRsProviderInterfaces(Providers.java:114)
at org.glassfish.jersey.internal.inject.Providers.<clinit>(Providers.java:87)
at org.glassfish.jersey.model.internal.ComponentBag.modelFor(ComponentBag.java:435)
at org.glassfish.jersey.model.internal.ComponentBag.lambda$registerModel$5(ComponentBag.java:394)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.model.internal.ComponentBag.registerModel(ComponentBag.java:386)
at org.glassfish.jersey.model.internal.ComponentBag.register(ComponentBag.java:297)
at org.glassfish.jersey.model.internal.CommonConfig.register(CommonConfig.java:459)
at org.glassfish.jersey.server.ResourceConfig.register(ResourceConfig.java:447)
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:332)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:178)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:370)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1269)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1182)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1072)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5368)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5660)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Aug 06, 2017 11:27:45 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Aug 06, 2017 11:27:45 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Aug 06, 2017 11:27:45 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 9144 ms
java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/Session;
I tried to add other jars suggested on the internet and some other things that I found here but nothing worked so far. I want it do redirect me to the specified pages after authentication. Any ideas?
Thank you very much!
回答1:
I actually had the same issue.. my solution was based upon the following:
RxInvokerProvider is in the jaxrs-api with version 2.1 -> you are referencing 2.0
jersey 2.26 is relying on jaxrs 2.1
After this issue you will be facing the "Injection" Issue (https://github.com/jersey/jersey/blob/12e5d8bdf22bcd2676a1032ed69473cf2bbc48c7/docs/src/main/docbook/migration.xml) so you will need the jersey-hk dependency
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jersey.version}</version>
</dependency>
回答2:
The java.lang.ClassNotFoundException: javax.ws.rs.client.RxInvokerProvider
Exception happens during initialization, not during Login. Requests would only be handled after the last lines in the log that you state: INFO: Starting ProtocolHandler ["http-bio-8080"]
.
In order to get to the root cause of this exception, run your tomcat in a debugger, set an exception breakpoint on ClassNotFoundException
and check the actual type of Servlet (there's GenericServlet.init()
in the stacktrace). That will be the servlet that you'll need to fix. Most likely through adding the required code (or any transitive dependency) to the classpath.
Other than this, this code is wrong on quite a number of levels:
- You're writing output to the response before you redirect: Once text has been sent to the browser, you won't be able to redirect any more, as this requires writing HTTPS headers. When they're already sent, you're out of luck and will generate more exceptions.
- You're not recovering in your transaction from any exceptions (e.g. commit/rollback should be within a finally block) - but at least you're not prone to SQL-injection.
- You're storing clear-text passwords in the database
- You're implementing your own log in when this problem has long been solved, e.g. through the container (read the servlet spec)
来源:https://stackoverflow.com/questions/45530132/java-lang-classnotfoundexception-javax-ws-rs-client-rxinvokerprovider