java method can't run in a jsp file - java

I have a problem.
I have configured in eclipse hibernate with a database in mysql.
I have created a file java, and if I try to save some data to database works perfectly.
The problem is, if I create a jsp file, and try to call the same method it doesn't work, I guess it doesn't connect with hibernate.
public class CreateData {
public int age;
public static void main(String[] args) throws Exception {
SessionFactory sessFact = HibernateUtil.getSessionFactory();
Session session = sessFact.getCurrentSession();
org.hibernate.Transaction tr = session.beginTransaction();
Employee emp = new Employee();
emp.setEmpName("Deepak Kumar");
emp.setEmpMobileNos("000000");
emp.setEmpAddress("Delhi - India");
session.save(emp);
tr.commit();
System.out.println("Successfully inserted");
sessFact.close();
}
public void insert() throws Exception {
SessionFactory sessFact = HibernateUtil.getSessionFactory();
Session session = sessFact.getCurrentSession();
org.hibernate.Transaction tr = session.beginTransaction();
Employee emp = new Employee();
emp.setEmpName("Deepak Kumar");
emp.setEmpMobileNos("000000");
emp.setEmpAddress("Delhi - India");
session.save(emp);
tr.commit();
System.out.println("Successfully inserted");
sessFact.close();
}
public int getAge(){return this.age;}
}
So, if a run the main of this class java ,everything works fine.
but I can't run insert() method in this jsp file (obviously does the some thing as the main )
JSP FILE example:
jsp:useBean id= "user" class= "javabean.CreateData" scope="session"
<% user.getAge();%> // THIS WORKS
<% user.insert();%> // THIS DOESN'T WORK
when I run the jsp gives me an error
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/hibernate/HibernateException
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
org.apache.jsp.savesclient_jsp._jspService(savesclient_jsp.java:121)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
java.lang.NoClassDefFoundError: org/hibernate/HibernateException
javabean.insertClient.inserisci2(insertClient.java:61)
org.apache.jsp.savesclient_jsp._jspService(savesclient_jsp.java:101)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
java.lang.ClassNotFoundException: org.hibernate.HibernateException
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
javabean.insertClient.inserisci2(insertClient.java:61)
org.apache.jsp.savesclient_jsp._jspService(savesclient_jsp.java:101)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

Related

Could not find class to hibernate configuration?

I am using Intellij, Maven, Tomcat, Hibernate and JEE to create some app.
When I try to run my application through Intellij through Tomcat there is an error in the webrowser:
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:579)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:461)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:909)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838)
org.apache.jsp.addAdvertisement_jsp._jspService(addAdvertisement_jsp.java:176)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
com.advertisement.entity.AdvertisementDAO.init(AdvertisementDAO.java:29)
org.apache.jsp.addAdvertisement_jsp._jspService(addAdvertisement_jsp.java:148)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1352)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1180)
com.advertisement.entity.AdvertisementDAO.init(AdvertisementDAO.java:29)
org.apache.jsp.addAdvertisement_jsp._jspService(addAdvertisement_jsp.java:148)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
My project structure looks like this:
When I run JUnit test all works correctly.
import com.advertisement.entity.Advertisement;
import com.advertisement.entity.AppUser;
import junit.framework.TestCase;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Unit test for simple App.
*/
public class AppTest extends TestCase {
public void testAppUser() {
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
AppUser user = new AppUser("firstuser");
session.save(user);
session.getTransaction().commit();
session.close();
}
public void testAppAdvertisement() {
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Advertisement advertisement = new Advertisement();
session.save(advertisement);
session.getTransaction().commit();
session.close();
}
}
The problem is when I deploy it to Tomcat and after that run. It's seems to me that Tomcat does not see the Hibernate or Hibernate's configuration, some Hibernate config path is incorrect?

org.apache.jasper.JasperException: An exception occurred processing JSP page /upload.jsp at line 17

I am Using Tomcat-7 And JRE 1.8.X
ClassPath: C:\Program Files\Tomcat\lib\commons-fileupload-1.3.2.jar;C:\Program Files\Tomcat\lib\commons-io-2.5.jar;C:\Program Files\Tomcat\lib\servlet-api.jar;
Woking Directory: C:\Program Files\Tomcat\webapps\src
Trying to upload a photo from computer to C:\Program Files\Tomcat\webapps\src\uplods\username.
My Code : \uplaod.jsp
<%# page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%# page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%# page import="org.apache.commons.fileupload.*"%>
<%# page import="java.util.*, java.io.*" %>
<%# page import="java.util.Iterator"%>
<%# page import="java.util.List"%>
<%# page import="java.io.File"%>
<%# page contentType="text/html;charset=UTF-8" %>
<%
try
{
File savedFile=new File("x.txt");
String ImageFile="";
String itemName = "";
String uname=session.getAttribute("uname").toString();
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart)
{%>
<script>alert("Form enctype must be "multipart/form-data".");</script>
<%}
else
{
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
items = upload.parseRequest(request);
Iterator itr = items.iterator();
FileItem item = (FileItem) itr.next();
try
{
itemName = item.getName();
savedFile = new File(config.getServletContext().getRealPath("/")+"\\src\\"+"\\uploads\\"+"\\"+uname+"\\"+uname+".jpg");
out.print(itemName);
item.write(savedFile);
}
catch (Exception e){out.println("Error"+e.getMessage());}
}
}
catch (Exception e){out.println(e.getMessage());}
response.sendRedirect("SignUpConfirmPage.htm");
%>
I am getting error message like:
ype Exception report
message javax.servlet.ServletException: java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:561)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:462)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
javax.servlet.ServletException: java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:916)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:845)
org.apache.jsp.upload_jsp._jspService(upload_jsp.java:147)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
org.apache.commons.fileupload.servlet.ServletFileUpload.isMultipartContent(ServletFileUpload.java:68)
org.apache.jsp.upload_jsp._jspService(upload_jsp.java:106)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.ClassNotFoundException: javax.servlet.http.HttpServletRequest
java.net.URLClassLoader.findClass(Unknown Source)
java.lang.ClassLoader.loadClass(Unknown Source)
java.lang.ClassLoader.loadClass(Unknown Source)
org.apache.commons.fileupload.servlet.ServletFileUpload.isMultipartContent(ServletFileUpload.java:68)
org.apache.jsp.upload_jsp._jspService(upload_jsp.java:106)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Please Help me with this.

Exception in JSP when populating a table with list

I have this jsp code .
<c:forEach var="item" items="${UsersList}">
<tr>
<td><c:out value="${item.userId}" /></td>
<td><c:out value="${item.userName}" /></td>
<td><c:out value="${item.car}" /></td> //Here , I am getting exception
</tr>
</c:forEach>
The UserList consists of an attribute called "car" of datatype "Car"
Pojo Class class for Users:
#Entity
#Table(name = "Users")
public class Users implements java.io.Serializable {
private int userId;
private Car car;
private Groups groupId;
private UserType userType;
private String userName;
//getters and setters
}
I am using Spring MVC Framework. On page load my app should show a table with the list of users . In controller I am querying for the list of users and I am adding it to the ModelAndView object
Controller code:
List<Users> userList = service.getUserList(); //this will get list of users
//System.out.println("Userlist ==" +userList.size());
mv.addObject("UsersList" , usersList);
//skipped remaining code
When I tried without car (tabledata tag) JSP is running without exception and showing the users list in the table . If I try to add car (tabledata tag) then I am getting an exception
HTTP Status 500 - An exception occurred processing JSP page /WEB-INF/pages/CreateUser.jsp at line 199
org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/pages/CreateUser.jsp at line 199
196:<tr>
197:<td><c:outvalue="${item.userId}" /></td>
198:<td><c:out value="${item.userName}" /></td>
199:<!-- <td><c:out value="${item.car}" /></td> -->
200:<td><c:out value="${item.modifiedDate}" /></td>
201:</tr>
202:</c:forEach>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWra pper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:4 70)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutput Model(InternalResourceView.java:168)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:3 03)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.j ava:1244)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(Disp atcherServlet.java:1027)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServl et.java:971)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServle t.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkSer vlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java :861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.ja va:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
root cause
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:149)
org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195)
org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
com.bdp.pojo.Role_$$_javassist_11.toString(Role_$$_javassist_11.java)
org.apache.taglibs.standard.tag.common.core.OutSupport.out(OutSupport.java:178)
org.apache.taglibs.standard.tag.common.core.OutSupport.doStartTag(OutSupport.java:99)
org.apache.jsp.WEB_002dINF.pages.CreateUser_jsp._jspx_meth_c_005fout_005f2(CreateUser_jsp.java:667)
org.apache.jsp.WEB_002dINF.pages.CreateUser_jsp._jspx_meth_c_005fforEach_005f0(CreateUser_jsp.java:591)
org.apache.jsp.WEB_002dINF.pages.CreateUser_jsp._jspService(CreateUser_jsp.java:264)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1244)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframeweb.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
Since you are using hibernate lazy initialization, whenever you return the main object to the JSP it will try to fetch sub classes. And this wont happen because session would be closed.
Therefore change your fetching strategy to the Eager:
On your car's getter method:
#OneToOne(fetch=FetchType.EAGER)
This will cause fetching car immediatly when you fetch the main class.
To make it on query:
List<Users> items = session.createCriteria(Users.class)
.setFetchMode("car", FetchMode.JOIN).
.createAlias("car", "car").
.list();
You receive error LazyInitializationException. Users objects in a lazy Car object. service.getuserlist(); method can bring in by fetch the Car objects.
Fetching Strategies
FetchType.LAZY is on demand
FetchType.EAGER is immediate
#OneToOne(fetch=FetchType.EAGER) when you do want to take the user object as object the car all the time.
service.getUserList () please write.
EDIT :
Example getUserList method:
public List<Users> getUserList(){
String sql = " SELECT user from Users user left join fetch user.car ";
Query query = entityManager.createQuery(sql);
return query.getResultList();
}

Hibernate `Session is closed` exception even the session is not closed

I have a Coupons database, and using Hibernate I'm trying to get 5 rows.
This is the code:
public List<Coupon> getPage(int pageNum, boolean isActive) {
session = factory.openSession();
session.beginTransaction();
Query query;
if(isActive) {
query = session.createQuery("from Coupon where (NOW() < fadeoutDate)");
} else {
query = session.createQuery("from Coupon");
}
query.setFirstResult((pageNum - 1) * numCouponsPerPage);
query.setMaxResults(numCouponsPerPage);
List<Coupon> page = (List<Coupon>) query.list();
closeSession();
return page;
}
(numCouponsPerPage is a constant that equal 5)
When trying to get a page:
List<Coupon> p = cm.getPage(dpage,active.equalsIgnoreCase("true")); (dpage = 1, the second parameter return false),
I'm getting this error:
Dec 14, 2013 4:11:06 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/firstXDw] threw exception [An exception occurred processing JSP page /views/system/loadPage.jsp at line 30
27:
28: if (whatToLoad.equalsIgnoreCase("coupons")) {
29:
30: List<Coupon> p = cm.getPage(dpage,active.equalsIgnoreCase("true"));
31: for (Coupon c : p) {
32: out.println("<tr>");
33: out.println("<td>" + c.getId() + "</td>");
Stacktrace:] with root cause
org.hibernate.SessionException: Session is closed!
at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:72)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1768)
at implementations.Coupons.getPage(Coupons.java:130)
at org.apache.jsp.views.system.loadPage_jsp._jspService(loadPage_jsp.java:82)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Why is exception Session is closed thrown? I open the session at the first line of the function..
What is the problem and how to solve it?

Unable to connect to database based on property file

Hi I'm new in java/jsp developing.
I'm trying to connect to database depending on parameter that jsp file receives from previous page.
My program works if i connect to single DB like
try {
InputStream in = getServletContext().getResourceAsStream("/WEB-INF/db.properties");
properties.load(in);
connString = properties.getProperty("db_conn").toString();
connUser = properties.getProperty("db_user").toString();
connPass = properties.getProperty("db_pass").toString();
bPath = properties.getProperty("exec_path").toString();
in.close();
} catch (IOException ex) {
System.out.println(ex);
}
and db.properties file contains
db_conn=jdbc:oracle:thin:#33.333.3.33:3333:DKSE1
db_user=guest
db_pass=guestpw
My attempt:
String userinfo = request.getParameter("userinfo");
try {
InputStream in = getServletContext().getResourceAsStream("/WEB-INF/db.properties");
properties.load(in);
db_conn = userinfo + "_conn";
db_user = userinfo + "_user";
db_pass = userinfo + "_pass";
connString = properties.getProperty(db_conn).toString();
connUser = properties.getProperty(db_user).toString();
connPass = properties.getProperty(db_pass).toString();
bPath = properties.getProperty("exec_path").toString();
in.close();
} catch (IOException ex) {
System.out.println(ex);
}
try {
// --- Required database stuff ---
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
Connection conn = DriverManager.getConnection(connString, connUser, connPass);
Statement stmt = conn.createStatement();
More codes....
}
db.properties
guest_conn=jdbc:oracle:thin:#33.333.3.33:3333:DKSE1
guest_user=guest
guest_pass=guestpw
user_conn=jdbc:oracle:thin#11.222.3.33:4444:ESMZ1
user_user=user
user_pass=upw1
user1_conn=jdbc:oracle:thin#11.11.1.11:1111:GEPD1
user1_user=user1
user1_pass=upw1
batch_exec_path = ${SERVER_DIR}
Stacktrace
Nov 30, 2012 9:03:55 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/dashboard] threw exception [An exception occurred processing JSP page /alpharunBatch.jsp at line 106
103: db_conn = userinfo + "_conn";
104: db_user = userinfo + "_user";
105: db_pass = userinfo + "_pass";
106: connString = properties.getProperty(db_conn).toString();
107: connUser = properties.getProperty(db_user).toString();
108: connPass = properties.getProperty(db_pass).toString();
109: bPath = properties.getProperty("exec_path").toString();
Stacktrace:] with root cause
java.lang.NullPointerException
at org.apache.jsp.alpharunBatch_jsp._jspService(alpharun_jsp.java:192)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Am i doing something fundamentally wrong?
Thanks for your help!
try following for test:
change that
InputStream in = getServletContext().getResourceAsStream("/WEB-INF/db.properties");
to
InputStream in = getServletContext().getResourceAsStream("db.properties");
and put your properties in the classpath folder(src)
Furthermore change
properties.getProperty("exec_path").toString();
to
properties.getProperty("batch_exec_path").toString();

Categories