java.lang.NoClassDefFoundError how to resolved this error - java

when i submit the registration form Registration servlet run but when i created an object of session factory of hibernate it give me an Exception ."javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)".Please help me to find from this exception.
package login;
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;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
#WebServlet("/Registration")
public class Registration extends HttpServlet {
private static final long serialVersionUID = 1L;
public Registration() {
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String username = request.getParameter("username");
String password = request.getParameter("password");
String name = request.getParameter("name");
User u = new User();
u.setUsername(username);
u.setPassword(password);
u.setName(name);
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
}

Add latest hibernate.jar in your class path

Related

Package doesnt exist error when trying to import my own package to servlet

im trying to import my own packages to a servlet im making, using tomcat.
The two java files im working with are the servlet (movieServlet.java), and another class im trying to import from a package (Movie.java)
The hierarchy of folders is:
classes
|
+--movieServlet.java
|
+--movie
|
+--Movie.java
movieServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import movie.Movie;
#WebServlet(urlPatterns = {"/movieServlet"})
public class movieServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
Movie Movie = new Movie();
request.setAttribute("movies", Movie.getAllMovies());
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/jsps/movies.jsp");
dispatcher.forward(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/jsps/movieDetails.jsp");
dispatcher.forward(request,response);
}
}
and this is the top of Movie.java (its a long file)
package movie;
import java.util.*;
import java.util.List;
import javax.sql.*;
import java.sql.*;
import javax.naming.InitialContext;
public class Movie implements java.io.Serializable
{
private static DataSource dataSource = null;
private static Connection c = null;
private int id;
private String title;
private String url;
private int year;
public Movie(){}
if theres more info needed please ask :), this has been reallllly bugging me.
I am new here so excuse me if my answer is not perfect. As you have mentioned that you want to import your own package which contains Movie class. I tried to implement all your main methods and it's running perfect and I am getting my value from Movie.java.
movieServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import movie.Movie;
public class movieServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
Movie Movie = new Movie();
request.setAttribute("movies", Movie.getAllMovies());
out.println(request.getAttribute("movies"));
}
}
Movie.java
package movie;
public class Movie {
String s;
public Movie(){
s="hello zDoctor";
}
public String getAllMovies(){
return s;
}
}
servlet output
file structure

Error while adding servlet mapping for path

I am trying to deploy my application on a local Websphere Application Server (Version "8.5.5.5").
If I click "Run on Server", I always get:
Error while adding servlet mapping for path-->/xcc-admin/test/query,
wrapper-->ServletWrapper[XCCAdminServlet.XCCAdminMain:[/XCCAdminMain,
/xcc-admin/rest/query, /xcc-admin/test/query]],
application-->xcc-admin.
Code:
package XCCAdminServlet;
import java.io.IOException;
import javax.annotation.Resource;
import javax.annotation.Resource.AuthenticationType;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
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 javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
/**
* Servlet implementation class XCCAdminMain
*/
#WebServlet({"/XCCAdminMain", "/xcc-admin/rest/query", "/xcc-admin/test/query"})
#Resource(name = "jdbc/MyDataSource", type = javax.sql.DataSource.class, shareable = true, authenticationType = AuthenticationType.CONTAINER)
public class XCCAdminMain extends HttpServlet {
private static final long serialVersionUID = 1L;
#PersistenceUnit(unitName = "XCCAdminUnit")
private EntityManagerFactory emf;
/**
* #see HttpServlet#HttpServlet()
*/
public XCCAdminMain() {
super();
// TODO Auto-generated constructor stub
}
#GET
#Path("/{param}")
public Response getQuery(#PathParam("param") String msg) throws ServletException {
return Response.status( 200 ).entity( "Get Request received, queryparam: " + msg ).build();
}
}
How to fix this?

how to write code for user task in JBPM

I am a newbie when it comes to JBPM. And while I have created the sample application with a script task and user task. When I remove user task the bpmn is executing fine. But when I keep UserTask it is throwing an exception.
Following is my code:
package com.Sservlet;
import java.io.IOException;
import java.util.List;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import org.drools.KnowledgeBase;
import org.drools.SystemEventListenerFactory;
import org.drools.builder.*;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.process.ProcessInstance;
import org.jbpm.bpmn2.handler.ServiceTaskHandler;
import org.jbpm.process.workitem.wsht.CommandBasedWSHumanTaskHandler;
import org.jbpm.task.*;
import org.jbpm.task.service.*;
public class SServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("Jcr.bpmn2"), ResourceType.BPMN2);
KnowledgeBase kbase = kbuilder.newKnowledgeBase();
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
CommandBasedWSHumanTaskHandler taskHandler = new CommandBasedWSHumanTaskHandler(ksession);
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new ServiceTaskHandler());
EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.task");
TaskService taskService = new TaskService(emf, SystemEventListenerFactory.getSystemEventListener());
TaskServiceSession taskSession = taskService.createSession();
// now register new users and groups
List list = taskSession.getTasksAssignedAsPotentialOwner("John", "en-UK");
taskHandler.connect();
ProcessInstance processInstance = ksession.startProcess("TestHandle");
System.out.println("hello here are");
}
}
I am receiving a No Persistence provider exception:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named org.jbpm.task
I don't need any database insertion here. It is a simple user-task. Is there any solution?

NullPointerException on Apache Server Servlet using JPA

Im trying to display contents of table (test_dept) which is in SQLSERVER
I have created a connection profile also.
I have written a Servlet like below... But Im getting this error.
import java.io.IOException;
import java.io.PrintWriter;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.servlet.ServletException;
//import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#SuppressWarnings("serial")
#WebServlet(urlPatterns = "/ServletClient")
public class ServletClient extends HttpServlet
{
#PersistenceUnit
EntityManagerFactory factory;
#SuppressWarnings("rawtypes")
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
//ServletOutputStream out = resp.getOutputStream();
PrintWriter pw = resp.getWriter();
java.util.List list = factory.createEntityManager().createQuery("select f from test_dept f;").getResultList();
pw.println("<html><body bgcolor=silver text=green><table>");
for (Object tdp : list)
{
pw.println("In The Loop");
pw.println("<tr><td>" + ((TestDept) tdp).getDptnam() + "</td></tr>");
}
pw.println("</table>");
pw.println("<font size=35><b>List created AdapChain</b></font>");
pw.println("</body></html>");
}
}
I don't think any version of Apache Tomcat supports injection of EntityManager or EntityManagerFactory objects out of the box.
You need to choose a server platform that supports more of the JavaEE specification.

Using HttpRequestHandlerServlet in programatically configured Jetty embedded server

I have programatically defined a Jetty server and added an instance of a HttpRequestHandlerServlet. I am trying to do all of this without a web.xml file. Here is a simplified version of my code:
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.Test;
import static org.mockito.Mockito.*;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.context.support.HttpRequestHandlerServlet;
public class TestServer extends Server {
public TestServer() throws Exception {
super(8888);
final ServletContextHandler contextHandler = new ServletContextHandler(this, "/", ServletContextHandler.NO_SESSIONS|ServletContextHandler.NO_SECURITY);
final ServletContext context = contextHandler.getServletContext();
StaticApplicationContext applicationContext = new StaticApplicationContext();
applicationContext.registerSingleton("testServlet", TestBean.class);
applicationContext.refresh();
GenericWebApplicationContext webApplicationContext = new GenericWebApplicationContext();
webApplicationContext.setParent(applicationContext);
webApplicationContext.setServletContext(context);
webApplicationContext.refresh();
context.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webApplicationContext);
ServletConfig servletConfig = new ServletConfig() {
#Override
public String getServletName() {
return "testServlet";
}
#Override
public ServletContext getServletContext() {
return context;
}
#Override
public String getInitParameter(String name) {
return null;
}
#Override
public Enumeration<String> getInitParameterNames() {
return null;
}
};
HttpRequestHandlerServlet httpRequestHandlerServlet = new HttpRequestHandlerServlet();
httpRequestHandlerServlet.init(servletConfig);
ServletHolder servletHolder = new ServletHolder(httpRequestHandlerServlet);
contextHandler.addServlet(servletHolder, "/testBean");
start();
}
#Test
public void test() throws Exception {
TestServer testServer = new TestServer();
Request baseRequest = new Request();
HttpServletRequest request = mock(HttpServletRequestWrapper.class);
HttpServletResponse response = mock(HttpServletResponseWrapper.class);
testServer.handle("/testBean", baseRequest, request, response);
}
}
class TestBean implements HttpRequestHandler {
#Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getOutputStream().write("Steve".getBytes());
}
}
If you run the test, the following exception (root cause) occurs:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.web.context.support.HttpRequestHandlerServlet-406774688' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:570)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1108)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:270)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1121)
at org.springframework.web.context.support.HttpRequestHandlerServlet.init(HttpRequestHandlerServlet.java:58)
at javax.servlet.GenericServlet.init(GenericServlet.java:241)
at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:477)
I am using Jetty 7.6.9.v20130131 (due to a dependency in my real project on Camel 2.12.1) and Spring 3.2.4.RELEASE.
Any help is greatly appreciated.
Steve Ardis
I knew that "The target bean name must match the HttpRequestHandlerServlet servlet-name as defined in web.xml" (see the JavaDoc on HttpRequestHandlerServlet), but I was only doing half of what was required when using the programmatic approach.
Adding the following code got me past this issue:
servletHolder.setName("testServlet");
Steve Ardis

Categories